zoukankan      html  css  js  c++  java
  • Laravel 定时任务调度 的 Artisan 命令调度

    1、创建命令

    php artisan make:command command_name --command=artisan_command_name
    
    # Explanation:
    # command_name: 生成的文件名
    # artisan_command_name: php artisan 命令调度时的命令名称
    # 结果: 在 /app/Console/Commands/ 下生成名为 command_name.php 的文件
    
    # Example: 
    # php artisan make:command LeaderMail --command=LeaderMail
    # 生成的文件名:LeaderMail
    # 调度时的命令名称:LeaderMail
    

    2、测试刚才生成的命令是否OK

    php artisan LeaderMail
    
    # Explanation:
    # 没有返回则表示成功。
    # 因为 /app/Console/Commands/LeaderMail.php 的 handle 方法中没有写内容。写了就会有返回。
    

    3、编辑生成的文件 /app/Console/Commands/LeaderMail.php 的 handle 方法

    <?php
    
    namespace AppConsoleCommands;
    
    use IlluminateConsoleCommand;
    
    class LeaderMail extends Command
    {
        /**
         * The name and signature of the console command.
         * 用来描述命令的名字与参数
         * @var string
         */
        protected $signature = 'LeaderMail';
    
        /**
         * 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 mixed
         */
        public function handle()
        {
            // 这里是任务的具体处理
        }
    }
    
    

    4、编辑 AppConsoleKernel.php 文件,添加调度

    # 先到 /app/Console/Kernel.php 中 $commands 数组中进行注册。
    # 然后在 /app/Console/Kernel.php 的 schedule 方法中定义调度任务。
    
    <?php
    
    namespace AppConsole;
    
    use AppConsoleCommandsLeaderMail;
    use IlluminateConsoleSchedulingSchedule;
    use IlluminateFoundationConsoleKernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         * 你的应用程序提供的Artisan命令。
         * @var array
         */
        protected $commands = [
            LeaderMail::class
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  IlluminateConsoleSchedulingSchedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            // $schedule->command('inspire')
            //          ->hourly();
    
            // 每分钟执行一次获取领导信箱
            // command() 调度时的命令名称
            // everyFiveMinutes() 调度规则
            // appendOutputTo() 调度命令进行操作的返回结果记录文件
            $schedule->command('LeaderMail')->everyFiveMinutes()->appendOutputTo(base_path('storage/crontab/log.log'));
        }
    
        /**
         * Register the commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            $this->load(__DIR__ . '/Commands');
    
            require base_path('routes/console.php');
        }
    }
    
    

    5、编辑机器的定时任务 crontab

    # 复习
    # crontab -l # 查看
    # crontab -e # 编辑
    # crontab -r # 删除所有
    
    # 开始操作
    crontab -e
    # 然后添加以下语句
    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
    
    # Explanation:
    # path-to-your-php/bin/php 是你的PHP的绝对路径,通过 which php 可以得到;
    # path-to-your-project/artisan 是你项目中Laravel框架中根目录下的 artisan 的绝对路径;
    

    6、如果想单独写出来也可以

    * * * * * path-to-your-php/bin/php /path-to-your-project/artisan LeaderMail >> /dev/null 2>&1
  • 相关阅读:
    LeetCode两数之和
    Windows端口被占用解决方法
    Vue函数防抖和函数节流
    Elasticsearch入门
    BIO/NIO/AIO对比
    Java日期格式转换不用发愁
    Java 类型转换工具类(持续更新)
    c++ regex 正则类例子及其gcc4.8报错
    c/c++ 编译错误汇总
    Android recover 修改更新字符串显示
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/13395841.html
Copyright © 2011-2022 走看看