laravel

laravel之任务调度

2018-05-18  本文已影响99人  空气KQ

之前执行任务定时,都会用linux的crontab,现在框架用一条就可以帮你去执行了

增加定时

crontab -e
* * * * * php /www/laravel52/artisan schedule:run >>  /dev/null 2>&1

定义调度

场景:订单未支付15分钟的关闭支付状态,支付成功的,发送消息通知等

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use DB;
use Log;
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *这里将你的artisan命令的类引入下面的数组里面
     * @var array
     */
    protected $commands = [
        // Commands\Inspire::class,
    ];

    /**
     * Define the application's command schedule.
     *调度任务编写函数
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
      //定义每分钟写入用户表
       $schedule->call(function () {
          DB::table('users')->insert(['name'=>'kongqi'.str_random(),'email'=>str_random()]);
        })->everyMinute();
    }
}

执行artisan命令

前面将命令类引入到$commands这个数组里面

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

发送命令到操作系统

$schedule->exec('node /home/forge/script.js')->daily();

定时器

方法 描述
->cron('* * * * *'); 在自定义Cron调度上运行任务
->everyMinute(); 每分钟运行一次任务
->everyFiveMinutes(); 每五分钟运行一次任务
->everyTenMinutes(); 每十分钟运行一次任务
->everyThirtyMinutes(); 每三十分钟运行一次任务
->hourly(); 每小时运行一次任务
->daily(); 每天凌晨零点运行任务
->dailyAt('13:00'); 每天13:00运行任务
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务
->weekly(); 每周运行一次任务
->monthly(); 每月运行一次任务
->quarterly(); 每个季度运行一次
->yearly(); 每年运行一次
$schedule->call(function () {
    // 每周星期一13:00运行一次...
})->weekly()->mondays()->at('13:00');
方法 描述
->weekdays(); 只在工作日运行任务
->sundays(); 每个星期天运行任务
->mondays(); 每个星期一运行任务
->tuesdays(); 每个星期二运行任务
->wednesdays(); 每个星期三运行任务
->thursdays(); 每个星期四运行任务
->fridays(); 每个星期五运行任务
->saturdays(); 每个星期六运行任务
->when(Closure); 基于特定测试运行任务

额外的调度约束列表:

方法 描述
->weekdays(); 只在工作日运行任务
->sundays(); 每个星期天运行任务
->mondays(); 每个星期一运行任务
->tuesdays(); 每个星期二运行任务
->wednesdays(); 每个星期三运行任务
->thursdays(); 每个星期四运行任务
->fridays(); 每个星期五运行任务
->saturdays(); 每个星期六运行任务
->when(Closure); 基于特定测试运行任务

根据条件

//如果给定闭包返回true,只要没有其它约束条件阻止任务运行,该任务就会执行
$schedule->command('emails:send')->daily()->when(function () {
    return true;
});

reject方法和when相反

避免任务重叠

默认情况下,即使前一个任务仍然在运行调度任务也会运行
现在使用withoutOverlapping可以让前面执行完,后面才执行

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

任务输出到文件

之对command命令生效,call不生效

$schedule->command('emails:send')
         ->daily()
         ->sendOutputTo($filePath);

追加输出到给定文件

$schedule->command('emails:send')
         ->daily()
         ->appendOutputTo($filePath);

发送到电子邮件

$schedule->command('foo')
         ->daily()
         ->sendOutputTo($filePath)
         ->emailOutputTo('foo@example.com');

任务钩子

使用before和after方法,你可以指定在调度任务完成之前和之后要执行的代码

$schedule->command('emails:send')
         ->daily()
         ->before(function () {
             // Task is about to start...
         })
         ->after(function () {
             // Task is complete...
         });

ping URL
使用pingBefore和thenPing方法,调度器可以在任务完成之前和之后自动ping给定的URL

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

猜你喜欢

热点阅读