{"id":20140253,"url":"https://github.com/10quality/scheduler","last_synced_at":"2025-09-11T16:42:29.805Z","repository":{"id":56937931,"uuid":"60442341","full_name":"10quality/scheduler","owner":"10quality","description":"PHP job scheduler. (Cronjob tasker)","archived":false,"fork":false,"pushed_at":"2019-11-16T19:19:50.000Z","size":27,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":5,"default_branch":"v1.0","last_synced_at":"2025-01-13T10:13:58.850Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/10quality.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-05T03:58:47.000Z","updated_at":"2019-11-16T19:18:23.000Z","dependencies_parsed_at":"2022-08-21T06:50:20.799Z","dependency_job_id":null,"html_url":"https://github.com/10quality/scheduler","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fscheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fscheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fscheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fscheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/10quality","download_url":"https://codeload.github.com/10quality/scheduler/tar.gz/refs/heads/v1.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241583975,"owners_count":19986076,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T21:49:54.395Z","updated_at":"2025-03-02T23:22:50.126Z","avatar_url":"https://github.com/10quality.png","language":"PHP","readme":"# Scheduler\n\n[![Latest Stable Version](https://poser.pugx.org/10quality/scheduler/v/stable)](https://packagist.org/packages/10quality/scheduler)\n[![Total Downloads](https://poser.pugx.org/10quality/scheduler/downloads)](https://packagist.org/packages/10quality/scheduler)\n[![License](https://poser.pugx.org/10quality/scheduler/license)](https://packagist.org/packages/10quality/scheduler)\n\nPHP job scheduler. (Cronjob tasker)\n\n## Installing\n\nVia composer:\n\n```bash\ncomposer require 10quality/scheduler\n```\n\n## Usage\n\n### Scheduler\n\nCreate a php file that will be called by cronjob, [Sample](https://github.com/10quality/scheduler/blob/v1.0/tests/_environment/scheduler.php), and include the following code lines:\n\n```php\n// 1) LOAD COMPOSER AUTOLOAD OR BOOTSTRAP FIRST\n\n// 2)\nuse Scheduler\\Scheduler;\n```\n\nInit scheduler with the required configuration:\n```php\nScheduler::ready([\n    'jobs'      =\u003e ['path' =\u003e __DIR__.'/jobs'],\n    'session'   =\u003e ['driver' =\u003e 'file', 'path'=\u003e__DIR__.'/.tmp'],\n]);\n```\n\n*NOTE:* Scheduler requires a defined jobs path (where jobs are located) and the session driver/settings to use to handle intervals (the package only supports file atm).\n\nRegister your jobs and execution interval.\n```php\nScheduler::ready([...])\n    -\u003ejob('MyJob', function($task) {\n        return $task-\u003edaily();\n    });\n```\n\nStart scheduler:\n```php\nScheduler::ready([...])\n    -\u003ejob(...)\n    -\u003estart();\n```\n\nThen setup a cronjob task to run scheduler, as follows:\n```bash\n* * * * * php /path/to/scheduler-file.php \u003e\u003e /dev/null 2\u003e\u00261\n```\n\n### Jobs\n\nScheduler runs jobs written in PHP. A job is a PHP class extended from `Scheduler\\Base\\Job` class. Once a job is registered in the scheduler, it will call to the `execute()` function in runtime, [Sample](https://github.com/10quality/scheduler/blob/v1.0/tests/_environment/jobs/TestJob.php).\n\nCreate a job mimicking the following example:\n\n```php\nuse Scheduler\\Base\\Job;\n\nclass MyJob extends Job\n{\n    public function execute()\n    {\n        // My code here...\n    }\n}\n```\n\nFor the example above, the job file must be named `MyJob.php` and must be stored in the `jobs path`.\n\n### Intervals\n\nExecution intervals are defined when registering a job:\n```php\nScheduler::ready([...])\n    -\u003ejob('MyJob', function($task) {\n        // Here we define the task interval\n        return $task-\u003edaily();\n    });\n```\n\nAvailable intervals:\n```php\n// On every execution\n$task-\u003enow();\n\n// Daily\n$task-\u003edaily();\n\n// Weekly\n$task-\u003eweekly();\n\n// Monthly\n$task-\u003emonthly();\n\n// Every minute\n$task-\u003eeveryMinute();\n\n// Every 5 minutes\n$task-\u003eeveryFiveMinutes();\n\n// Every 10 minutes\n$task-\u003eeveryTenMinutes();\n\n// Every 30 minutes\n$task-\u003eeveryHalfHour();\n\n// Every hour\n$task-\u003eeveryHour();\n\n// Every 12 hours\n$task-\u003eeveryTwelveHours();\n\n// Every 2 days\n$task-\u003eeveryTwoDays();\n\n// Every 3 days\n$task-\u003eeveryThreeDays();\n\n// Every XXX minutes (custom minutes)\n// @param init $minutes Custome minutes interval\n$task-\u003ecustom($minutes);\n```\n\n### Events\n\nYou can define callable event handlers when configuring the scheduler, like in the following example:\n\n```php\nScheduler::ready([\n    'jobs'      =\u003e ['path' =\u003e __DIR__.'/jobs'],\n    'session'   =\u003e ['driver' =\u003e 'file', 'path'=\u003e__DIR__.'/.tmp'],\n    'events'    =\u003e [\n                    'on_start' =\u003e function($microtime) {\n                        // Do something during event\n                    },\n                    'on_job_start' =\u003e function($jobName, $microtime) {\n                        if ($jobName === 'Job')\n                            // Do something during event\n                    }\n                ],\n]);\n```\n\nList of events (parameters can be used optionally):\n\n| Event key | Parameters | Description |\n| --- | --- | --- |\n| `on_init` |  | Triggered before session is validated. |\n| `on_start` | *int* **$microtime** | Triggered before executing any job. |\n| `on_finish` | *int* **$microtime** | Triggered after executing all jobs and saving session. |\n| `on_job_start` | *string* **$jobName**, *int* **$microtime** | Triggered before executing of a job. |\n| `on_job_finish` | *string* **$jobName**, *int* **$microtime** | Triggered after executing of job. |\n\n### Handling Exceptions\n\nScheduler will run continuously without interruption. You can handle exceptions individually per job or globally to log them as needed.\n\n#### Global Exception Handling\n\nAdd a `on_exception` callable handler in the scheduler's events configuration, like in the following example:\n\n```php\nScheduler::ready([\n    'jobs'      =\u003e ['path' =\u003e __DIR__.'/jobs'],\n    'session'   =\u003e ['driver' =\u003e 'file', 'path'=\u003e__DIR__.'/.tmp'],\n    'events'    =\u003e [\n                    'on_exception' =\u003e function($e) {\n                        // Do anything with exception\n                        echo $e-\u003egetMessage();\n                    }\n                ],\n]);\n```\n\n#### Job Exceptions\n\nAdd a exception handling callable when defining the job task, like this:\n\n```php\nScheduler::ready([...])\n    -\u003ejob('MyJob', function($task) {\n        // Here we define the task interval\n        return $task-\u003edaily()\n            -\u003eonException(function($e) {\n                // Do anything with exception\n                echo $e-\u003egetMessage();\n            });\n    });\n```\n\nTo reset the job when an exception occurs and force it to be executed on the next run, indicate it on the task:\n```php\nScheduler::ready([...])\n    -\u003ejob('MyJob', function($task) {\n        // Here we define the task interval\n        return $task-\u003edaily()\n            -\u003ecanReset()\n            -\u003eonException(function(Exception $e) {\n                // Do anything with exception\n            });\n    });\n```\n\n### Session\n\n#### File\n\nIndicate the folder where session file will be stored. Make sure this folder has the proper permissions.\n\n```php\nScheduler::ready([\n    'jobs'      =\u003e ['path' =\u003e __DIR__.'/jobs'],\n    'session'   =\u003e ['driver' =\u003e 'file', 'path'=\u003e__DIR__.'/.tmp'],\n]);\n```\n\n#### Callable\n\nYou can create your own session driver by extending from the [Session](https://github.com/10quality/scheduler/blob/v1.0/src/Contracts/Session.php) interface:\n\n```php\n\nuse Scheduler\\Contracts\\Session;\n\nclass MySessionDriver implements Session\n{\n    /*\n     * See and develop required interface methods....\n     */\n}\n```\n\nThen, when initializing the scheduler, set the driver to `callable` and add the *callable* as second parameter, like the following example:\n\n```php\nScheduler::ready([\n    'jobs'      =\u003e ['path' =\u003e __DIR__.'/jobs'],\n    'session'   =\u003e [\n                    'driver'    =\u003e 'callable',,\n                    'callable'  =\u003e function() use(\u0026$session) {\n                        return MySessionDriver::load( $custom_options );\n                    }\n                ],\n]);\n```\n\n## Requirements\n\n* PHP \u003e= 5.4\n\n## Coding guidelines\n\nPSR-4.\n\n## LICENSE\n\nThe MIT License (MIT)\n\nCopyright (c) 2016-2019 [10Quality](http://www.10quality.com).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10quality%2Fscheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F10quality%2Fscheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10quality%2Fscheduler/lists"}