Laravel工作者没有尝试这项工作,而是删除了它 [英] Laravel worker not attempting the job but rather deleting it

查看:45
本文介绍了Laravel工作者没有尝试这项工作,而是删除了它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

laravel 5.4
php 7.1.32
主管 3.3.1
(我知道...我知道.该公司已经落后了3年)

laravel 5.4
php 7.1.32
supervisor 3.3.1
(I know... I know. The company is running 3 years behind)

config/queue.php

'database' => [
    'driver' => 'database',
    'connection' => 'queue', // means in config/database.php I have to setup a new connection
    'table' => 'jobs',
    'queue' => 'default',
    'retry_after' => 90,
],

主管配置

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/project/artisan queue:work
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/project/storage/logs/supervisord.log


SCENARIO :调度队列后,会将新行添加到 jobs 表中.然后,将 Laravel Worker 分配给一个工作,那时 reserved_at &尝试列已更新.作业完成后,该行(作业)将被删除.


SCENARIO: When a queue is dispatched, a new row gets added to the jobs table. Then a Laravel Worker is assigned to a job, that's when the reserved_at & attempts columns are updated. Once the job is finished, the row (job) gets deleted.

现在,由于某些神秘的原因,有时一项工作可以无缝进行&有时有工作,但没有分配任何工人,并且在一秒钟之内删除了该工作.刚刚发生了什么?而且,这是断断续续的,以至于很难跟踪实际尝试过哪些作业,后来又被此问题删除或仅删除了.

Now, for some mystical reasons, sometimes a job works seamlessly & sometime a job is there but no worker is assigned to it and within a split second that job is deleted. What just happened? Also, this is so intermittent that it's hard to track which jobs were actually attempted and later removed or just removed by this issue.

我已经挠头至少一个星期了,请指导我.

I've been scratching my head for at least a week now, please guide me.

修改
- php artisan queue:restart 广播,主管会根据正常运行时间进行更新,但在此问题上没有任何改善.
-后来,我删除了主管,并尝试以手动方式 php artisan queue:work ,甚至删除了该作业,而不是尝试(failed_jobs中未插入新数据)要么).

Edit
- php artisan queue:restart broadcast, the supervisor updates with the up-time but no improvement on the issue.
- Later on, I removed the supervisor and tried doing the manual way php artisan queue:work and even that removed the job instead of attempting it (No new data inserted in failed_jobs either).

编辑(2)-日志
这是发送工作

public static function sendVoiceCall($instituteSmsSettings, $announcementId, $postUrl, $mobileNumbers)
    {
        Log::debug('Job dispatched');
        dispatch(new SendVoiceCall($instituteSmsSettings, $announcementId, $postUrl, $mobileNumbers));
    }

这是工作

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class SendVoiceCall implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;
    protected $ARR_POST_DATA = array();
    protected $postUrl = '';

    public function __construct($instituteSmsSettings, $announcementId, $postUrl, $mobileNumbers)
    {
        $this->ARR_POST_DATA['username'] = $instituteSmsSettings->username;
        $this->ARR_POST_DATA['token'] = $instituteSmsSettings->token;
        $this->ARR_POST_DATA['announcement_id'] = $announcementId;
        $this->ARR_POST_DATA['plan_id'] = $instituteSmsSettings->plan_id;
        $this->ARR_POST_DATA['caller_id'] = $instituteSmsSettings->caller_id;
        $this->ARR_POST_DATA['contact_numbers'] = $mobileNumbers;
        $this->postUrl = $postUrl;
    }

    public function handle()
    {
        Log::debug('Job started');
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->postUrl);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->ARR_POST_DATA));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

        $sendCallResponse = curl_exec($curl);
        $curlResponse = json_decode($sendCallResponse, true);
        curl_close($curl);

        Log::info('Queue complete. Announcement Id: ' . ($this->ARR_POST_DATA['announcement_id'] ?? ''));
    }
}

这是日志

[2020-04-13 10:17:49] production.DEBUG: Job dispatched  
[2020-04-13 10:17:50] production.DEBUG: Job started  
[2020-04-13 10:17:50] production.INFO: Queue complete. Announcement Id: 203691 

如您所见,队列是同时开始和结束的(应该相差2-3秒.工作人员正在删除工作,而不是尝试工作.

As you can see, the queue started and finished at the same time (there should have been a 2-3 sec difference. The worker is deleting the job rather than attempting it.

推荐答案

您的作业正在发出curl请求,但不执行任何错误处理或检查该调用.该请求可能由于某种原因而失败,但是这项工作永远不会告诉您.尤其是现在,当出现无法解释的问题时,有意义的是对请求和响应进行一些基本的错误检查.

Your job is making a curl request, but does no error handling or checking of that call. The request could be failing for some reason, but this job will never tell you. Particularly now, when there is an unexplained problem, it makes sense to do some basic error checking of the request and the response.

您可以检查几件事.解决此特定问题后,可能无需执行所有这些操作,但是您可能应该至少使用其中的一个来保持此呼叫的状态.

You can check several things. Probably no need to do all these once you have solved this particular problem, but you should probably use at least one of them going forward to keep tabs on this call.

  • Use curl_error() to check if the call worked;

使用 curl_getinfo() 向您显示网络请求和响应的详细信息.这将为您提供很多信息,包括 http_response ,您可以测试它是否为 200 或您期望的任何内容;

Use curl_getinfo() to show you details of the network request and response. This will give you lots of info, including http_response, you could test that it was 200 or whatever you expect;

如果远程服务器响应某些消息-大概是因为您正在 json_decode()对其进行记录-将其记录下来.如果响应中包含某种成功/失败元素,请对其进行测试,确保它符合您的期望;

If the remote server responds with some message - presumably it is since you are json_decode()ing it - log it. If there is some kind of success/failure element to the response, test it, make sure it is what you expect;

例如(如前所述,您通常不会做所有这些事情,只选择适合您的情况):

For example (as mentioned you would not typically do all of these, just choose which suits your situation):

$curl = curl_init();
// ... your code

// Get request info as an array
$info = curl_getinfo($curl);

$sendCallResponse = curl_exec($curl);
curl_close($curl);

// Log the actual response from the remote server
Log:info('Curl response: ' . $sendCallResponse);

// Log extensive info about the request/response, good when troubleshooting
Log:info('Curl info: ' . print_r($info, true));

// Maybe check the http response code
if ($info['http_code'] !== 200) {
    // handle an error, send a notification, etc
    Log:info('Whoah, remote server responded with ' . $info['http_code']);
}

// Maybe check if curl returned an error
if ($sendCallResponse === false) {
    // handle an error, send a notification, etc
    Log:info('Oh noes, curl error! ' . curl_error($curl));
}

// Maybe test the actual response, if there is something testable in it, 
// say a "status":
if ($curlResponse['status'] !== 'OK') {
    // handle an error, send a notification, etc
    Log:info('Remote returned status ' . $curlResponse['status']);
}

这篇关于Laravel工作者没有尝试这项工作,而是删除了它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆