1

Im dispatching jobs that makes use of a table containing unprocessed data fetched from a file from an ftp server; which this job should process and insert or update on a separate table.

In my job file I have the following code

public function handle(SyncData $syncData)
{
    try {
        $syncData->execute($this->sync_id);
    } catch (Exception $exception) {
        $this->fail($exception);
    }
}

public function failed(Exception $exception)
{
    DB::table('sync_updates')->where('id', $this->sync_id)->update([
        'errors' => json_encode([
            'message' => $exception->getMessage(),
            'stacktrace' => $exception->getTraceAsString(),
        ]),
        'status' => SyncStatus::FAILED,
    ]);
}

Somewhere in the execute method being called in this handle function, will throw an exception if some data is not found from the unprocessed data. It's technically just something like this

throw new Exception('Some error message here');

The problem here is that when an exception is thrown, no data gets changed whether I use a Model/Eloquent or a DB facade. As you can see in the failed method, I'm trying to change the sync status to failed, as well as logging the cause of the error. But the failed method is only able update data in the database once the job has been moved in the failed_jobs table.

This raises another issue that the exception being caught this time around is saying the job has been attempted too many times, instead of the original exception that caused the error.

Is there anyway I can commit these data changes at the first time it catches an exception without having to wait for it to retry this job before it fully fails and moves to the failed_jobs table?

NOTES:

The failed method is always triggered, even during the first time it catches the exception I throw.

The job can only commit database changes if successful, or if the job failed and has moved to the failed_jobs table

The error being logged to the sync_updates table, is saying '.. attempted too many times..', instead of the original exception, since it's only able to push database changes when the job is being moved to the failed_jobs table

Rick
  • 2,003
  • 4
  • 16
  • 28

0 Answers0