定时任务


新建一个定时控制器(Cron)

目录结构

/app
  |--cli
  |   |--controller
  |   |   |--Cli.php
  |   |   |--Cron.php

路径: /app/cli/controller/Cron.php

Cron.php代码如下:

<?php

namespace app\cli\controller;

class Cron extends Cli
{
    /**
     * 每天凌晨2点执行
     */
    public function day_2()
    {
        //要执行的代码
    }
    
    /**
     * 每月一号凌晨5:10执行
     */
    public function month_1_5_10()
    {
        //要执行的代码
    }
    
    public function mReport()
    {
        //要执行的代码
    }
    
    /**
     * 每5分钟调用一次
     */
    public function run()
    {
        $minute = date('i');
        $day = date('H:i');
        $week = date('N:H:i'); // N 1 ~ 7
        $month = date('j:H:i'); // j 1 ~ 31

        $cron_config = Config::load('runtime')->get('cron');
        if (array_key_exists($day, $cron_config['day'])) {
            $action = $cron_config['day'][$day];
            try {
                call_user_func([$this, $action]);
            } catch (\Exception $exception) {
                self::errorLog($exception, $action);
            }
            Log::write(['day' => $day, 'action' => $action], 'Normal', 'cron/run');
        }
        if (array_key_exists($week, $cron_config['week'])) {
            $action = $cron_config['week'][$week];
            try {
                call_user_func([$this, $action]);
            } catch (\Exception $exception) {
                self::errorLog($exception, $action);
            }
            Log::write(['week' => $week, 'action' => $action], 'Normal', 'cron/run');
        }
        if (array_key_exists($month, $cron_config['month'])) {
            $action = $cron_config['month'][$month];
            try {
                call_user_func([$this, $action]);
            } catch (\Exception $exception) {
                self::errorLog($exception, $action);
            }
            Log::write(['month' => $month, 'action' => $action], 'Normal', 'cron/run');
        }

        if (array_key_exists('10m', $cron_config['loop']) && intval($minute) % 10 == 0) {
            $action = $cron_config['loop']['10m'];
            call_user_func([$this, $action]);
        }
    }
}

配置定时任务

配置文件位置

/app/cli/config.php

配置文件解释

配置名称叫cron
选项有 day week month loop
day     配置每天几点几分执行
week    配置每周周几几点几分执行
month   配置每月几号几点几分执行

比如:
'month' => [
    '1:05:00' => 'month_1_5_00',
],

每月1号5点整执行Cron控制器的“month_1_5_00”这个方法,当然这个方法名是自定义的,比如,可以叫“mReport”(月报)
'month' => [
    '1:05:00' => 'mReport',
],

配置文件内容

<?php
return [
    'cron' => [
        'day' => [
            '02:00' => 'day_2',
            '02:10' => 'day_2_10',
            '02:40' => 'day_2_40',
            '03:00' => 'day_3',
            '04:00' => 'day_4',
        ],
        'week' => [
            '1:02:30' => 'week_1_2_30',
            '1:02:50' => 'week_1_2_50',
        ],
        'month' => [
            '1:05:00' => 'month_1_5_00',
            '1:05:05' => 'month_1_5_05',
            '1:05:10' => 'month_1_5_10',
        ],
        'loop' => [
            '10m' => 'everyTenMinutes'
        ],
    ],
];