Laravel:计划任务完成后发送松弛通知

时间:2019-10-11 11:02:32

标签: laravel slack

我需要在使用Laravel构建的应用程序上安排一些任务,并且我希望在这些任务完成输出后发送松弛通知。

Laravel提供了一个“之后”挂钩(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。我也尝试过将参数传递给闭包,但是我想我需要指定一个类型,但是我不知道,文档也不是很清楚。

有人对如何执行此操作有任何想法吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

假设您的命令中有此

$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

答案 1 :(得分:0)

您的命令生成什么样的输出?是命令行还是只是您要传递给after()的变量?

或者,在命令的handle()方法中,您可以在所有代码都被执行之后调用所需的命令,甚至可以将参数传递给该命令。

您可以使用Artisan

Artisan::call('*command_name_here*');
相关问题