0
点赞
收藏
分享

微信扫一扫

Laravel5.1定时任务


分如下步骤:

1.创建任务计划的主文件:

:console AllConsole

也可以跟​​--command testconsole​​​,这个就是命令的标识,加了的话待会儿在运行​​php artisan list​​​的时候就可以看到这个命令,如果不加,看不到,但是照样可以执行。
执行以上命令之后,会生成如下文件,我的开发环境是win10,执行计划任务最后是在Linux上执行,Windows下面只要测试通过了就可以传到Linux下。

D:\phpStudy\WWW\BCCAdminV1.0\app\Console\Commands\AllConsole.php

2.进到这个文件,修改一下代码:

<?php

namespace App\Console\Commands;

use App\Models\MyLesson;
use App\Models\Order;
use App\Models\Resource;
use Illuminate\Console\Command;
use Log;

class AllConsole extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'testconsole';

/**
* The console command description.
*
* @var string
*/
protected $description = '这是一个测试Laravel定时任务的描述';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
Log::info('这是我写的log');
}
}

这个里面就三个参数,
​​​signature​​​:信号的名字;
​​​description​​​:任务描述;
​​​handle()​​​:方法就是要执行的代码了,里面和写一般的controller方法一样,该引用的还是要引用;
我这里是写的一个定时写日志的测试;

3.写定时器:

进到D:\phpStudy\WWW\BCCAdminV1.0\app\Console\Kernel.php修改如下代码:

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
// Commands\Inspire::class,
Commands\AllConsole::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule) {
// $schedule->command('inspire')
// ->hourly();
$schedule->command('testconsole')->everyMinute();
}
}

​commands​​​:里面需要把要用的定时任务的class都引进来;
​​​schedule​​​:里面就可以定义执行的命令和执行的周期。
其他可用周期可以查看如下文件:

D:\phpStudy\WWW\BCCAdminV1.0\vendor\laravel\framework\src\Illuminate\Console\Scheduling\Event.php

方法很多,这里就不重复了,进去一看就知道怎么用.

4.下面我们就在windows下面测试一下能否正常执行:

php artisan testconsole

Laravel5.1定时任务_任务计划


我在测试的方法里面没有进行输出,所以执行完命令之后没反应,但是我们直接去日志文件,可以看到我们的log信息已经写进去了,这表明任务计划代码没有问题,windows下面只能测试,真正执行任务计划要到linux下面去,windows下面也可以执行任务计划,但是比较繁琐,这里仅仅测试代码是否按照我们预想的执行.

执行结果如下:

Laravel5.1定时任务_任务计划_02

5.把代码推到linux下,编辑定时任务:

sudo crontab -e

加入下面代码,就大功告成了:

* * * * * /usr/bin/php /xxx/production/xxx/artisan schedule:run >> /dev/null 2>&1

下面这个可以静默执行,只输出你程序里面的结果,并把这些记录输出到一个指定的文件,这样的话你就可以看到哪一次没有被成功执行。

* * * * * /usr/bin/php /xxx/production/xxx/artisan schedule:run -q >>/xxx/production/xxx/storage/logs/console.log  2>&1

上面的意思是用php去执行artisan里面的schedule:run这个命令,周期是每分钟都执行.

然后我们再去Laravel.log去看一下效果,如下:

Laravel5.1定时任务_php_03

出现这个说明任务计划已经正常执行了,每分钟被执行了一次.
到这里,Laravel的任务计划就是这样的了.

但是还有一点疑问:我是不是需要每见一个计划任务就建个专门的命令和专门的文件?可不可以在一个文件里面写所有的任务计划?

经过查找,发现解决方案了!以下的用法我在国内没有找到案例.
运行命令并把结果发送邮件:

$schedule->command('cache:clear')
->hourly()
->sendOutputTo($filePath)
->emailOutputTo('john@doe.com');

直接调用类里面的方法:

$schedule->call('SomeClass@method')->dailyAt('10:00');

闭包调用:

$schedule->call(function(){
//..
})->everyThirtyMinutes();

命令行:

$schedule->terminal('gulp task')->fridays()->when(function(){ 
return true;
});

支持的时间:

->hourly()
->daily()
->at($time) // 24 hour time
->dailyAt($time)
->twiceDaily()
->weekdays()
->mondays()
->tuesdays()
->wednesdays()
->thursdays()
->fridays()
->saturdays()
->sundays()
->weekly()
->weeklyOn($day, $time)
->monthly()
->yearly()
->everyFiveMinutes()
->everyTenMinutes()
->everyThirtyMinutes()
->days() // Days of the week.

参考:
​​​https://laravel-news.com/laravel-5-scheduler​​​ 更多:
​​http://laravelacademy.org/post/235.html​​


举报

相关推荐

0 条评论