Laravel定时任务的基本用法是什么,如何理解实现过程
Admin 2022-06-14 群英技术资讯 755 次浏览
一个网站系统往往会有很多定时任务要执行。例如推送订阅消息,统计相关数据等,Linux一般采用crontab对定时任务进行设置和管理,但是随着任务的增多,管理定时任务就比较麻烦,容易管理混乱。laravel 对此的解决方案是只设置一条定时任务,业务中所有的定时任务在这条定时任务进行处理和判断,实现了在代码层面对定时任务的管理。
首先配置crontab:
* * * * * php artisan schedule:run >> /dev/null 2>&1
上面的意思是设置定时任务每分钟执行一次,具体的业务配置,放在了App\Console\Kernel 的 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 这个指定是在vendor\illuminate\console\Scheduling\ScheduleRunCommand 类里面进行定义的,定义的形式和一般的定时任务相同:
/** * 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, [ 'Illuminate\Console\Scheduling\ScheduleRunCommand', ]); }
所以 php artisan schedule:run 命令就是框架内置的一个命令。
在命令启动的时候,会默认找类中的handle 方法进行执行:
/** vendor\illuminate\console\Command.php * Execute the console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $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 \Illuminate\Contracts\Container\Container $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 \Illuminate\Console\Scheduling\Event $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();
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
php框架的底层原理:1、PHP实现了典型的动态语言执行过程。2、底层原理分析,Zend整体是纯c实现的,是PHP的核心部分,将PHP代码翻译,实现opcode的处理。
php中echo与print区别是什么?我们知道echo与print都能够输出字符,实际应用上两者的功能也是差不多,因此,一些朋友对于echo与print区别并不是很清楚。其实,两者还是有一定的区别的,下面小编就给大家介绍一下。
在之前的文章中给大家带来了《PHP回调函数怎样使用(实例详解)》,这篇文章中详细的介绍了PHP中的回调函数的相关知识,本篇文章来看一看PHP函数中基本的匿名函数有哪些值得关注的知识点。希望能够帮助到大家!
swoole使用常量,Client::MSG_WAITALL,用于Client->recv()方法的第二个参数,阻塞等待直到收到指定长度的数据后返回。
php FastCGI模式的优缺点:1、优点,独立的进程池来运行、支持分布式运算、动态逻辑的分离。2、缺点,对所有的子目录只有一个可用的php.ini文件、环境升级都有延迟。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008