Laravel队列作业保持Web服务响应

时间:2018-09-19 14:06:04

标签: laravel queue lumen

我在lumen v5.6项目中实现了队列作业。对于队列作业,我正在使用数据库驱动程序。但是问题是,当我调度作业并将睡眠方法放在job handle()方法中15秒钟时,作业api响应将保持15秒钟,直到整个作业被解雇。但是当我在后台调度作业时,我必须立即发回api响应。

命名空间App \ Jobs;

使用App \ Helpers \ NotificationProcess;

类SendPushNotification扩展Job {

protected $queueData;
protected $activity;
protected $activity_user;
protected $post;
protected $mentions;
protected $noti;
protected $mention_activities;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($data) {
    \Log::info("Queue Start Time:: " . microtime());
    $this->queueData = $data;
    $this->activity = $data['activity'];
    $this->activity_user = $data['user'];
    $this->post = $data['post'];
    $this->mentions = $data['mentions'];
    $this->mention_activities = $data['mention_activities'];
    $this->noti = new NotificationProcess();
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle() {
    try {
        \Log::info("wait for 15 seconds");
        sleep(15);
        $receiver = \App\Models\DeviceToken::getDeviceTokensByUsers([$this->post->user_id], $this->activity_user->id, $this->activity['domain']);
        if (!$receiver->isEmpty() && !empty($this->activity))
            $this->noti->sendCommentPushNotification($this->activity, $receiver[0]->toArray(), $this->activity_user);
        if (!empty($this->mentions)) {
            $receivers = \App\Models\DeviceToken::getDeviceTokensByUsers($this->mentions, $this->activity_user->id, 'M');
            if (!$receivers->isEmpty() && !empty($this->mention_activities))
                $this->noti->sendMentionPushNotifications($this->mention_activities, $receivers->toArray(), $this->activity_user);
        }
        \Log::info("==== Notifications sent ===");
        return true;
    } catch (\Exception $ex) {
         \Log::info("Exception::".$ex->getMessage());
        return false;
    }
}

}

现在,我只想立即将响应发送回api,因为我为后台进程分派作业,它不应该包含api的响应。

注意:一项作业可以在2分钟内处理,但这不会影响api的响应

0 个答案:

没有答案