译文GitHub https://github.com/yuansir/diving-laravel-zhphp
原文连接 https://divinglaravel.com/task-scheduling/properties-of-an-eventlaravel
Every entry you add is converted into an instance of Illuminate\Console\Scheduling\Event
and stored in an $events
class property of the Scheduler, an Event object consists of the following:git
你添加的每一个记录都将转换为 Illuminate\Console\Scheduling\Event
的实例,并存储在Scheduler的 $events
类属性中,Event对象由如下内容组成:github
The command to run could be one of the following:web
命令可能像下面一种方式运行:shell
In case of a callback, the Container::call()
method is used to run the value we pass which means we can pass a callable or a string representing a method on a class:express
在回调的状况下,Container::call()
方法用于运行咱们传递的值,这意味着咱们能够传递一个能够调用或表示方法的字符串:数组
protected function schedule(Schedule $schedule) { $schedule->call(function () { DB::table('recent_users')->delete(); })->daily(); }
Or:app
protected function schedule(Schedule $schedule) { $schedule->call('MetricsRepository@cleanRecentUsers')->daily(); }
If you would like to pass a command for the operating system to run you can use exec()
:less
若是要运行操做系统的命令,可使用 exec()
:
$schedule->exec('php /home/sendmail.php --user=10 --attachInvoice')->monthly();
You can also pass the parameters as an array:
您还能够将数组做为参数:
$schedule->exec('php /home/sendmail.php', [ '--user=10', '--subject' => 'Reminder', '--attachInvoice' ])->monthly();
$schedule->command('mail:send --user=10')->monthly();
You can also pass the class name:
你也能够传一个类名
$schedule->command('App\Console\Commands\EmailCommand', ['user' => 10])->monthly();
The values you pass are converted under the hood to an actual shell command and passed to exec()
to run it on the operating system.
你传递的值将转换为实际的shell命令,并传递给 exec()
在操做系统上运行。
You may dispatch a job to queue using the Job class name or an actual object:
您可使用Job类名称或实际对象将做业分发到队列中:
$schedule->job('App\Jobs\SendOffer')->monthly(); $schedule->job(new SendOffer(10))->monthly();
Under the hood Laravel will create a callback that calls the dispatch()
helper method to dispatch your command.
Laravel会建立一个回调函数,调用 dispatch()
辅助方法来分发你的命令。
So the two actual methods of creating an event here is by calling exec()
or call()
, the first one submits an instance of Illuminate\Console\Scheduling\Event
and the latter submits Illuminate\Console\Scheduling\CallbackEvent
which has some special handling.
因此在这里建立一个事件的两个实际方法是经过调用 exec()
或 call()
,第一个提交一个 Illuminate\Console\Scheduling\Event
的实例,后者提交 Illuminate\Console\Scheduling\CallbackEvent
来作一些特殊处理。
Using the timing method of the Scheduled Event, laravel builds a CRON expression for that event under the hood, by default the expression is set to run the command every minute:
使用计划事件的计时方法,laravel会为该事件建立一个CRON表达式,默认状况下,表达式设置为每分钟运行一次命令:
* * * * * *
But when you call hourly()
for example the expression will be updated to:
但当你调用 hourly()
时表达式会更新成这样:
0 * * * * *
If you call dailyAt('13:30')
for example the expression will be updated to:
当你调用 dailyAt('13:30')
时表达式会更新成这样:
30 13 * * * *
If you call twiceDaily(5, 14)
for example the expression will be updated to:
当你调用 twiceDaily(5, 14)
时表达式会更新成这样:
0 5,14 * * * *
A very smart abstraction layer that saves you tons of research to find the right cron expression, however you can pass your own expression if you want as well:
一个很是聪明的抽象层,能够节省大量的精力来找到正确的cron表达式,可是若是你只要你想你也能够传递你本身的表达式:
$schedule->command('mail:send')->cron('0 * * * * *');
If you want the CRON expression to be evaluated with respect to a specific timezone you can do that using:
若是您但愿CRON表达式针对特定时区,则可使用如下方式进行:
->timezone('Europe/London')
Under the hood Laravel checks the timezone value you set and update the Carbon
date instance to reflect that.
Laravel检查您设置的时区值,并更新 Carbon
日期实例使其起做用。
Exactly, Laravel uses the mtdowling/cron-expression library to determine if the command is due based on the current system time (with respect to the timezone we set).
偏偏相反,Laravel使用 mtdowling/cron-expression 库来肯定命令是否基于当前系统时间(相对于咱们设置的时区)。
For example if you want the command to run daily but only between two specific dates:
例如,若是您但愿命令天天运行,但只能在两个特定日期之间运行:
->between('2017-05-27', '2017-06-26')->daily();
And if you want to prevent it from running during a specific period:
若是你想防止它在一段特定的时间内运行:
->unlessBetween('2017-05-27', '2017-06-26')->daily();
You can use the environments()
method to pass the list of environments the command is allowed to run under:
您可使用 environments()
设置传递命令容许运行的环境列表:
->environments('staging', 'production');
By default scheduled commands won't run when the application is in maintenance mode, however you can change that by using:
默认状况下,当应用程序处于维护模式时,调度的命令不会运行,可是您能够经过使用如下命令来更改:
->evenInMaintenanceMode()
You can set the Operating System user that'll run the command using:
你能够设置那个操做系统用户来执行这个命令:
->user('forge')
Under the hood Laravel will use sudo -u forge
to set the user on the operating system.
Laravel将使用 sudo -u forge
设置在操做系统上运行的用户。
You can define your own custom constraint using the when()
and skip()
methods:
您可使用 when()
和 skip()
方法定义自定义约束:
// Runs the command only when the user count is greater than 1000 ->when(function(){ return User::count() > 1000; }); // Runs the command unless the user count is greater than 1000 ->skip(function(){ return User::count() > 1000; });
Using the before()
and then()
methods you can register callbacks that'll run before or after the command finishes execution:
使用 before()
和 then()
方法能够注册在命令完成执行以前或以后运行的回调函数:
->before(function(){ Mail::to('myself@Mail.com', new CommandStarted()); }) ->then(function(){ Mail::to('myself@Mail.com', new CommandFinished()); });
You can also ping URLs or webhooks using the pingBefore()
and thenPing()
methods:
您还可使用 pingBefore()
and thenPing()
方法ping URL或webhooks:
->ping('https://my-webhook.com/start')->thenPing('https://my-webhook.com/finish')
Using these commands laravel registers a before/after callbacks under the hood and uses Guzzle to send a GET
HTTP request:
使用这些命令laravel在注册一个前/后回调,并使用Guzzle发送一个 GET
HTTP请求:
return $this->before(function () use ($url) { (new HttpClient)->get($url); });
转载请注明: 转载自Ryan是菜鸟 | LNMP技术栈笔记
若是以为本篇文章对您十分有益,何不 打赏一下
本文连接地址: 剖析Laravel计划任务--事件属性