mengguo 8个月前

修改理由:

补齐翻译缺失

相关信息:


此投稿由 Trace92 8个月前 合并。

标题修改:

+ 任务调度

内容修改:

红色背景 为原始内容

绿色背景 为新增或者修改的内容

OldNewDifferences
1  
21# 任务调度
32
43- [简介](#introduction)
 
4039       DB::table('recent_users')->delete();
4140   })->daily();
4241
43 
44 
 42
 43
4544除了使用闭包调度外,还可以调度 [invokable 对象](https://secure.php.net/manual/en/language.oop5.magic.php#object.invoke)。invokable 对象指的是包含 `__invoke` 方法的简单 PHP 类:
4645
4746   Schedule::call(new DeleteRecentUsers)->daily();
 
8382       DB::table('recent_users')->delete();
8483   })->purpose('Delete recent users')->daily();
8584
86 
87 
 85
 86
8887如果你需要向闭包命令传递参数,可以将它们提提供给 `schedule` 方法:
8988
9089   Artisan::command('emails:send {user} {--force}', function ($user) {
 
164163`->yearlyOn(6, 1, '17:00');` | 每年 6 月 1 日 17:00 运行一次任务
165164`->timezone('America/New_York');` | 设置任务时区
166165
167 
168 
 166
 167
169168这些方法可以与其他约束结合使用,创建更加精细的调度,只在一周的某些特定日子运行。例如,你可以调度命令在每周一运行一次:
170169
171170   use Illuminate\Support\Facades\Schedule;
 
212211                   ->hourly()
213212                   ->days([0, 3]);
214213
215 
216 
 214
 215
217216不仅如此,你还可以使用 `Illuminate\Console\Scheduling\Schedule` 类中的常量来设置任务在指定日期运行:
218217
219218   use Illuminate\Support\Facades;
 
264263               ->daily()
265264               ->environments(['staging', 'production']);
266265
267 
268 
 266
 267
269268<a name="timezones"></a>
270269### 时区
271270
 
301300
302301   Schedule::command('emails:send')->withoutOverlapping(10);
303302
304 
305 
 303
 304
306305在幕后,withoutOverlapping 方法使用应用程序的 [cache](/docs/laravel/11.x/cache) 来获取锁。如果必要,你可以使用 `schedule:clear-cache` Artisan 命令清除这些缓存锁。通常只有在服务器出现意外问题导致任务卡住时才需要这样做。
307306
308307<a name="running-tasks-on-one-server"></a>
 
339338           ->onOneServer();
340339```
341340
342 
343 
 341
 342
344343同样,如果打算在一台服务器上运行计划闭包,则必须为其分配一个名称:
345344
346345```php
 
377376
378377现在,我们已经学会了如何定义计划任务,接下来让我们讨论如何真正在服务器上运行它们。`schedule:run` Artisan 命令将评估你的所有计划任务,并根据服务器的当前时间决定它们是否运行。
379378
380 
381 
 379
 380
382381因此,当使用 Laravel 的调度器时,我们只需要在服务器上添加一个单一的 cron 配置项,每分钟运行一次 `schedule:run` 命令。如果你不知道如何向服务器添加 cron 条目,请考虑使用诸如 [Laravel Forge](https://forge.laravel.com) 之类的服务来为你管理 cron 配置项:
383382
384383```shell
 
411410
412411由于在定义了小于一分钟的任务时,`schedule:run` 命令会在调用的整分钟内运行,因此有时可能需要在部署应用程序时中断该命令。否则,已在运行的 `schedule:run` 命令实例将继续使用你的应用程序先前部署的代码,直到当前分钟结束。
413412
414 
415 
 413
 414
416415为了中断正在进行的 `schedule:run` 调用,你可添加 `schedule:interrupt` 命令到应用程序的部署脚本中。该命令应在你的应用程序部署完成后调用:
417416
418417```shell
 
461460> [!WARNING]
462461> `emailOutputTo` , `emailOutputOnFailure` , `sendOutputTo` 和 `appendOutputTo` 方法是 `command` 和 `exec` 方法所独有的。
463462
464 
465 
 463
 464
466465<a name="task-hooks"></a>
467466## 任务钩子
468467
 
520519            ->pingBeforeIf($condition, $url)
521520            ->thenPingIf($condition, $url);
522521
523 
524 
 522
 523
525524`pingOnSuccess` 和 `pingOnFailure` 方法可用于仅在任务成功或失败时 ping 给定的网址。失败表示预定的 Artisan 或系统命令以非零退出代码终止:
526525
527526   Schedule::command('emails:send')
 
530529            ->pingOnFailure($failureUrl);
531530
532531<a name="events"></a>
533 ## Events
 532## 事件
534533
535534Laravel 在调度过程中发送各种[事件](/docs/laravel/11.x/events)。你可以为以下事件[定义监听器](/docs/laravel/11.x/events):
536535
 
541540`Illuminate\Console\Events\ScheduledBackgroundTaskFinished` |
542541`Illuminate\Console\Events\ScheduledTaskSkipped` |
543542`Illuminate\Console\Events\ScheduledTaskFailed` |
 543  
544544
545