AirWork--基于Laravel开发团队协作工具--自定义命令和定时任务(六)

自定义命令和任务

Laravel artisan 支持自定义命令和设置定时任务。

自定义命令

例子 自定义数据库清理命令

运行一下命令,创建FreshTablesMonthly命令

php artisan make:command FreshTablesMonthly

运行完后,发现已经创建app/Console/Commands/FreshTablesMonthly.php文件。

修改这个文件的handle()方法,在这个方法中调用,数据库刷新命令和填充数据命令。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Tools\Helper;

class FreshTablesMonthly extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'maintain:freshTablesMonthly';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'drop tables, build tables and seed';
    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() {
        
        $runFreshTablesMonthlyBool = env( 'runFreshTablesMonthlyBool', false );
        
        if( $runFreshTablesMonthlyBool == false ){
            Helper::echoIt( __METHOD__, __LINE__, [ 'env.runFreshTablesMonthlyBool == false' ] );
            return false;
        }
        
        
        Helper::echoIt( __METHOD__, __LINE__, [ 'runFreshTablesMonthlyBool start:' ] );
        
        \Artisan::call('migrate:fresh');
        
        Helper::echoIt( __METHOD__, __LINE__, [ 'runFreshTablesMonthlyBool migrate:fresh done' ] );
        
        \Artisan::call('db:seed');
        
        Helper::echoIt( __METHOD__, __LINE__, [ 'runFreshTablesMonthlyBool db:seed done' ] );
        
        Helper::echoIt( __METHOD__, __LINE__, [ 'runFreshTablesMonthlyBool done' ] );
        
    }
    
}

另外,使用composer dump-autoload命令可以重新加载类
以上已经完成自定义命令,可以像其他Artisan一样使用命令行调用

设置定时任务

设置Crontab

$ serevice cron start
$ crontab -e
##加入以下定时任务,每分钟调用laravel总任务
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 

注册定时任务执行自定义命令

打开app/Console/Kernel.php文件,修改$commands成员变量和成员方法schedule()。

如下注册了App\Console\Commands\FreshTablesMonthly命令,并且每月1号00点1分执行一次。

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\FreshTablesMonthly',
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {
        // $schedule->command('inspire')->hourly();
        
        $schedule->command( 'maintain:freshTablesMonthly' )->cron('1 0 1 * *')->withoutOverlapping();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值