0
点赞
收藏
分享

微信扫一扫

Laravel - Crontab

月白色的大狒 2023-03-14 阅读 83

Laravel 任务调度,可以很方便的实现和处理系统定时任务。

调度列表

现已有的调度任务

php artisan schedule:list

生成调度

在控制台核心配置文件 /app/Console/Kernel.php 的 schedule 方法中,定义调度:

<?php
//调度一个回调函数
$schedule->call(function(){ //... })->daily();

//调度类,需要实现 __invoke 方法
$schedule->call(new __Invokeable())->daily();

//调度 artisan 命令
$schedule->command('emails:send Taylor --force')->daily();

//调度 command 类
$schedule->command(SendEmailsCommand::class, ['Taylor', '--force'])->daily();

//调度 shell 命令
$schedule->exec('node /home/forge/script.js')->daily();

//调度队列任务
$schedule->job(new Heartbeat, 'heartbeats', 'sqs')->everyFiveMinutes();

运行调度

php artisan schedule:work

调度频率

使用方法

<?php
$schedule->call(function () {
//..
})->weekly()->mondays()->at('13:00');

可选频率

->cron('* * * * *');

自定义 Cron 计划执行任务

->everyMinute();

每分钟执行一次任务

->everyTwoMinutes();

每两分钟执行一次任务

->everyThreeMinutes();

每三分钟执行一次任务

->everyFourMinutes();

每四分钟执行一次任务

->everyFiveMinutes();

每五分钟执行一次任务

->everyTenMinutes();

每十分钟执行一次任务

->everyFifteenMinutes();

每十五分钟执行一次任务

->everyThirtyMinutes();

每三十分钟执行一次任务

->hourly();

每小时执行一次任务

->hourlyAt(17);

每小时第十七分钟时执行一次任务

->everyTwoHours();

每两小时执行一次任务

->everyThreeHours();

每三小时执行一次任务

->everyFourHours();

每四小时执行一次任务

->everySixHours();

每六小时执行一次任务

->daily();

每天 00:00 执行一次任务

->dailyAt('13:00');

每天 13:00 执行一次任务

->twiceDaily(1,13);

每天 01:00 和 13:00 各执行一次任务

->weekly();

每周日 00:00 执行一次任务

->weeklyOn(1,'8:00');

每周一 08:00 执行一次任务

->monthly();

每月第一天 00:00 执行一次任务

->monthlyOn(4,'15:00');

每月第四天 15:00 执行一次任务

->twiceMonthly(1,16,'13:00');

每月第一天和第 十六天的 13:00 各执行一次任务

->lastDayOfMonth('15:00');

每月最后一天 15:00 执行一次任务

->quarterly();

每季度第一天 00:00 执行一次任务

->yearly();

每年第一天 00:00 执行一次任务

->yearlyOn(6,1,'17:00');

每年六月第一天 17:00 执行一次任务

->timezone('America/New_York');

设置时区

避免重复:如果之前调度时的任务还在执行,就不会执行

$schedule->command('emails:send')->withoutOverlapping();

过期时长

$schedule->command('emails:send')->withoutOverlapping(10);

单机模式

$schedule->command('report:generate')
->fridays()
->at('17:00')
->onOneServer();

后台运行

$schedule->command('analytics:report')
->daily()
->runInBackground();

维护模式

$schedule->command('emails:send')->evenInMaintenanceMode();

输出任务

$schedule->command('emails:send')
->daily()
// ->sendOutputTo($filePath);
// ->appendOutputTo($filePath);
// ->emailOutputTo('taylor@example.com');
// ->emailOutputOnFailure('taylor@example.com'); //失败时发送

前后钩子

$schedule->command('emails:send')
->daily()
->before(function () {
// 任务即将执行。。。
})
->after(function () {
// 任务已经执行。。。
});

前后Ping

$schedule->command('emails:send')
->daily()
->pingBefore($url)
->thenPing($url);

$schedule->command('emails:send')
->daily()
->pingOnSuccess($successUrl)
->pingOnFailure($failureUrl);

$schedule->command('emails:send')
->daily()
->pingBeforeIf($condition, $url)
->thenPingIf($condition, $url);

成败钩子

$schedule->command('emails:send')
->daily()
->onSuccess(function () {
// 任务执行成功。。。
})
->onFailure(function () {
// 任务执行失败。。。
});

举报

相关推荐

0 条评论