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

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

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

【相关推荐:laravel

应用场景

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

基本用法

首先配置crontab:

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

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

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

原理解析:

基本原理:
schedule:run 这个指定是在vendorilluminateconsoleSchedulingScheduleRunCommand 类里面进行定义的,定义的形式和一般的定时任务相同:

/**  * The console command name.  *  * @var string  */protected $name = 'schedule:run';

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

	/**      * Get the commands to add to the application.      *      * @return array      */     protected function getCommands()     {         return array_merge($this->commands, [             'IlluminateConsoleSchedulingScheduleRunCommand',         ]);     }

所以 php artisan schedule:run 命令就是框架内置的一个命令。
在命令启动的时候,会默认找类中的handle 方法进行执行:

/** vendorilluminateconsoleCommand.php  * Execute the console command.  *   * @param  SymfonyComponentConsoleInputInputInterface  $input  * @param  SymfonyComponentConsoleOutputOutputInterface  $output  * @return mixed  */protected function execute(InputInterface $input, OutputInterface $output){     return $this->laravel->call([$this, 'handle']);}

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

    /**      * Schedule the event to run every minute.      * 代码每分钟执行一次      * @return $this      */     public function everyMinute()     {         return $this->spliceIntoPosition(1, '*');     }          /**      * Splice the given value into the given position of the expression.      * 拼接定时任务表达式      * @param  int  $position      * @param  string  $value      * @return $this      */     protected function spliceIntoPosition($position, $value)     {         $segments = explode(' ', $this->expression);          $segments[$position - 1] = $value;          return $this->cron(implode(' ', $segments));     }

ScheduleRunCommand::handle函数:

/**      * Execute the console command.      *       * @return void      */     public function handle()     {         foreach ($this->schedule->dueEvents($this->laravel) as $event) {             if (! $event->filtersPass($this->laravel)) {                 continue;             }             $this->runEvent($event);         }     }

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

public function create(Event $event){     return $this->cache->store($this->store)->add(         $event->mutexName(), true, $event->expiresAt     );}

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

/**      * Run the given event.      * 运行任务      * @param  IlluminateContractsContainerContainer  $container      * @return void      */     public function run(Container $container)     {         if ($this->withoutOverlapping &&             ! $this->mutex->create($this)) {             return;         }                  //判断是否是后台运行         $this->runInBackground                     ? $this->runCommandInBackground($container)                     : $this->runCommandInForeground($container);     }

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

/**      * Build the command for running the event in the background.      * 构建定时任务后台运行语句      * @param  IlluminateConsoleSchedulingEvent  $event      * @return string      */     protected function buildBackgroundCommand(Event $event)     {         $output = ProcessUtils::escapeArgument($event->output);          $redirect = $event->shouldAppendOutput ? ' >> ' : ' > ';          $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"';          return $this->ensureCorrectUser($event,             '('.$event->command.$redirect.$output.' 2>&1 '.(windows_os() ? '&' : ';').' '.$finished.') > '             .ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'         );     }

其他用法:

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

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

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

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

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

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

【相关推荐:laravel

以上就是

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享