Laravel定时任务用法及原理详解

 2377

本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了关于定时任务的用法以及原理的相关内容,根据应用场景讲解一下定时任务的相关问题,下面一起来看一下,希望对大家有帮助。


Laravel定时任务用法及原理详解


应用场景

一个网站系统往往会有很多定时任务要执行。例如推送订阅消息,统计相关数据等,Linux一般采用crontab对定时任务进行设置和管理,但是随着任务的增多,管理定时任务就比较麻烦,容易管理混乱。laravel 对此的解决方案是只设置一条定时任务,业务中所有的定时任务在这条定时任务进行处理和判断,实现了在代码层面对定时任务的管理。

基本用法

首先配置crontab:

  1. php artisan schedule:run >> /dev/null 2>&1

上面的意思是设置定时任务每分钟执行一次,具体的业务配置,放在了App\Console\Kernel 的 schedule 方法中:

  1. class Kernel extends ConsoleKernel{
  2.     Protected function schedule(Schedule $schedule)
  3.     {
  4.         //综合数据统计
  5.         $schedule->command('complex_data_log')
  6.         ->everyMinute() //每分钟执行一次(除此之外还有,每五、十、十五、三十...,不同方法设置的默认时间不同)
  7.         ->withoutOverlapping() //防止重复执行
  8.         ->onOneServer() //在单台服务器上跑
  9.         ->runInBackground() //任务后台运行
  10.         //->appendOutputTo('log_path')//日志输出,默认追加
  11.         ->sendOutputTo('log_path'); //日志输出,默认覆盖先前日志
  12.     }
  13. }


原理解析:

基本原理:

schedule:run 这个指定是在vendor\illuminate\console\Scheduling\ScheduleRunCommand 类里面进行定义的,定义的形式和一般的定时任务相同:

  1. /**
  2.  * The console command name.
  3.  *
  4.  * @var string
  5.  */
  6. protected $name = 'schedule:run';

在laravel 解析命令的时候,ScheduleRunCommand 这个类与 Kernel 类里面的 commands 数组进行了合并:

  1. /**
  2.  * Get the commands to add to the application.
  3.  *
  4.  * @return array
  5.  */
  6. protected function getCommands()
  7. {
  8.     return array_merge($this->commands, [
  9.         'Illuminate\Console\Scheduling\ScheduleRunCommand',
  10.     ]);
  11. }

所以 php artisan schedule:run 命令就是框架内置的一个命令。

在命令启动的时候,会默认找类中的handle 方法进行执行:

  1. /** vendor\illuminate\console\Command.php
  2.  * Execute the console command.
  3.  * 
  4.  * @param  \Symfony\Component\Console\Input\InputInterface  $input
  5.  * @param  \Symfony\Component\Console\Output\OutputInterface  $output
  6.  * @return mixed
  7.  */
  8. protected function execute(InputInterface $input, OutputInterface $output){
  9.     return $this->laravel->call([$this, 'handle']);
  10. }

php artisan schedule:run 指令会每分钟扫描Kernel::schedule里面注册的所有指令,并判断该指令是否已经到达执行周期,如果到达,就推入待执行队列:

  1. /**
  2.  * Schedule the event to run every minute.
  3.  * 代码每分钟执行一次
  4.  * @return $this
  5.  */
  6. public function everyMinute()
  7. {
  8.     return $this->spliceIntoPosition(1, '*');
  9. }
  10.  
  11. /**
  12.  * Splice the given value into the given position of the expression.
  13.  * 拼接定时任务表达式
  14.  * @param  int  $position
  15.  * @param  string  $value
  16.  * @return $this
  17.  */
  18. protected function spliceIntoPosition($position, $value)
  19. {
  20.     $segments = explode(' ', $this->expression);
  21.  
  22.     $segments[$position - 1] = $value;
  23.  
  24.     return $this->cron(implode(' ', $segments));
  25. }

ScheduleRunCommand::handle函数:

  1. /**
  2.  * Execute the console command.
  3.  * 
  4.  * @return void
  5.  */
  6. public function handle()
  7. {
  8.     foreach ($this->schedule->dueEvents($this->laravel) as $event) {
  9.         if (! $event->filtersPass($this->laravel)) {
  10.             continue;
  11.         }
  12.         $this->runEvent($event);
  13.     }
  14. }


避免任务重叠:

有时候单个定时任务执行时间过长,到了下一个执行时间后,上一次的执行任务还没有跑完,这个时候,我们可以采用withoutOverlapping()方法,避免任务重叠。在 withoutOverlapping方法中,给对应的任务加锁(onOneServer 方法同理):

  1. public function create(Event $event){
  2.     return $this->cache->store($this->store)->add(
  3.         $event->mutexName(), true, $event->expiresAt
  4.     );
  5. }

只有拿到对应的任务锁,才能执行任务:

  1. /**
  2.  * Run the given event.
  3.  * 运行任务
  4.  * @param  \Illuminate\Contracts\Container\Container  $container
  5.  * @return void
  6.  */
  7. public function run(Container $container)
  8. {
  9.     if ($this->withoutOverlapping &&
  10.         ! $this->mutex->create($this)) {
  11.         return;
  12.     }
  13.      
  14.     //判断是否是后台运行
  15.     $this->runInBackground
  16.                 ? $this->runCommandInBackground($container)
  17.                 : $this->runCommandInForeground($container);
  18. }


任务后台运行:

由于定时任务是依次执行的,上一个任务执行时间过长,会影响下一个任务的执行时间,所以我们可以采用runInBackground方法,将任务放到后台执行,有点类似于shell 中 & 的作用:

  1. /**
  2.  * Build the command for running the event in the background.
  3.  * 构建定时任务后台运行语句
  4.  * @param  \Illuminate\Console\Scheduling\Event  $event
  5.  * @return string
  6.  */
  7. protected function buildBackgroundCommand(Event $event)
  8. {
  9.     $output = ProcessUtils::escapeArgument($event->output);
  10.     $redirect = $event->shouldAppendOutput ? ' >> ' : ' > ';
  11.     $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"';
  12.     return $this->ensureCorrectUser($event,
  13.         '('.$event->command.$redirect.$output.' 2>&1 '.(windows_os() ? '&' : ';').' '.$finished.') > '
  14.         .ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'
  15.     );
  16. }


其他用法:

除了上面的方法,我们还可以用laravel 的定时任务去调用Shell 命令:

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

也可以使用闭包进行调度:

  1. $schedule->call(function () {
  2.     DB::table('recent_users')->delete();})->daily();

想了解更多使用方法的话,可以查看laravel 的文档:

https://laravelacademy.org/post/19517.html


本文网址:https://www.zztuku.com/detail-12166.html
站长图库 - Laravel定时任务用法及原理详解
申明:本文转载于《CSDN》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐