indatus/dispatcher
Composer 安装命令:
composer require indatus/dispatcher
包简介
Schedule your artisan commands within your application's source code
README 文档
README
Dispatcher allows you to schedule your artisan commands within your Laravel 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.
use Indatus\Dispatcher\Scheduling\ScheduledCommand; use Indatus\Dispatcher\Scheduling\Schedulable; use Indatus\Dispatcher\Drivers\DateTime\Scheduler; class MyCommand extends ScheduledCommand { public function schedule(Schedulable $scheduler) { //every day at 4:17am return $scheduler ->daily() ->hours(4) ->minutes(17); } }
README Contents
- Features
- Tutorial
- Installation
- For Laravel 4 (see 1.4 branch)
- For Laravel 5 - discontinued, see Laravel 5's scheduler
- Upgrading from 1.4 to 2.0
- Usage
- Drivers
- Custom Drivers
- FAQ
- Schedule artisan commands to run automatically
- Scheduling is maintained within your version control system
- Single source of truth for when and where commands run
- Schedule commands to run with arguments and options
- Run commands as other users
- Run commands in certain environments
- Use custom drivers for custom scheduling contexts
By Ben Kuhl at the Laravel Louisville meetup (@lurvul): Video - Slides
By Jefferey Way at Laracasts: Recurring Tasks the Laravel Way
## InstallationNOTICE: Laravel 5 now includes scheduling out of the box. This package will no longer be maintained for Laravel 5 and above
| Requirements | 1.4.* | 2.* |
|---|---|---|
| Laravel | 4.1/4.2 | 5.x |
| PHP | 5.3+ | 5.4+ |
| HHVM | 3.3+ | 3.3+ |
| Install with Composer... | ~1.4 | ~2.0@dev |
If you're using Laravel 4 view the readme in the 1.4 branch
Add this line to the providers array in your config/app.php file :
'Indatus\Dispatcher\ServiceProvider',
Add the following to your root Crontab (via sudo crontab -e):
* * * * * php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
If you are adding this to /etc/cron.d you'll need to specify a user immediately after * * * * *.
### Upgrading from 1.4 to 2.0You may add this to any user's Crontab, but only the root crontab can run commands as other users.
In all scheduled commands...
- Replace
use Indatus\Dispatcher\Drivers\Cron\Schedulerwithuse Indatus\Dispatcher\Drivers\DateTime\Scheduler - Replaced uses of
Scheduler::[DAY_OF_WEEK]withDay::[DAY_OF_WEEK]andScheduler::[MONTH_OF_YEAR]withMonth::[MONTH_OF_YEAR] executableconfig option has been removed. Dispatcher now inherits the path to the binary that was initially used to runscheduled:run
scheduled
scheduled:make Create a new scheduled artisan command
scheduled:run Run scheduled commands
scheduled:summary View a summary of all scheduled artisan commands
If commands are not visible via php artisan then they cannot be scheduled.
Use php artisan scheduled:make to generate a new scheduled command, the same way you would use artisan's command:make. Then register your command with Laravel.
You may either implement \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface or follow the below steps.
- Add use statements to your command. If you're using a custom driver you will use a different
Schedulerclass.
use Indatus\Dispatcher\Scheduling\ScheduledCommand; use Indatus\Dispatcher\Scheduling\Schedulable; use Indatus\Dispatcher\Drivers\DateTime\Scheduler;
- Extend
\Indatus\Dispatcher\Scheduling\ScheduledCommand - Implement schedule():
/** * When a command should run * * @param Scheduler $scheduler * * @return Scheduler */ public function schedule(Schedulable $scheduler) { return $scheduler; }
For details and examples on how to schedule, see the DateTime Driver.
### Running Commands As UsersYou may override user() to run a given artisan command as a specific user. Ensure your scheduled:run artisan command is running as root.
public function user() { return 'backup'; }
### Environment-Specific CommandsThis feature may not be supported by all drivers.
You may override environment() to ensure your command is only scheduled in specific environments. It should provide a single environment or an array of environments.
public function environment() { return ['development','staging']; }### Maintenance Mode
By 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.
You may override runInMaintenanceMode() to force your command to still be run while the application is in maintenance mode.
public function runInMaintenanceMode() { return true; }### Advanced scheduling
You may schedule a given command to to run at multiple times by schedule() returning multiple Schedulable instances.
public function schedule(Schedulable $scheduler) { return [ // 5am Mon-Fri $scheduler->everyWeekday()->hours(5), // 2am every Saturday App::make(get_class($scheduler)) ->daysOfTheWeek(Scheduler::SATURDAY) ->hours(2) ]; }
You may also schedule a command to run with arguments and options.
public function schedule(Schedulable $scheduler) { return [ // equivalent to: php /path/to/artisan command:name /path/to/file $scheduler->args(['/path/to/file']) ->everyWeekday() ->hours(5), // equivalent to: php /path/to/artisan command:name /path/to/file --force --toDelete="expired" --exclude="admins" --exclude="developers" $scheduler->args(['/path/to/file']) ->opts([ 'force', 'toDelete' => 'expired', 'exclude' => [ 'admins', 'developers' ] ]) ->daysOfTheMonth([1, 15]) ->hours(2) ]; }
## DriversNOTE: Both
args()andopts(), whichever is called first, will internally create a newSchedulableinstance for you so you don't need toApp::make().
Drivers provide the ability to add additional context to your scheduling. Building custom drivers is a great way to customize this context to your application's needs.
### DateTime (Default)Examples of how to schedule:
public function schedule(Schedulable $scheduler) { //every day at 4:17am return $scheduler->daily()->hours(4)->minutes(17); }
public function schedule(Schedulable $scheduler) { //every Tuesday/Thursday at 5:03am return $scheduler->daysOfTheWeek([ Scheduler::TUESDAY, Scheduler::THURSDAY ])->hours(5)->minutes(3); }
public function schedule(Schedulable $scheduler) { //the second and third Tuesday of every month at 12am return $scheduler->monthly()->week([2, 3])->daysOfTheWeek(Day::TUESDAY); }## Custom Drivers
Custom 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.
Create a packagepath such as \MyApp\ScheduleDriver\ and create two classes:
Schedulerthatimplements Indatus\Dispatcher\Scheduling\Schedulable. This class should provide a useful interface for programmers to schedule their commands.ScheduleServicethatextends \Indatus\Dispatcher\Services\ScheduleService. This class contains logic on how to determine if a command is due to run.
Publish 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):
'driver' => '\MyApp\ScheduleDriver'
## FAQ
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?
Schedule scheduled:run to run every minute with rcron:
* * * * * /usr/bin/rcron php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
Why are my commands not running when I've scheduled them correctly? I'm also not seeing any error output
-
Verify that mcrypt is installed and working correctly via the command
php -i | mcrypt. -
Utilizing
php artisan scheduled:run --debugwill tell you why they're not running. If you do not see your command listed here then it is not set up correctly.
Example:
$ php artisan scheduled:run --debug
Running commands...
backup:avatars: No schedules were due
command:name: No schedules were due
myTestCommand:name: No schedules were due
cache:clean: /usr/bin/env php /Users/myUser/myApp/artisan cache:clean > /dev/null &
mail:subscribers: /usr/bin/env php /Users/myUser/myApp/artisan mail:subscribers > /dev/null &
I have commands that extend ScheduledCommand but why don't they appear in when I run scheduled:summary?
Commands that are disabled will not appear here. Check and be sure isEnabled() returns true on those commands.
indatus/dispatcher 适用场景与选型建议
indatus/dispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 911.47k 次下载、GitHub Stars 达 1.05k, 最近一次更新时间为 2014 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「schedule」 「dispatcher」 「laravel」 「artisan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 indatus/dispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 indatus/dispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 indatus/dispatcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Package that provides an integration with Doctrine ORM to automatically dispatch Domain Events.
Enables the creation of cron-jobs tasks to be consumed by symfony/messenger
Event dispatcher package
A Deployment/Change Log Schedule and Notes system for SilverStripe sites
Monitoring for scheduled jobs
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
统计信息
- 总下载量: 911.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1059
- 点击次数: 28
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-07