我已经核对了关于这个问题的多个答案,但似乎没有一个正确的解决办法。我正在启动一个作业,它在sync模式下工作得很好,但在database驱动程序中却完全不工作。
queue.php
'default' => env('QUEUE_CONNECTION', 'sync'),.env
QUEUE_DRIVER=database我是如何解雇这份工作的。我试着删除onConnection('database'),但是它会在sync驱动程序上运行
SendNotifications::dispatch(array("message" => "test"))->onConnection('database');我正在执行以下命令来侦听
php artisan queue:work database当我启动作业时,它会被写入jobs表中,几秒钟后,它就会得到processed。问题是:这不是开火。如果我删除onConnection('database'),它可以在sync模式下工作,没有任何问题。
class SendNotifications implements ShouldQueue {
public function __construct(){
}
public function handle(){
// here i'm connecting to a notifications api
}
}handle()内部的示例
if($this->event_name === "example"){
$room = $this->getRoomByID($example_id_variable); // database_call
if(empty($room) === true) {
return;
}
$notify_receivers = getBlabla(); //get the receivers from the database (complex query)
if(empty($notify_receivers) === false){
foreach($notify_receivers as $receiver){
$this->sendMessageAPI($receiver, $this->payload);
}
}
}sentMessageAPI
public function sendMessageAPI($id, $payload) {
$curl = curl_init();
$post_fields = array(
"key" => "key",
"secret" => "secret",
"channelId" => $id,
"message" => $payload
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.piesocket.com/api/publish",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post_fields),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
}我正在使用xampp在本地主机上运行这个程序。知道它为什么不是真的开火吗?我已经检查了数据库表jobs中的jobs列,它是正确的。
发布于 2021-03-19 00:02:23
首先,您需要在您的QUEUE_CONNECTION文件中使用QUEUE_DRIVER而不是QUEUE_DRIVER。这将消除对onConnection('database')的需求,并使数据库成为默认的。如果要强制单个作业运行同步,则可以使用dispatch_now。
你能扩展一下“不开火”吗?你说过它在几秒钟后就会被处理,那么任务是根本没有被捡起来,还是只是没有正常工作呢?作业是否进入failed_jobs表?
另外要注意的是,如果您在示例中更改了作业类,那么您必须重新启动队列工作器。工作人员不一定要重读类,而且可能使用的是旧类或错误类。对于本地测试,我建议使用queue:listen或探索--once参数。https://laravel.com/docs/8.x/queues#the-queue-work-command
https://stackoverflow.com/questions/66698719
复制相似问题