Neuron-PHP

RunCommand extends Command
in package

CLI command for running both the scheduler and queue worker together.

This command simplifies job system management by running both the scheduler and worker in separate processes, managing their lifecycle together.

Table of Contents

Properties

$arguments  : array<string|int, mixed>
$input  : Input
$inputReader  : IInputReader|null
$options  : array<string|int, mixed>
$output  : Output
$childProcesses  : array<string|int, mixed>
$shutdown  : bool

Methods

configure()  : void
Configure the command (add arguments and options) Override this method to define command parameters
execute()  : int
Execute the command
getArguments()  : array<string|int, mixed>
Get all configured arguments
getDescription()  : string
Get the command description
getHelp()  : string
Get detailed help for the command.
getName()  : string
Get the command name
getOptions()  : array<string|int, mixed>
Get all configured options
handleSignal()  : void
Handle shutdown signals.
hasArgument()  : bool
Check if the command has a specific argument
hasOption()  : bool
Check if the command has a specific option
setInput()  : self
Set the input instance
setInputReader()  : self
Set the input reader (for dependency injection, especially in tests).
setOutput()  : self
Set the output instance
validate()  : void
Validate that required arguments are present
addArgument()  : self
Add an argument to the command
addOption()  : self
Add an option to the command
choice()  : string
Prompt user to select from a list of options.
confirm()  : bool
Ask user for yes/no confirmation.
getInputReader()  : IInputReader
Get the input reader instance.
prompt()  : string
Prompt user for input.
secret()  : string
Prompt for sensitive input without echoing to console.
buildSchedulerCommand()  : string
Build the scheduler command.
buildWorkerCommand()  : string
Build the worker command.
cleanup()  : void
Clean up child processes.
monitorProcesses()  : void
Monitor child processes and handle their output.
registerSignalHandlers()  : void
Register signal handlers for graceful shutdown.
startScheduler()  : void
Start the scheduler process.
startWorker()  : void
Start the worker process.

Properties

$arguments

protected array<string|int, mixed> $arguments = []

$options

protected array<string|int, mixed> $options = []

$childProcesses

private array<string|int, mixed> $childProcesses = []

Methods

configure()

Configure the command (add arguments and options) Override this method to define command parameters

public configure() : void
Tags
inheritDoc

execute()

Execute the command

public execute() : int
Tags
inheritDoc
Return values
int

Exit code (0 for success)

getArguments()

Get all configured arguments

public getArguments() : array<string|int, mixed>
Return values
array<string|int, mixed>

getDescription()

Get the command description

public getDescription() : string
Tags
inheritDoc
Return values
string

getHelp()

Get detailed help for the command.

public getHelp() : string
Return values
string

getName()

Get the command name

public getName() : string
Tags
inheritDoc
Return values
string

getOptions()

Get all configured options

public getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

handleSignal()

Handle shutdown signals.

public handleSignal(int $signal) : void
Parameters
$signal : int

hasArgument()

Check if the command has a specific argument

public hasArgument(string $name) : bool
Parameters
$name : string
Return values
bool

hasOption()

Check if the command has a specific option

public hasOption(string $name) : bool
Parameters
$name : string
Return values
bool

setInput()

Set the input instance

public setInput(Input $input) : self
Parameters
$input : Input
Return values
self

setInputReader()

Set the input reader (for dependency injection, especially in tests).

public setInputReader(IInputReader $inputReader) : self

This allows tests to inject a TestInputReader with pre-programmed responses instead of requiring actual user input.

Parameters
$inputReader : IInputReader
Return values
self

setOutput()

Set the output instance

public setOutput(Output $output) : self
Parameters
$output : Output
Return values
self

validate()

Validate that required arguments are present

public validate() : void
Tags
throws
InvalidArgumentException

addArgument()

Add an argument to the command

protected addArgument(string $name[, bool $required = false ][, string $description = '' ][, mixed $default = null ]) : self
Parameters
$name : string

Argument name

$required : bool = false

Whether the argument is required

$description : string = ''

Description of the argument

$default : mixed = null

Default value if not provided

Return values
self

addOption()

Add an option to the command

protected addOption(string $name[, string|null $shortcut = null ][, bool $hasValue = false ][, string $description = '' ][, mixed $default = null ]) : self
Parameters
$name : string

Option name (long form)

$shortcut : string|null = null

Option shortcut (single character)

$hasValue : bool = false

Whether the option accepts a value

$description : string = ''

Description of the option

$default : mixed = null

Default value if not provided

Return values
self

choice()

Prompt user to select from a list of options.

protected choice(string $message, array<string|int, string> $options[, string|null $default = null ]) : string

Convenience method that delegates to the input reader. Users can select by entering either the option index (numeric) or the exact option text.

Example:

$env = $this->choice(
    "Select environment:",
    ['development', 'staging', 'production'],
    'development'
);
Parameters
$message : string

The prompt message

$options : array<string|int, string>

Available options

$default : string|null = null

Default option (will be marked with *)

Return values
string

The selected option

confirm()

Ask user for yes/no confirmation.

protected confirm(string $message[, bool $default = false ]) : bool

Convenience method that delegates to the input reader. Accepts: y, yes, true, 1 (case-insensitive) as positive responses.

Example:

if( $this->confirm( "Delete all files?" ) ) {
    // User confirmed
}
Parameters
$message : string

The confirmation message

$default : bool = false

Default value if user just presses enter

Return values
bool

True if user confirms, false otherwise

getInputReader()

Get the input reader instance.

protected getInputReader() : IInputReader

Creates a default StdinInputReader if not already set. This enables testable CLI commands by abstracting user input.

If output hasn't been set, a default Output instance will be created automatically.

Return values
IInputReader

prompt()

Prompt user for input.

protected prompt(string $message) : string

Convenience method that delegates to the input reader.

Example:

$name = $this->prompt( "Enter your name: " );
Parameters
$message : string

The prompt message to display

Return values
string

The user's response (trimmed)

secret()

Prompt for sensitive input without echoing to console.

protected secret(string $message) : string

Convenience method that delegates to the input reader. Note: Secret input hiding only works on Unix-like systems.

Example:

$password = $this->secret( "Enter password: " );
Parameters
$message : string

The prompt message

Return values
string

The user's input (trimmed)

buildSchedulerCommand()

Build the scheduler command.

private buildSchedulerCommand() : string
Return values
string

buildWorkerCommand()

Build the worker command.

private buildWorkerCommand() : string
Return values
string

cleanup()

Clean up child processes.

private cleanup() : void

monitorProcesses()

Monitor child processes and handle their output.

private monitorProcesses() : void

registerSignalHandlers()

Register signal handlers for graceful shutdown.

private registerSignalHandlers() : void

startScheduler()

Start the scheduler process.

private startScheduler() : void

startWorker()

Start the worker process.

private startWorker() : void

        
On this page

Search results