Shivamyadav's avatar

How to push the failed job from the custom table to the queue?

My app service provider queue event store the failed jobs to the custom table

Queue::failing(function (JobFailed $event)  use ($queueJobStatusRepository) {
            /** Extracting the current job class name */
            $job = $event->job;
            $payload = $job->payload();
            $action = $payload['displayName'];
            /** Extracting the current job class name */

            $queueJobStatusRepository->updateOrCreate(
                ['job_id' => $event->job->uuid()], // INFO: Uniquely identify to create or update
                [
                    'action' => $action, // INFO: Get the current Event / Job Class name
                    'status' => 4, // INFO: Failed
                    'exception' => str()->limit($event->exception->getMessage(), 2000)
                ]
            );
        });

I want to get the failed job uuid and based on that I could push that failed jobs to the queue again to process it.

0 likes
10 replies
Shivamyadav's avatar

My senior asked me to Store each job to the custom table with it's status queued, processing, processed or failed and later we can manually retry the failed jobs from the custom table with the payload.

Glukinho's avatar

This functionality is already implemented by php artisan queue:retry <job id> command. You can see how this command is implemented and do the same: vendor/laravel/framework/src/Illuminate/Queue/Console/RetryCommand.php

But I'm sure recreating Laravel queue system from scratch is not what your senior wants you to do.

1 like
Shivamyadav's avatar

Yes, I think he just was a list of all failed jobs in the ui listing and from there it self he could have an option to trigger all failed jobs or a particular without using the terminal. Everytime we don't have system with us may be for that thing.

1 like
martinbean's avatar

Yes, I think he just was a list of all failed jobs in the ui listing

@shivamyadav Your senior is literally re-inventing the wheel.

You could have just used Horizon for Redis-based queues. If you’re not using Redis, you still don’t need to push jobs to an entirely new table. You could have just queried Laravel’s native failed_jobs table. I’ve done it myself; including with a button to re-try failed jobs; no custom table needed.

Given the questions you’ve asked in such a short span of time, I don’t feel this “senior” is really someone you want to be learning habits from, as they seem to be senior in name only.

2 likes
Shivamyadav's avatar

As we are using the AWS so, I don't know how the AWS SQS works but it also stored the failed jobs but something different way it Store the payload and unique I'd as like primary key not the job uuid.

martinbean's avatar

@shivamyadav That makes absolutely no difference. I use SQS myself. You don’t need to store failed jobs in a custom table just to then present them in a UI.

You can query the failed_jobs table just like any other table:

$failedJobs = DB::table('failed_jobs')->paginate();
@foreach($failedJobs as $failedJob)
    <tr>
        <td>{{ $failedJob->uuid }}</td>
        <td>
            <!-- Button to re-try failed job -->
        </td>
    </tr>
@endforeach
1 like
martinbean's avatar

How to customise this table?

@shivamyadav By… customising it?

I don’t really know what you want me to tell you? You may as well give me access to your Git repository if you just want me to give you complete code examples.

kevinbui's avatar

What do you mean by customising the failed_jobs table? I believe it already got everything you need.

By the way, if you want to query the failed_jobs table, this will also work:

resolve('queue.failer')
    ->getTable()
    // ->where(...)
    ->paginate();

Please or to participate in this conversation.