{"id":13669529,"url":"https://github.com/Indatus/dispatcher","last_synced_at":"2025-04-27T04:33:26.723Z","repository":{"id":14792067,"uuid":"17514103","full_name":"Indatus/dispatcher","owner":"Indatus","description":"Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.","archived":false,"fork":false,"pushed_at":"2019-03-02T17:58:02.000Z","size":1207,"stargazers_count":1061,"open_issues_count":0,"forks_count":70,"subscribers_count":39,"default_branch":"master","last_synced_at":"2024-05-22T02:42:36.995Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/Indatus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-07T13:01:48.000Z","updated_at":"2024-05-20T10:19:46.000Z","dependencies_parsed_at":"2022-08-21T10:10:54.524Z","dependency_job_id":null,"html_url":"https://github.com/Indatus/dispatcher","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Indatus%2Fdispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Indatus%2Fdispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Indatus%2Fdispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Indatus%2Fdispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Indatus","download_url":"https://codeload.github.com/Indatus/dispatcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224060356,"owners_count":17249009,"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-08-02T08:01:16.473Z","updated_at":"2024-11-11T06:30:29.629Z","avatar_url":"https://github.com/Indatus.png","language":"PHP","funding_links":[],"categories":["Popular/Notable Packages","PHP","定时任务( Crontab )","定时任务 Crontab"],"sub_categories":[],"readme":"# Dispatcher\n\n\nDispatcher allows you to schedule your artisan commands within your [Laravel](http://laravel.com) project, eliminating the need to touch the crontab when deploying.  It also allows commands to run per environment and keeps your scheduling logic where it should be, in your version control.\n\n\u003cimg align=\"left\" height=\"300\" src=\"https://s3-us-west-2.amazonaws.com/oss-avatars/dispatcher_round_readme.png\"\u003e\n\n```php\nuse Indatus\\Dispatcher\\Scheduling\\ScheduledCommand;\nuse Indatus\\Dispatcher\\Scheduling\\Schedulable;\nuse Indatus\\Dispatcher\\Drivers\\DateTime\\Scheduler;\n\nclass MyCommand extends ScheduledCommand {\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n        //every day at 4:17am\n        return $scheduler\n            -\u003edaily()\n            -\u003ehours(4)\n            -\u003eminutes(17);\n    }\n}\n```\n\n---\n\n[![Latest Stable Version](https://poser.pugx.org/indatus/dispatcher/v/stable.png)](https://packagist.org/packages/indatus/dispatcher) [![Total Downloads](https://poser.pugx.org/indatus/dispatcher/downloads.png)](https://packagist.org/packages/indatus/dispatcher) [![Build Status](https://travis-ci.org/Indatus/dispatcher.png?branch=master)](https://travis-ci.org/Indatus/dispatcher) [![Code Coverage](https://scrutinizer-ci.com/g/Indatus/dispatcher/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Indatus/dispatcher/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Indatus/dispatcher/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Indatus/dispatcher/?branch=master)\n\n## README Contents\n\n* [Features](#features)\n* [Tutorial](#tutorial)\n* [Installation](#installation)\n  * [For Laravel 4 (see 1.4 branch)](https://github.com/Indatus/dispatcher/tree/1.4#installation)\n  * [For Laravel 5](#installation) - discontinued, see [Laravel 5's scheduler](http://laravel-news.com/2014/11/laravel-5-scheduler/)\n  * [Upgrading from 1.4 to 2.0](#upgrading-1.4-2.0)\n* [Usage](#usage)\n  * [Generating New Scheduled Commands](#new-commands)\n  * [Scheduling Existing Commands](#scheduling-commands)\n  * [Running Commands As Users](#commands-as-users)\n  * [Environment-Specific Commands](#environment-commands)\n  * [Running Commands In Maintenance Mode](#maintenance-mode)\n  * [Advanced Scheduling](#advanced-scheduling)\n* [Drivers](#drivers)\n  * [DateTime](#datetime)\n* [Custom Drivers](#custom-drivers)\n* [FAQ](#faq)\n\n\u003ca name=\"features\" /\u003e\n## Features\n\n * Schedule artisan commands to run automatically\n * Scheduling is maintained within your version control system\n * Single source of truth for when and where commands run\n * Schedule commands to run with arguments and options\n * Run commands as other users\n * Run commands in certain environments\n * Use custom drivers for custom scheduling contexts\n\n\u003ca name=\"tutorial\" /\u003e\n## Tutorial\n\nBy Ben Kuhl at the [Laravel Louisville meetup](http://laravel-louisville.github.io/meetup/) ([@lurvul](https://twitter.com/lurvul)): [Video](http://vimeo.com/94212203) - [Slides](http://bkuhl.github.io/dispatcher-slides)\n\nBy Jefferey Way at [Laracasts](https://www.laracasts.com): [Recurring Tasks the Laravel Way](https://laracasts.com/lessons/recurring-tasks-the-laravel-way)\n\n\u003ca name=\"installation\" /\u003e\n## Installation\n\n\u003e **NOTICE: [Laravel 5 now includes scheduling](http://laravel-news.com/2014/11/laravel-5-scheduler/) out of the box.  This package will no longer be maintained for Laravel 5 and above**\n\n| Requirements                  | 1.4.*                         | 2.*                               |\n|-------------------------------|-------------------------------|-----------------------------------|\n| [Laravel](http://laravel.com) | 4.1/4.2                       | 5.x                               |\n| [PHP](https://php.net)        | 5.3+                          | 5.4+                              |\n| [HHVM](http://hhvm.com)       | 3.3+                          | 3.3+                              |\n| Install with Composer...      | ~1.4                          | ~2.0@dev                          |\n\n\u003e If you're using **Laravel 4** view the [readme in the 1.4 branch](https://github.com/Indatus/dispatcher/tree/1.4)\n\nAdd this line to the providers array in your `config/app.php` file :\n\n```php\n        'Indatus\\Dispatcher\\ServiceProvider',\n```\n\nAdd the following to your root Crontab (via `sudo crontab -e`):\n\n```php\n* * * * * php /path/to/artisan scheduled:run 1\u003e\u003e /dev/null 2\u003e\u00261\n```\n\nIf you are adding this to `/etc/cron.d` you'll need to specify a user immediately after `* * * * *`.\n\n\u003e You may add this to any user's Crontab, but only the root crontab can run commands as other users.\n\n\u003ca name=\"upgrading-1.4-2.0\" /\u003e\n### Upgrading from 1.4 to 2.0\n\nIn all scheduled commands...\n\n * Replace `use Indatus\\Dispatcher\\Drivers\\Cron\\Scheduler` with `use Indatus\\Dispatcher\\Drivers\\DateTime\\Scheduler`\n * Replaced uses of `Scheduler::[DAY_OF_WEEK]` with `Day::[DAY_OF_WEEK]` and `Scheduler::[MONTH_OF_YEAR]` with `Month::[MONTH_OF_YEAR]`\n * `executable` config option has been removed.  Dispatcher now inherits the [path to the binary](http://php.net/manual/en/reserved.constants.php#constant.php-binary) that was initially used to run `scheduled:run`\n\n\u003ca name=\"usage\" /\u003e\n## Usage\n\n```\nscheduled\n  scheduled:make              Create a new scheduled artisan command\n  scheduled:run               Run scheduled commands\n  scheduled:summary           View a summary of all scheduled artisan commands\n```\n\nIf commands are not visible via `php artisan` then they cannot be scheduled.\n\n\u003ca name=\"new-commands\" /\u003e\n### Generating New Scheduled Commands\n\nUse `php artisan scheduled:make` to generate a new scheduled command, the same way you would use artisan's `command:make`.  Then [register your command](http://laravel.com/docs/commands#registering-commands) with Laravel.\n\n\u003ca name=\"scheduling-commands\" /\u003e\n### Scheduling Existing Commands\n\nYou may either implement `\\Indatus\\Dispatcher\\Scheduling\\ScheduledCommandInterface` or follow the below steps.\n\n1. Add use statements to your command.  If you're using a custom driver you will use a different `Scheduler` class.\n```php\nuse Indatus\\Dispatcher\\Scheduling\\ScheduledCommand;\nuse Indatus\\Dispatcher\\Scheduling\\Schedulable;\nuse Indatus\\Dispatcher\\Drivers\\DateTime\\Scheduler;\n```\n2. Extend `\\Indatus\\Dispatcher\\Scheduling\\ScheduledCommand`\n3. Implement schedule():\n```php\n\t/**\n\t * When a command should run\n\t *\n\t * @param Scheduler $scheduler\n\t *\n\t * @return Scheduler\n\t */\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n\t\treturn $scheduler;\n    }\n```\n\nFor details and examples on how to schedule, see the [DateTime Driver](#datetime).\n\n\u003ca name=\"commands-as-users\" /\u003e\n### Running Commands As Users\n\nYou may override `user()` to run a given artisan command as a specific user.  Ensure your `scheduled:run` artisan command is running as root.\n\n```php\n    public function user()\n    {\n        return 'backup';\n    }\n```\n\n\u003e This feature may not be supported by all drivers.\n\n\u003ca name=\"environment-commands\" /\u003e\n### Environment-Specific Commands\n\nYou may override `environment()` to ensure your command is only scheduled in specific environments.  It should provide a single environment or an array of environments.\n\n```php\n    public function environment()\n    {\n        return ['development','staging'];\n    }\n```\n\n\u003ca name=\"maintenance-mode\" /\u003e\n### Maintenance Mode\n\nBy default, cron commands will *not* run when application is in Maintenance Mode. This will prevent all sorts of weird output that might occur if a cron command is run while you are migrating a database or doing a composer update.\n\nYou may override `runInMaintenanceMode()` to force your command to still be run while the application is in maintenance mode.\n\n```php\n    public function runInMaintenanceMode()\n    {\n        return true;\n    }\n```\n\n\u003ca name=\"advanced-scheduling\" /\u003e\n### Advanced scheduling\n\nYou may schedule a given command to to run at multiple times by `schedule()` returning multiple `Schedulable` instances.\n\n```php\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n        return [\n            // 5am Mon-Fri\n            $scheduler-\u003eeveryWeekday()-\u003ehours(5),\n\n            // 2am every Saturday\n            App::make(get_class($scheduler))\n                -\u003edaysOfTheWeek(Scheduler::SATURDAY)\n                -\u003ehours(2)\n        ];\n    }\n```\n\nYou may also schedule a command to run with arguments and options.\n\n```php\n\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n\t\treturn [\n            // equivalent to: php /path/to/artisan command:name /path/to/file\n            $scheduler-\u003eargs(['/path/to/file'])\n                -\u003eeveryWeekday()\n                -\u003ehours(5),\n\n            // equivalent to: php /path/to/artisan command:name /path/to/file --force --toDelete=\"expired\" --exclude=\"admins\" --exclude=\"developers\"\n            $scheduler-\u003eargs(['/path/to/file'])\n                -\u003eopts([\n                    'force',\n                    'toDelete' =\u003e 'expired',\n                    'exclude' =\u003e [\n                        'admins',\n                        'developers'\n                    ]\n                ])\n                -\u003edaysOfTheMonth([1, 15])\n                -\u003ehours(2)\n        ];\n\t}\n```\n\n\u003e NOTE: Both `args()` and `opts()`, whichever is called first, will internally create a new `Schedulable` instance for you so you don't need to `App::make()`.\n\n\u003ca name=\"drivers\" /\u003e\n## Drivers\n\nDrivers provide the ability to add additional context to your scheduling.  [Building custom drivers](#custom-drivers) is a great way to customize this context to your application's needs.\n\n\u003ca name=\"datetime\" /\u003e\n### DateTime (Default)\n\nExamples of how to schedule:\n\n```php\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n        //every day at 4:17am\n        return $scheduler-\u003edaily()-\u003ehours(4)-\u003eminutes(17);\n    }\n```\n\n\n```php\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n        //every Tuesday/Thursday at 5:03am\n        return $scheduler-\u003edaysOfTheWeek([\n                Scheduler::TUESDAY,\n                Scheduler::THURSDAY\n            ])-\u003ehours(5)-\u003eminutes(3);\n    }\n```\n\n```php\n\tpublic function schedule(Schedulable $scheduler)\n\t{\n        //the second and third Tuesday of every month at 12am\n        return $scheduler-\u003emonthly()-\u003eweek([2, 3])-\u003edaysOfTheWeek(Day::TUESDAY);\n    }\n```\n\n\u003ca name=\"custom-drivers\" /\u003e\n## Custom Drivers\n\nCustom drivers allow you to provide application context within scheduling.  For example, an education-based application may contain scheduling methods like `inServiceDays()`, `springBreak()` and `christmasBreak()` where commands are run or don't run during those times.\n\nCreate a packagepath such as `\\MyApp\\ScheduleDriver\\` and create two classes:\n\n * `Scheduler` that `implements Indatus\\Dispatcher\\Scheduling\\Schedulable`.  This class should provide a useful interface for programmers to schedule their commands.\n * `ScheduleService` that `extends \\Indatus\\Dispatcher\\Services\\ScheduleService`.  This class contains logic on how to determine if a command is due to run.\n\nPublish the configs using `php artisan config:publish indatus/dispatcher`. Then update your driver configuration to reference the package in which these 2 classes are included (do not include a trailing slash):\n\n```\n    'driver' =\u003e '\\MyApp\\ScheduleDriver'\n```\n\n\u003ca name=\"faq\" /\u003e\n## FAQ\n\n**I need to deploy to multiple servers representing a single environment.  How can I be sure my command is only run by a single server and not run on each server?**\n\nSchedule `scheduled:run` to run every minute with [rcron](https://code.google.com/p/rcron/):\n\n```php\n* * * * * /usr/bin/rcron php /path/to/artisan scheduled:run 1\u003e\u003e /dev/null 2\u003e\u00261\n```\n\n**Why are my commands not running when I've scheduled them correctly?  I'm also not seeing any error output**\n\n1) Verify that mcrypt is installed and working correctly via the command `php -i | mcrypt`.\n\n2) Utilizing `php artisan scheduled:run --debug` will tell you why they're not running.  If you do not see your command listed here then it is not set up correctly.\n\nExample:\n\n```\n$ php artisan scheduled:run --debug                                                                                        \nRunning commands...\n     backup:avatars: No schedules were due\n     command:name: No schedules were due\n     myTestCommand:name: No schedules were due\n     cache:clean: /usr/bin/env php /Users/myUser/myApp/artisan cache:clean \u003e /dev/null \u0026\n     mail:subscribers: /usr/bin/env php /Users/myUser/myApp/artisan mail:subscribers \u003e /dev/null \u0026\n```\n\n**I have commands that extend `ScheduledCommand` but why don't they appear in when I run `scheduled:summary`?**\n\nCommands that are disabled will not appear here.  Check and be sure `isEnabled()` returns true on those commands.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIndatus%2Fdispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FIndatus%2Fdispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIndatus%2Fdispatcher/lists"}