Laravel作业失败后是否可以获取最后的failed_jobs记录ID [英] Is it possible to get last failed_jobs record id after Laravel job failed

查看:30
本文介绍了Laravel作业失败后是否可以获取最后的failed_jobs记录ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为failed_jobs创建GUI并将它们与其他表记录相关联.这样用户就可以知道在作业处理期间哪个作业属性值失败并可以重试.

I want to create GUI for failed_jobs and associate them with other tables record. So user could know which jobs property value failed during job handle and could be retried.

Laravel Job具有功能 failed(\ Exception $ exception),但在异常之后但记录保存在failed_jobs表中之前被调用.

Laravel Job has a function failed(\Exception $exception) but it is called after the exception but before the record is saved in the failed_jobs table.

Laravel也有 Queue:failing(FailedJob $ job)事件,但是我只有序列化的工作,而没有failed_jobs.

Also Laravel has Queue:failing(FailedJob $job) event but there I have only serialized job but not failed_jobs.

有人遇到类似的问题吗?与处理的作业和失败的作业有关系吗?

Did anyone run into similar problem? Is there any relation with processed job and failed one?

推荐答案

大惊小怪之后,我通过将相关模型嵌入存储在数据库中的Exception来实现此目的.您可以轻松地执行类似操作,但仅将ID存储在异常中,并在以后使用它查找模型.由你决定...

After much fussing, I accomplished this by embedding the related model within the Exception that is stored to the database. You could easily do similar but store only the id within the exception and use it to look up the model later. Up to you...

无论何时发生使作业失败的异常:

Wherever the exception that fails the job occurs:

try {
    doSomethingThatFails();
} catch (\Exception $e) {
    throw new MyException(OtherModel::find($id), 'Some string error message.');
}

app/Exceptions/MyException.php:

app/Exceptions/MyException.php:

<?php

namespace App\Exceptions;

use App\Models\OtherModel;
use Exception;

class MyException extends Exception
{
    /**
     * @var OtherModel
     */
    private $otherModel = null;

    /**
     * Construct
     */
    public function __construct(OtherModel $otherModel, string $message)
    {
        $this->otherModel = $otherModel;

        parent::__construct(json_encode((object)[
            'message' => $message,
            'other_model' => $otherModel,
        ]));
    }

    /**
     * Get OtherModel
     */
    public function getOtherModel(): ?object
    {
        return $this->otherModel;
    }
}

这会将包含对象的字符串存储到 failed_jobs 表的 exception 列中.然后,您只需要稍后对其进行解码...

That will store a string containing an object to the exception column on the failed_jobs table. Then you just need to decode that later on...

app/Models/FailedJob(或仅是app/FailedJob):

app/Models/FailedJob (or just app/FailedJob):

    /**
     * Return the human-readable message
     */
    protected function getMessageAttribute(): string
    {
        if (!$payload = $this->getPayload()) {
            return 'Unexpected error.';
        }

        $data = $this->decodeMyExceptionData();

        return $data->message ?? '';
    }

    /**
     * Return the related record
     */
    protected function getRelatedRecordAttribute(): ?object
    {
        if (!$payload = $this->getPayload()) {
            return null;
        }

        $data = $this->decodeMyExceptionData();

        return $data->other_model ?? null;
    }

    /**
     * Return the payload
     */
    private function getPayload(): ?object
    {
        $payload = json_decode($this->payload);

        return $payload ?? null;
    }

    /**
     * Return the data encoded in a WashClubTransactionProcessingException
     */
    private function decodeMyExceptionData(): ?object
    {
        $data = json_decode(preg_replace('/^App\\\Exceptions\\\WashClubTransactionProcessingException: ([^\n]*) in.*/s', '$1', $this->exception));

        return $data ?? null;
    }

任何地方:

$failedJob = FailedJob::find(1);

dd([
    $failedJob->message,
    $failedJob->related_record,
]);

这篇关于Laravel作业失败后是否可以获取最后的failed_jobs记录ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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