Search code examples
laravelredisscheduled-tasks

Laravel scheduler executes the wrong job


I have some schedules in my Kernel and they will be executed as schedule:

protected function schedule(Schedule $schedule): void
{
    $schedule->job(new PdnsSync())
        ->everyTenMinutes()
        ->onOneServer();


     $schedule->job(new DismissRiskyUsersScheduler)
         ->everyFiveMinutes()
         ->onOneServer();

     // many more...
}

Due too some planed maintenance, I had to stop the queue workers (about 20 containers) for some time. After bringing them back online, the PdnsSync job will be queued correctly but it executes the DismissRiskyUsersScheduler.

I found this behaviour by enable telescope, go to the scheduler, click on the PdnsSync command an follow the jobs:

telegram/scheduler

Jobs from the scheduled command:

telescope/jobs

  • queue-connection is redis
  • laravel version 10
  • php 8.2

I've tried:

php artisan optimize:clear
php artisan cache:clear
  • restarted all containers (front-end and workers)
  • restarted redis

Because the question was asked if the PdnsSync class is realy calling the correct jobs, here the class:

class PdnsSync implements ShouldQueue, ShouldBeUnique
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const CHUNK = 10;

    protected array $jobs = [
        LhgTenantJob::class,
        AviatarTenantJob::class
    ];

    public function handle(): void
    {
        $jobs = $this->getJobs();

        if (empty($jobs)) {
            return;
        }

        if (count($jobs) > self::CHUNK) {
            $jobs = array_chunk($jobs, self::CHUNK);
        }

        Bus::batch([$jobs])
            ->name('pdns')
            ->dispatch();
    }

    protected function getJobs(): array
    {
        foreach ($this->jobs as $job) {
            $jobs[] = new $job;
        }

        return $jobs ?? [];
    }
}

Update

I've changed the PdnsSync job to run in a dedicated queue called pdns. I also added the queue to the workers using the php artisan queue:work --queue=default,pdns command.

php artisan schedule:list shows all the schedules as expected:

# truncated list
*/10 * * * *  App\Jobs\PdnsSync ............ Next Due: xxx minutes from now
# ...other schedules

running the PdnsSync schedule manually via php artisan schedule:test, still not executing the job.

php artisan queue:monitor redis:default,pdns

  Queue name ......................................... Size / Status
  [redis] default ........................................... [0] OK
  [redis] pdns .............................................. [0] OK

In addition, telescope does even not showing any jobs in relation to the scheduled task:

telescope/pdns_3

To summarize this:

All scheduled task/commands work fine except the PdnsSync which did before and I run out of ideas.

Update

I've renamed the class from App\Jobs\PdnsSync to App\Jobs\PrivateDnsUpdate. Now the jobs run as they should. I would like to understand this in depth.

Thanks for any ideas!


Solution

  • I've removed the ShouldBeUnique interface in the Pdns Class, which resolved the problem.

    Before

    class PdnsSync implements ShouldQueue, ShouldBeUnique
    

    After

    class PdnsSync implements ShouldQueue
    

    However, I don't understand (yet) why this worked before but not after the maintenance.