问题描述
免责声明:我使用的是一个非常简单且看似毫无意义的示例,但有关功能的问题仍然有效。
我有以下设置:
class TestController
{
// this is the controller method where the request will start
public function testQ(TestService $s)
{
$setup = ['One','Two','Three'];
foreach ($setup as $name) {
$s->testQ($name);
}
}
class TestService
{
// the service that dispatches the job
public function testQ($name)
{
TestingQueue::dispatch($name)->afterResponse();
}
}
class TestingQueue implements ShouldQueue
{
use dispatchable,InteractsWithQueue,Queueable;
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function handle(OtherService $s)
{
$s->testUpdate($this->name);
}
}
class OtherService
{
public function testUpdate($name)
{
$animal = Animal::find(1);
$oldName = $animal->name;
$animal->name = $name;
$animal->save();
Log::info("$oldName -> $name");
}
}
动物名开头为“杰克”。
我的日志显示
[2020-08-24 23:22:57] production.INFO: Jack -> One
[2020-08-24 23:22:57] production.INFO: One -> Two
[2020-08-24 23:22:57] production.INFO: Two -> Three
[2020-08-24 23:23:17] production.INFO: Three -> One
[2020-08-24 23:23:17] production.INFO: One -> Two
[2020-08-24 23:23:17] production.INFO: Two -> Three
[2020-08-24 23:23:32] production.INFO: Three -> One
[2020-08-24 23:23:32] production.INFO: One -> Two
[2020-08-24 23:23:32] production.INFO: Two -> Three
[2020-08-24 23:23:33] production.INFO: Three -> One
[2020-08-24 23:23:33] production.INFO: One -> Two
[2020-08-24 23:23:33] production.INFO: Two -> Three
如上所示,分派的工作将动物的名称从杰克更新为杰克,然后从一更新为二,然后从二更新为三,然后在其他运行中重复该循环。
我想防止这种情况。我的日志应该只显示Jack到3,然后显示3到3-我不想对单个请求多次重复运行此作业,因此名称仅应更新一次。
是否有一种方法可以将作业限制为只能运行一次?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)