Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jdc1898's avatar
Level 21

Job Retries not logging

I have 3 Jobs that are within a Batch. Job 2 is designed to fail on the first run so that the Batch appears as cancelled. When I then retry the Batch from the Horizon dashboard, Job 2 completes successfully as intended. (I manually fixed the error so it would work) However, none of the log statements in my job are triggered on the retry and I cannot figure out why?

Here is how I define the Batch:

        $batch = Bus::batch($jobs)->then(function (Batch $batch) {
            $batch->delete();
        })->catch(function (Batch $batch, Throwable $e){
            Log::error('The Job: ' . $batch->failedJobIds[0] . ' has failed.');
        })->name($workflow->name . ' Batch')->dispatch();

Job2

public function handle(): void
    {
        $batchId = $this->batch()->id ?? null;

        Log::info('Demo Job 2 - Batch ID: ' . $batchId);
        Log::info('Demo Job 2 - Job ID:' . $this->job->getJobId());

        /** If the batch ID is not set, it means that this is not being ran from a Batch */
        if (!$batchId) {
            Log::info('Demo Job 2 - The Batch was not found. We are checking the DB for a match.');
            $batchId = WorkflowLog::where('job_id', $this->job->getJobId())->value('batch_id');
        }

        if (!$batchId) {
            throw new Exception('Unable to determine the Batch ID from either the Job nor the DB!');
        }

        $this->log = WorkflowLog::create(
            [
                'workflow_id' => 1,
                'step_id' => 2,
                'result' => 'in-progress',
                'batch_id' => $batchId,
                'job_id' => $this->job->getJobId()
            ]
        );

        $status = rand(0, 1);
        if ($status) {
            $this->log->update(['result' => 'failed']);
            throw new Exception('An exception has been thrown in Job 2.');
        }

        $this->log->update(['result' => 'success']);
        Log::info('Demo Job 2 finished');
    }

I've tried triggering the Job to be retried from Horizon and the artisan command but both result in the Job completing successfully and the Batch being completed and then deleted. I just don't understand why there are no log entries or DB entries.

Anyone ran into this?

0 likes
1 reply
jdc1898's avatar
jdc1898
OP
Best Answer
Level 21

Update

So after reviewing, it seems as the issue is related to the SkipIfBatchCancelled middleware.

    public function handle($job, $next)
    {
        if (method_exists($job, 'batch') && $job->batch()?->cancelled()) {
            return;
        }

        $next($job);
    }

When I remove this middleware, I can retry the Job and it works. However, the other Jobs in the Batch were processed while they should have been skipped until this one was cleared.

Please or to participate in this conversation.