zoukankan      html  css  js  c++  java
  • Laravel 定时任务 任务调度 可手动执行

    1、创建一个命令

    php artisan make:command TestCommand 

    执行成功后会提示:

    Console command created successfully.

    生成了一个新的命令文件

    AppConsoleCommandsTestCommand.php

    <?php
    
    namespace AppConsoleCommands;
    
    use IlluminateConsoleCommand;
    
    class TestCommand extends Command
    {
        /**
         * The name and signature of the console command.
         * 命令名称(执行时需要用到)
         * @var string
         */
        protected $signature = 'test';
    
        /**
         * The console command description.
         * 命令描述
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         * 处理业务逻辑
         * @return int
         */
        public function handle()
        {
            echo 123123;
            echo PHP_EOL;
            exit;
        }
    }

    2、配置console的Kernel

    <?php
    
    namespace AppConsole;
    
    use AppConsoleCommandsTestCommand;
    use IlluminateConsoleSchedulingSchedule;
    use IlluminateFoundationConsoleKernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            //
            TestCommand::class,
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  IlluminateConsoleSchedulingSchedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            // $schedule->command('inspire')->hourly();
            $schedule->command('test')->everyMinute();
        }
    
        /**
         * Register the commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
    
            require base_path('routes/console.php');
        }
    }

    3、执行命令

    手动执行:

    php artisan test(命令名称)

    自动执行:

    php artisan schedule:run

    定时执行:crontab添加

    php artisan schedule:run 

    ->cron('* * * * *');
    自定义 Cron 计划执行任务


    ->everyMinute();
    每分钟执行一次任务


    ->everyFiveMinutes();
    每五分钟执行一次任务


    ->everyTenMinutes();
    每十分钟执行一次任务


    ->everyFifteenMinutes();
    每十五分钟执行一次任务


    ->everyThirtyMinutes();
    每三十分钟执行一次任务


    ->hourly();
    每小时执行一次任务


    ->hourlyAt(17);
    每小时第 17 分钟执行一次任务


    ->daily();
    每天 0 点执行一次任务


    ->dailyAt('13:00');
    每天 13 点执行一次任务


    ->twiceDaily(1, 13);
    每天 1 点及 13 点各执行一次任务


    ->weekly();
    每周日 0 点执行一次任务


    ->weeklyOn(1, '8:00');
    每周一的 8 点执行一次任务


    ->monthly();
    每月第一天 0 点执行一次任务


    ->monthlyOn(4, '15:00');
    每月 4 号的 15 点 执行一次任务


    ->quarterly();
    每季度第一天 0 点执行一次任务


    ->yearly();
    每年第一天 0 点执行一次任务


    ->timezone('America/New_York');
    设置时区

      
  • 相关阅读:
    IntelliJ IDEA(十) :常用操作
    IntelliJ IDEA(九) :酷炫插件系列
    IntelliJ IDEA(八) :git的使用
    IntelliJ IDEA(七) :Project Structure
    IntelliJ IDEA(六) :Settings(下)
    IntelliJ IDEA(五) :Settings(中)
    Echo中间件使用
    Makefile
    swag-ui与swag-edit
    mysql 连接数太多
  • 原文地址:https://www.cnblogs.com/starfish29/p/13577145.html
Copyright © 2011-2022 走看看