Bump
Eloquent does not save changes to database in Queue Event handlers
I have bound some function to Queue Events in laravel.
Queue::failing(function (JobFailed $event) {
Log::info("FAILED JOB");
Helpers::updateJobLogToFailed($event);
Log::info("UPDATED JOB LOG TO FAILED JOB");
});
public static function updateJobLogToFailed(JobFailed $event) {
$job_id = $event->job->getJobId();
\Log::info("FAILING JOB $job_id");
$payload = $event->job->payload();
$job_uuid = $payload['uuid'];
$job_name = $payload['displayName'];
\Log::info("$job_name has failed");
$log = JobLog::firstWhere('job_uuid', $payload['uuid']);
if (!isset($log)) {
\Log::info("Job log not found for: $job_name");
return;
}
$job_failed_status = Lov::firstWhere([
'key' => 'failed',
'identifier' => 'JOB_STATUS'
]);
$log->status = $job_failed_status->key;
$log->exception = $event->exception->getMessage();
$log->stack_trace = $event->exception->getTraceAsString();
$log->save();
\Log::info("FAILED JOB $job_id: $job_uuid");
}
I am using laravel horizon to manage queues. One of my job failed after a timeout (2000s). This is what the logs look like
[2024-01-03 15:08:22] local.INFO: FAILED JOB
[2024-01-03 15:08:22] local.INFO: FAILING JOB d150b091-df57-43af-964e-821f3cd4cad1
[2024-01-03 15:08:22] local.INFO: App\Jobs\JobName has failed
[2024-01-03 15:08:22] local.INFO: FAILED JOB d150b091-df57-43af-964e-821f3cd4cad1: d150b091-df57-43af-964e-821f3cd4cad1
Note the last line in the logs, that line is logged at the end of "updateJobLogToFailed" method defined above. Logging this line also means that the log to be updated was found in the DB since the if condition for !isset($log) did not satisfy.
Despite all that the log is not updated to 'failed' and remains as if it was never updated in the database even when the logs indicate otherwise.
I would appreciate any help regarding this, for now I have no idea where to begin solving this. I anyone is willing to help, please let me know if you require additional information.
Please or to participate in this conversation.