创建一个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