laravel定时任务

创建一个Test.php测试任务

php artisan make:command Test

创建完成后会在app/Console/Commands/目录下Test.php

打开Test.php

<?php

namespace app\Console\Commands;


use app\common\facades\Setting;

use app\Jobs\DispatchesJobs;
use app\Jobs\MessageNoticeJob;
use Illuminate\Console\Command;


class Test extends Command
{

    protected $signature = 'test';//命令名称,待会调用php artisan test就会执行

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '测试';//命令描述,没什么用

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();//自构函数,也用不到
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       //主要业务逻辑在这些
       Log::info('测试任务');

       // Setting::$uniqueAccountId = \YunShop::app()->uniacid = 9;
       // $job = new MessageNoticeJob(1, [], '', '');
       // DispatchesJobs::dispatch($job,DispatchesJobs::LOW);
    }

}

注册这个任务。
在Kernel.php中完成注册。

app\console\Kernel.php
protected $commands = [
        'app\console\Commands\UpdateVersion',
        'app\console\Commands\RepairWithdraw',
        'app\console\Commands\Test',
        'app\console\Commands\WechatOpen',
        'app\console\Commands\RebuildDb',
        'app\console\Commands\MigrateHFLevelExcelData',
        'app\console\Commands\MigrateMemberDistributor',
        'app\console\Commands\UpdateInviteCode',
        WriteFrame::class,
    ];

调用任务

php artisan test

可以在日志文件中看到
说明我们已经成功调用了这个测试任务。

定时任务的调度
在kernel.php中还有一个schedule函数,这个就是用来做定时调度的。

 protected function schedule(Schedule $schedule)
    {
         $schedule->command('inspire')
                  ->hourly();
    }

点击链接加入群聊:企鹅群:827944755

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值