Laravel: 计划任务完成后发送松弛通知
Laravel: Send slack notification after a scheduled task finished
我需要在使用 Laravel 构建的应用程序上安排一些任务,我想在这些任务完成输出后发送一个松弛通知。
Laravel 提供了一个 "after" 钩子 (https://laravel.com/docs/5.8/scheduling#task-hooks) 所以我可以这样做:
$schedule->command('mycommand')
->daily()
->after(function () {
// How can I access the command's name and output from here?
});
我试过 $this->output
,但 $this
指向 App\Console\Kernel
,它显示 Undefined property: App\Console\Kernel::$output
。我也试过给闭包传递一个参数,但我想我需要指定一个类型,但我不知道,文档也不是很清楚。
有人知道怎么做吗?
提前致谢!
您的命令生成什么样的输出?它是命令行还是只是您要传递给 after()
?
的变量
或者,在您的命令的 handle()
方法中,您可以在执行完所有代码后调用所需的命令,您甚至可以将参数传递给命令。
您可以使用 Artisan
Artisan::call('*command_name_here*');
假设你的命令中有这个
$this->info('hello');
在您的内核中,您可以将输出发送到一个临时文件,然后读取该文件并发送它
/** @var \Illuminate\Console\Scheduling\Event $command */
$command = $schedule->command('mycommand')
->daily()
->sendOutputTo('storage/app/logs.txt');
$command->after(function () use ($command) {
\Log::debug([$command->command, $command->output]);
\Log::debug(file_get_contents($command->output));
});
你会得到
[2019-10-11 13:03:38] local.DEBUG: array (
0 => '\'/usr/bin/php7.3\' \'artisan\' command:name',
1 => 'storage/app/logs.txt',
)
[2019-10-11 13:03:38] local.DEBUG: hello
也许是时候重新开启这个提案了 https://github.com/laravel/ideas/issues/122#issuecomment-228215251
我需要在使用 Laravel 构建的应用程序上安排一些任务,我想在这些任务完成输出后发送一个松弛通知。
Laravel 提供了一个 "after" 钩子 (https://laravel.com/docs/5.8/scheduling#task-hooks) 所以我可以这样做:
$schedule->command('mycommand')
->daily()
->after(function () {
// How can I access the command's name and output from here?
});
我试过 $this->output
,但 $this
指向 App\Console\Kernel
,它显示 Undefined property: App\Console\Kernel::$output
。我也试过给闭包传递一个参数,但我想我需要指定一个类型,但我不知道,文档也不是很清楚。
有人知道怎么做吗?
提前致谢!
您的命令生成什么样的输出?它是命令行还是只是您要传递给 after()
?
或者,在您的命令的 handle()
方法中,您可以在执行完所有代码后调用所需的命令,您甚至可以将参数传递给命令。
您可以使用 Artisan
Artisan::call('*command_name_here*');
假设你的命令中有这个
$this->info('hello');
在您的内核中,您可以将输出发送到一个临时文件,然后读取该文件并发送它
/** @var \Illuminate\Console\Scheduling\Event $command */
$command = $schedule->command('mycommand')
->daily()
->sendOutputTo('storage/app/logs.txt');
$command->after(function () use ($command) {
\Log::debug([$command->command, $command->output]);
\Log::debug(file_get_contents($command->output));
});
你会得到
[2019-10-11 13:03:38] local.DEBUG: array (
0 => '\'/usr/bin/php7.3\' \'artisan\' command:name',
1 => 'storage/app/logs.txt',
)
[2019-10-11 13:03:38] local.DEBUG: hello
也许是时候重新开启这个提案了 https://github.com/laravel/ideas/issues/122#issuecomment-228215251