登录  /  注册
首页 > php框架 > Laravel > 正文

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

WBOY
发布: 2022-05-30 11:52:23
转载
5312人浏览过

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

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

【相关推荐:laravel视频教程

应用场景

一个网站系统往往会有很多定时任务要执行。例如推送订阅消息,统计相关数据等,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();
登录后复制

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

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

【相关推荐:laravel视频教程

以上就是laravel定时任务用法及原理详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:CSDN网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号