0

0

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

WBOY

WBOY

发布时间:2022-05-30 11:52:23

|

5997人浏览过

|

来源于CSDN

转载

本篇文章给大家带来了关于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函数:

秘塔AI搜索
秘塔AI搜索

秘塔AI搜索,没有广告,直达结果

下载
/**
     * 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视频教程

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

PHP自制框架
PHP自制框架

共8课时 | 0.6万人学习

PHP面向对象基础课程(更新中)
PHP面向对象基础课程(更新中)

共12课时 | 0.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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