phplaravellaravel-horizon

Laravel Horizon: job status tracking


For a certain Laravel Horizon job, I have a function that checks if job for specific model is currently running, based on Horizon repositories:

public static function isRunning($model)
{        
    $tag = self::getTag($model);
    $tagRepository = app()->make(TagRepository::class);
    $jobRepository = app()->make(JobRepository::class);
    $jobIds = $tagRepository->jobs($tag);
    $jobs = $jobRepository->getJobs($jobIds);
    $runningJobs = collect($jobs)->reject(function($job) {
        return $job->status === HorizonRedisJobStatuses::STATUS_COMPLETED // 'completed'
            || $job->status === HorizonRedisJobStatuses::STATUS_FAILED; // 'failed'
    });

    return $runningJobs->count() > 0;
}

This code works most of time, however, there are times when when such job is restarted because of timeout. In such case, all the jobs I have in $jobs variable are marked as "completed", so function returns false. It seems that restarted jobs are not populated to repositories.

Currently the only workaround I came up with is to track job status manually in database on start/finish of processes, however, that can still give me wrong results if, for instance, Horizon runs out of attempts.

So does anyone have any ideas how to track job status in Horizon properly?


Solution

  • I posted it as a bug on Horizon Github, it was fixed in pull request https://github.com/laravel/horizon/pull/478

    Now the code in example should work properly.