Download the PHP package evgeek/scheduler without Composer
On this page you can find all versions of the php package evgeek/scheduler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download evgeek/scheduler
More information about evgeek/scheduler
Files in evgeek/scheduler
Package scheduler
Short Description Simple scheduler
License MIT
Homepage https://github.com/evgeek/scheduler
Informations about the package scheduler
Simple PHP Scheduler
Supports various types of tasks, task batches, customizable logging with any PSR-3 logger or stdout/stderr, full launch log in the database (powered by Doctrine DBAL).
Installation
Requires PHP version 7.2 or higher
Basic usage
Create php file ( for example) with scheduler setup code:
And add new line to crontab to run your scheduler file every minute:
Supported task types
All tasks are created using the method. The task type is recognized automatically.
Closure
Bash command
Php file
Job interface
The scheduler can work with any class that implements a simple interface (single required method must run the task).
Bunch
You can combine several tasks of different types into a batch and manage their launch in the same way as a single task.
Setting up a task
After creating a task using the method, you can add it to the scheduler using the method. After that, various methods of setting up the task launch become available.
Work mode methods
- repetitive - using the (every X minutes) or (X minutes after previous launch finished) methods.
- interval - using the , , , and methods. If interval mode is used with repetitive mode, the task will be launched repetitive at the specified intervals, otherwise the task will be launched once per interval.
Examples
-
Every hour
-
Every night all night with 5 minutes delay
-
Every Sunday, once from 03:00 to 04:00 and once from 15:00 to 16:00
-
Once on 1st January and 1st December, and every Monday and Wednesday in January and December
- Every minute January 1, 2022
Setup methods
-
- task name for the log.
-
- task description for the log.
- (default ) - if true, the task cannot start if another instance of this task is currently running.
- (default ) - how long (in minutes) the task lock should be recognized as frozen and reset.
- (default ) - how many attempts to complete the task should be made in case of an error.
- (default ) - how long (in minutes) to wait before retrying the failed task.
Helper methods
-
- returns array with task settings.
-
- send a message to the debug channel immediately.
-
- send a message to the error channel immediately.
Scheduler setup
You can configure scheduler with handler object implements and config object . Example:
Handlers
Lock handler, implements . So far, only one is available.
- \ Stores locks in the database with tasks information in one table and a full launch log in another. Needs configured Doctrine DBAL object as first parameter of constructor. You can pass custom task table name to the second optional parameter, and table name to the third.
Config
Allows you to configure other scheduling options. You can do this using constructor parameters ( object is immutable). Constructor parameters are of the following types:
Logger
The scheduler has two log channels: for getting detailed launch information and for task errors. Methods for configure:
- (default ) - set PSR-3 compatible logger. If passed , log will be sent to /.
- (default ) - if / - debug messages from the scheduler will be disabled. If - enabled, with PSR-3 logger messages will be sent to the level, without - to . And you can pass custom log level (like ) to change the default PSR-3 channel.
- (default ) - if / - error messages from the scheduler and tasks will be disabled. If - enabled, with PSR-3 logger messages will be sent to the level, without - to . And you can pass custom log level (like ) to change the default PSR-3 channel.
- (default ) - registers shutdown function for log uncaught errors such as PHP errors or incorrect task settings.
- (default ) - ff true, PHP non-fatal errors will be sent to the error channel, otherwise - to the debug channel.
-
(default ) - formatting template for task logger. Pass for set default formatting (). Available variables:
-
Lowercase for regular case, uppercase - for forced uppercase. Debug log message example with default formatting ( don't forgot create with enabled ):
-
(default ) - the maximum length of the log message (the longer one will be truncated) . Set null for disable truncation. Useful if some logging channel does not accept too long messages.
- (default ) - for more complex exceptions handling. You can pass an array of mapping to select the logging level for a specific exception:
First, the mapping goes according to a specific class, then - according to the parents, from top to bottom. Therefore, place more abstract errors below.
-
(default ) - formatting template for logging exceptions ( part of the parameter). Pass for set default formatting (). Available variables:
{{header}}
- accompanying message{{code}}
-{{class}}
- class of exception{{message}}
{{stacktrace}}
-
(default ) - the maximum length of the exception message (the longer one will be truncated) . Set null for disable truncation. Useful with if your exceptions messages are too long, and you don't want to truncate the stacktrace.
- (default ) - enable/disable shell output for
bash command
tasks.
Default task options
Some options for setting default task options. The parameters specified in the task overwrite the default values.
- (default ) - if true, the task cannot start if another instance of this task is currently running.
- (default ) - how long (in minutes) the task lock should be recognized as frozen and reset.
- (default ) - how many attempts to complete the task should be made in case of an error.
- (default
0
) - how long (in minutes) to wait before retrying the failed task.
Others
$restartAfterFail
(defaultfalse
) - If true, the failed task will be restarted without delay for Every/Delay modes, and at the same interval for Single mode (but only at the correct interval).$minimumIntervalLength
(default30
) - Minimum interval size in minutes (for task methodaddInterval()
). Currently, tasks are started sequentially and synchronously, so the scheduler cannot guarantee the exact time when the task will start. Because of this, I had to limit the minimum size of the interval to make sure that the task will not be missed because the interval is too small. This is not a good decision. In future updates, task launching will be implemented asynchronously, and the interval limitation will be removed.
Future plans
- Asynchronous task launch.
- Tests.
- More lock handlers, first - file lock handler as default behavior.
- More scheduling options, including launch at exact time.
- Managing the scheduler using console commands - list of task, force start all or specific task etc.
All versions of scheduler with dependencies
psr/log Version ^1.1
nesbot/carbon Version ^2.54
doctrine/dbal Version ^3.2
ext-json Version *