php - Laravel 5.7 - 在一定时间后杀死工匠
问题描述
我正在开发一个laravel 5.7
应用程序。
我创建了一个应该设置我的数据库的命令:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class TestSetupCommand extends Command
{
protected $signature = 'test:data';
protected $description = 'Basic Setup for Test Data';
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Artisan::call('migrate:refresh', ['--seed' => '']);
Artisan::call('basis:cc');
Artisan::call('tick:exchange');
$this->info("DB freshly setup: DONE");
$this->info("Coin: DONE");
$this->info("Exchange Scrapped: DONE");
}
}
我的问题是每个命令都需要几分钟才能运行完。用数据填充整个数据库总共花费了我 25 分钟。
我想每个命令只运行 1 分钟,然后杀死它们。
有什么建议可以在我的 laravel 命令中实现这一点吗?
解决方案
我认为最好的方法是将这些命令提取到后台作业中。然后,此工匠命令将成为将该新作业(或多个作业)排队的代码。
为什么?通过覆盖如下值,很容易将作业配置为在 x 时间后超时:
<?php
namespace App\Jobs;
class ProcessPodcast implements ShouldQueue
{
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 120;
}
另外,为什么要刷新数据库?这似乎是一个疯狂的想法,除非这纯粹是一个分析平台(根本没有用户数据)。如果该刷新命令超时,这可能是一件坏事 - 您可能会查看作业链接,以便保证刷新命令成功,然后其他命令(现在是新作业)已设置超时。
推荐阅读
- reactjs - 在 mapStateToProps 中访问 dispatchedProps 操作
- amazon-dynamodb - 获取项目列表的状态
- c++ - 返回非静态本地对象时选择复制构造函数而不是移动构造函数
- javascript - 如何在 Storybook 6.0.0 中显示道具类型表
- javascript - 错误页面在 Sapper 导出的生产中不起作用
- visual-studio-code - 如何在移动插入符号一个字符时在vscode中保持显示建议弹出窗口?
- javascript - 使用 HTML 表单作为 GUI PowerShell 时出现 innerHTML 错误
- javascript - 如何使用 JavaScript 从 href 中找到特定链接?
- python - 在用户输入条件下查找平均值,直到用户按下没有值的输入
- javascript - 包装一个没有 jquery 的元素