Scheduler
extends CommandLineBase
in package
Advanced job scheduling and execution engine for the Neuron framework.
This scheduler provides comprehensive job management with cron-like scheduling, daemon operation, and robust error handling. It supports both one-time job execution and continuous polling for scheduled jobs.
Key features:
- YAML-based job configuration with cron expressions
- Daemon mode with configurable polling intervals
- Job lifecycle management (creation, execution, cleanup)
- Comprehensive logging and error handling
- Debug mode for development and troubleshooting
- Integration with the Neuron logging system
Configuration format (schedule.yaml):
jobs:
email_cleanup:
schedule: "0 2 * * *" # Daily at 2 AM
class: "App\\Jobs\\EmailCleanup"
backup_database:
schedule: "0 1 * * 0" # Weekly on Sunday at 1 AM
class: "App\\Jobs\\BackupDatabase"
Tags
Table of Contents
Properties
- $_crashed : bool
- $_handleErrors : bool
- $_handleFatal : bool
- $_parameters : array<string|int, mixed>
- $_settings : SettingManager|null
- $_version : string
- $_basePath : string|null
- $_configFile : string
- $_debug : bool
- $_eventListenersPath : string
- $_handlers : array<string|int, mixed>
- $_interval : int
- $_jobs : array<string|int, mixed>
- $_poll : bool
- $_registry : Registry|null
Methods
- __construct() : mixed
- Initial setup for the application.
- addJob() : void
- Add a job to the scheduler
- beautifyException() : string
- Format exception/error for display Base implementation outputs plain text (CLI-friendly) MVC Application overrides this for HTML output
- fatalHandler() : void
- Handler for fatal errors.
- getBasePath() : string
- getConfigFile() : string
- getCrashed() : bool
- getDescription() : string
- Get the description of the application for --help.
- getEventListenersPath() : string
- getInterval() : int
- getJobs() : array<string|int, mixed>
- getParameter() : mixed
- Gets a parameter by name.
- getParameters() : array<string|int, mixed>
- returns parameters passed to the run method.
- getRegistryObject() : mixed
- getSetting() : mixed
- getSettingManager() : SettingManager|null
- getVersion() : string
- Application version number.
- globalExceptionHandler() : void
- Global exception handler for uncaught exceptions and errors Handles both Exception and Error (PHP 7+)
- infinitePoll() : void
- Infinite poll loop.
- initEvents() : void
- Loads event-listeners.yaml and maps all event listeners to their associated events.
- initLogger() : void
- Initializes the logger based on the parameters set in neuron.yaml.
- isCommandLine() : bool
- Returns true if the application is running in command line mode.
- loadSchedule() : array<string|int, mixed>
- phpErrorHandler() : bool
- Handler for PHP errors.
- poll() : int
- Single job poll.
- run() : bool
- Call to run the application.
- scheduleJobs() : void
- Schedule jobs from configuration
- setBasePath() : Scheduler
- Set the base path for configuration files
- setConfigFile() : Scheduler
- setDebug() : Scheduler
- setEventListenersPath() : Base
- setHandleErrors() : Base
- setHandleFatal() : Base
- setInterval() : Scheduler
- setRegistryObject() : mixed
- setSetting() : void
- setSettingSource() : $this
- willHandleErrors() : bool
- willHandleFatal() : bool
- addHandler() : void
- Adds a handler for command line parameters.
- executeInitializers() : void
- Executes all initializer classes located in app/Initializers.
- formatFatalError() : string
- Format fatal error for display Uses HTML for web, plain text for CLI
- getHandlers() : array<string|int, mixed>
- Returns an array of all handlers for command line parameters.
- help() : bool
- Activated by the --help parameter. Shows all configured switches and their hints.
- initErrorHandlers() : void
- Sets up the php error and fatal handlers.
- initSettings() : void
- intervalCommand() : bool
- Command line parameter to set the interval between polls.
- onCrash() : void
- Called by the fatal handler if invoked.
- onError() : bool
- Called for any unhandled exceptions.
- onFinish() : void
- Called immediately after onRun.
- onRun() : void
- Must be implemented by derived classes.
- onStart() : bool
- Called by ApplicationBase. Returning false terminates the application.
- pollCommand() : bool
- Command line parameter to poll events one time.
- processParameters() : bool
- Processes all parameters passed to the application.
- handleParameter() : bool
- Handles a single parameter passed on the command line.
- initSchedule() : void
- Schedule the jobs from the schedule.yaml file.
Properties
$_crashed
protected
bool
$_crashed
= false
$_handleErrors
protected
bool
$_handleErrors
= false
$_handleFatal
protected
bool
$_handleFatal
= false
$_parameters
protected
array<string|int, mixed>
$_parameters
$_settings
protected
SettingManager|null
$_settings
= null
$_version
protected
string
$_version
$_basePath
private
string|null
$_basePath
= null
$_configFile
private
string
$_configFile
= 'schedule.yaml'
$_debug
private
bool
$_debug
= false
$_eventListenersPath
private
string
$_eventListenersPath
$_handlers
private
array<string|int, mixed>
$_handlers
$_interval
private
int
$_interval
= 60
$_jobs
private
array<string|int, mixed>
$_jobs
= []
$_poll
private
bool
$_poll
= false
$_registry
private
Registry|null
$_registry
Methods
__construct()
Initial setup for the application.
public
__construct(string $version[, ISettingSource|null $source = null ]) : mixed
Loads the config file. Initializes the logger.
Parameters
- $version : string
- $source : ISettingSource|null = null
Tags
addJob()
Add a job to the scheduler
public
addJob(string $name, string $cron, IJob $job[, array<string|int, mixed> $arguments = [] ][, string|null $queue = null ]) : void
Parameters
- $name : string
-
Job name
- $cron : string
-
Cron expression
- $job : IJob
-
Job instance
- $arguments : array<string|int, mixed> = []
-
Job arguments
- $queue : string|null = null
-
Queue name (null = run directly, not queued)
beautifyException()
Format exception/error for display Base implementation outputs plain text (CLI-friendly) MVC Application overrides this for HTML output
public
static beautifyException(Throwable $throwable) : string
Parameters
- $throwable : Throwable
Return values
stringfatalHandler()
Handler for fatal errors.
public
fatalHandler() : void
Checks for actual fatal errors and formats them for display
getBasePath()
public
getBasePath() : string
Return values
stringgetConfigFile()
public
getConfigFile() : string
Return values
stringgetCrashed()
public
getCrashed() : bool
Return values
boolgetDescription()
Get the description of the application for --help.
public
getDescription() : string
Return values
stringgetEventListenersPath()
public
getEventListenersPath() : string
Return values
stringgetInterval()
public
getInterval() : int
Return values
intgetJobs()
public
getJobs() : array<string|int, mixed>
Return values
array<string|int, mixed>getParameter()
Gets a parameter by name.
public
getParameter(string $name) : mixed
Parameters
- $name : string
getParameters()
returns parameters passed to the run method.
public
getParameters() : array<string|int, mixed>
Return values
array<string|int, mixed>getRegistryObject()
public
getRegistryObject(string $name) : mixed
Parameters
- $name : string
getSetting()
public
getSetting(string $section, string $name) : mixed
Parameters
- $section : string
- $name : string
getSettingManager()
public
getSettingManager() : SettingManager|null
Return values
SettingManager|nullgetVersion()
Application version number.
public
getVersion() : string
Return values
stringglobalExceptionHandler()
Global exception handler for uncaught exceptions and errors Handles both Exception and Error (PHP 7+)
public
globalExceptionHandler(Throwable $throwable) : void
Parameters
- $throwable : Throwable
infinitePoll()
Infinite poll loop.
public
infinitePoll() : void
initEvents()
Loads event-listeners.yaml and maps all event listeners to their associated events.
public
initEvents() : void
initLogger()
Initializes the logger based on the parameters set in neuron.yaml.
public
initLogger() : void
destination format file level
Tags
isCommandLine()
Returns true if the application is running in command line mode.
public
isCommandLine() : bool
Return values
boolloadSchedule()
public
loadSchedule() : array<string|int, mixed>
Return values
array<string|int, mixed>phpErrorHandler()
Handler for PHP errors.
public
phpErrorHandler(int $errorNo, string $message, string $file, int $line) : bool
Parameters
- $errorNo : int
- $message : string
- $file : string
- $line : int
Return values
boolpoll()
Single job poll.
public
poll() : int
Checks all scheduled jobs and either runs them directly or dispatches to queue.
Return values
int —Number of jobs triggered (run or dispatched).
run()
Call to run the application.
public
run([array<string|int, mixed> $argv = [] ]) : bool
Parameters
- $argv : array<string|int, mixed> = []
Tags
Return values
boolscheduleJobs()
Schedule jobs from configuration
public
scheduleJobs(array<string|int, mixed> $schedule) : void
Parameters
- $schedule : array<string|int, mixed>
-
Schedule configuration array
setBasePath()
Set the base path for configuration files
public
setBasePath(string $basePath) : Scheduler
Parameters
- $basePath : string
Return values
SchedulersetConfigFile()
public
setConfigFile(string $configFile) : Scheduler
Parameters
- $configFile : string
Return values
SchedulersetDebug()
public
setDebug(bool $debug) : Scheduler
Parameters
- $debug : bool
Return values
SchedulersetEventListenersPath()
public
setEventListenersPath(string $eventListenersPath) : Base
Parameters
- $eventListenersPath : string
Return values
BasesetHandleErrors()
public
setHandleErrors(bool $handleErrors) : Base
Parameters
- $handleErrors : bool
Return values
BasesetHandleFatal()
public
setHandleFatal(bool $handleFatal) : Base
Parameters
- $handleFatal : bool
Return values
BasesetInterval()
public
setInterval(int $interval) : Scheduler
Parameters
- $interval : int
Return values
SchedulersetRegistryObject()
public
setRegistryObject(string $name, mixed $object) : mixed
Parameters
- $name : string
- $object : mixed
setSetting()
public
setSetting(string $section, string $name, string $value) : void
Parameters
- $section : string
- $name : string
- $value : string
setSettingSource()
public
setSettingSource(ISettingSource $source) : $this
Parameters
- $source : ISettingSource
Return values
$thiswillHandleErrors()
public
willHandleErrors() : bool
Return values
boolwillHandleFatal()
public
willHandleFatal() : bool
Return values
booladdHandler()
Adds a handler for command line parameters.
protected
addHandler(string $switch, string $description, string $method[, bool $param = false ]) : void
The switch is the parameter that causes the specified method to be called. If the param parameter is set to true, the token immediately following the switch on the command line will be passed as the parameter to the handler.
Parameters
- $switch : string
-
the name of the switch.
- $description : string
-
the description of the switch.
- $method : string
-
the name of the switch handler method.
- $param : bool = false
-
if true, the next parameter will be passed to the handler as the value of the switch.
Tags
executeInitializers()
Executes all initializer classes located in app/Initializers.
protected
executeInitializers() : void
formatFatalError()
Format fatal error for display Uses HTML for web, plain text for CLI
protected
formatFatalError(string $type, string $message, string $file, int $line) : string
Parameters
- $type : string
- $message : string
- $file : string
- $line : int
Return values
stringgetHandlers()
Returns an array of all handlers for command line parameters.
protected
getHandlers() : array<string|int, mixed>
Return values
array<string|int, mixed>help()
Activated by the --help parameter. Shows all configured switches and their hints.
protected
help() : bool
Return values
boolinitErrorHandlers()
Sets up the php error and fatal handlers.
protected
initErrorHandlers() : void
initSettings()
protected
initSettings(ISettingSource|null $source) : void
Parameters
- $source : ISettingSource|null
intervalCommand()
Command line parameter to set the interval between polls.
protected
intervalCommand(int $interval) : bool
Parameters
- $interval : int
-
interval in seconds.
Return values
boolonCrash()
Called by the fatal handler if invoked.
protected
onCrash(array<string|int, mixed> $error) : void
Parameters
- $error : array<string|int, mixed>
onError()
Called for any unhandled exceptions.
protected
onError(string $message) : bool
Returning false skips executing onFinish.
Parameters
- $message : string
Return values
boolonFinish()
Called immediately after onRun.
protected
onFinish() : void
onRun()
Must be implemented by derived classes.
protected
onRun([array<string|int, mixed> $argv = [] ]) : void
Parameters
- $argv : array<string|int, mixed> = []
onStart()
Called by ApplicationBase. Returning false terminates the application.
protected
onStart() : bool
Return values
boolpollCommand()
Command line parameter to poll events one time.
protected
pollCommand() : bool
Return values
boolprocessParameters()
Processes all parameters passed to the application.
protected
processParameters() : bool
Return values
bool —returns false if the execution should be halted.
handleParameter()
Handles a single parameter passed on the command line.
private
handleParameter(string $param, int &$index) : bool
Parameters
- $param : string
- $index : int
Return values
bool —returns false if the execution should be halted.
initSchedule()
Schedule the jobs from the schedule.yaml file.
private
initSchedule() : void