2017 © Pedro Peláez
 

library console-process

Console implementation.

image

phlib/console-process

Console implementation.

  • Friday, February 16, 2018
  • by letssurf
  • Repository
  • 5 Watchers
  • 10 Stars
  • 2,215 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 10 Versions
  • 54 % Grown

The README.md

phlib/console-process

Code Checks Codecov Latest Stable Version Total Downloads Licence, (*1)

Console signal implementation using PHPs Process control functions., (*2)

There are 2 implementations., (*3)

  1. Background command. Repeatedly execute a command, until interrupted using the signal handler.
  2. Daemon command. Builds on the Background command to allow forking (detaching) the process.

Install

Via Composer, (*4)

``` bash $ composer require phlib/console-process, (*5)


## Background Command ### Basic Usage The Background Command is implemented in the same way as you're used to with the normal Symfony Command, however it must allow for the `execute()` method to be called multiple times. There is a processing delay between each execution which can be customised. ```php <?php declare(strict_types=1); use Phlib\Console\Command\BackgroundCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyProcessCommand extends BackgroundCommand { protected function configure(): void { $this->setName('my:process') ->setDescription('My background process.'); } protected function execute(InputInterface $input, OutputInterface $output): int { $output->writeln('Doing important work!'); return 0; } }

Stopping execution

Normal usage

Typically, the command will continue execution until interrupted by a signal, e.g. a user pressing Ctrl+C., (*6)

Self-termination

Alternatively, if an implementation has a finite task, for example deleting records in batches, it may need to terminate itself once the task is complete. This is done by calling shutdown()., (*7)

class MyProcessCommand extends BackgroundCommand
{
    // ...

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $countDeleted = $this->deleteBatchOfRecords();
        if ($countDeleted === 0) {
            $output->writeln('All done!');
            $this->shutdown();
        }
        return 0;
    }
}

Non-zero exit

If an execution returns a non-zero exit code, iteration will be stopped and the exit code will be passed back to the console, as with a normal Symfony Command., (*8)

class MyProcessCommand extends BackgroundCommand
{
    // ...

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $isInputValid = $this->someValidationChecks($input);
        if ($isInputValid === false) {
            $output->writeln('Message explaining invalid input');
            return 1;
        }

        // do some work

        return 0;
    }
}

Lifecycle Methods

The background command has additional methods that get called when the process starts and finishes. Useful for any initialising or final cleanup., (*9)

  • onStart(InputInterface $input, OutputInterface $output): void
    • Similar to standard initialize(); useful for DaemonCommand.
  • onShutdown(InputInterface $input, OutputInterface $output): void
  • onException(\Exception $e, InputInterface $input, OutputInterface $output): void
class MyProcessCommand extends BackgroundCommand
{
    // ...

    protected function onShutdown(InputInterface $input, OutputInterface $output): void
    {
        $output->writeln('onShutdown method called.');
    }
}

Daemon Command

Basic Usage

Apart from extending a different class, the Daemon Command looks and works in a similar way to the Background Command. The PID file argument is optional. If it is not specified the process generates it's own PID file based on the command name., (*10)

<?php

declare(strict_types=1);

use Phlib\Console\Command\DaemonCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyProcessCommand extends DaemonCommand
{
    protected function configure()
    {
        $this->setName('my:process')
            ->setDescription('My background process.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Doing important work!');
    }
}

Output

Once a daemon process is detached, the original output is also lost. The --child-log | -o option can be used to specify a filename to write output. Alternatively, the createChildOutput() method can be overridden to return a new output instance. For example:, (*11)

use Symfony\Component\Console\Output\StreamOutput;

class MyProcessCommand extends DaemonCommand
{
    // ... 

    protected function createChildOutput()
    {
        return new MyOutputToLoggerClass();
    }
}

Lifecycle Methods

The daemon command has additional methods, to the standard Symfony command methods and the background command, which gets called during the process lifecycle. The following example demonstrates overriding the methods., (*12)

class MyProcessCommand extends DaemonCommand
{
    // ...

    protected function onBeforeDaemonize(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onBeforeDaemonize method called.');
    }

    protected function onAfterDaemonizeChild(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onAfterDaemonizeChild method called.');
    }

    protected function onAfterDaemonizeParent(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('onAfterDaemonizeParent method called.');
    }

    protected function onStart(InputInterface $input, OutputInterface $output): void
    {
        // Similar to `onAfterDaemonizeChild()` but also called if `--daemonize` option is not set.
        $output->writeln('onStart method called.');
    }
}

Command Line

# path/to/my/process start -d
# path/to/my/process status
# path/to/my/process stop

Options

Name Short Type Required Default Description
action Argument yes start, stop, status
daemonize d Option no no Detaches the process
pid-file p Option no auto Name of the PID file to use. Not used if daemonize is not set.
child-log o Option no no Name of the file to save child output. Not used if daemonize is not set.

License

This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version., (*13)

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details., (*14)

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/., (*15)

The Versions

16/02 2018

dev-master

9999999-dev

Console implementation.

  Sources   Download

LGPL-3.0

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

16/02 2018

1.0.1

1.0.1.0

Console implementation.

  Sources   Download

LGPL-3.0

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

13/06 2017

1.0.0

1.0.0.0

Console implementation.

  Sources   Download

LGPL-3.0

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

03/01 2017

0.7

0.7.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

01/07 2016

0.6

0.6.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

19/02 2016

0.5

0.5.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

21/01 2016

0.4

0.4.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

20/01 2016

0.3

0.3.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

07/01 2016

0.2

0.2.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix

02/12 2015

0.1

0.1.0.0

Console implementation.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Luke Richards

console symfony background pcntl daemon posix