yalsicor
1 month ago
236
4
General

Scheduled job constructor not called on subsequent calls in containerized environment

Posted 1 month ago by yalsicor
  • Laravel Version: 7.23.0
  • PHP Version: 7.4.4
  • Database Driver & Version: psql 12.3.0

I am running Laravel in a Heroku like PaaS environment. There are no cronjobs so I use a long running schdeuler process to trigger scheduler events. When dispatching jobs on the scheduler the Job constructor only gets called once, subsequent calls seem to use the same instance and no new data can be passed to the job.

When running the scheduler via cronjobs everything works as expected.

Long running scheduler:

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;

class SchedulerDaemon extends Command
{
    protected $signature = 'schedule:daemon {--sleep=60}';

    protected $description = 'Call the scheduler every minute.';

    public function handle()
    {
        while (true) {
            $this->line('<info>[' . Carbon::now()->format('Y-m-d H:i:s') . ']</info> Calling scheduler');
            $this->call('schedule:run');
            sleep($this->option('sleep'));
        }
    }
}

In app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
    {
        $schedule->job(new SomeJob())->everyMinute();
    }

My SomeJob class:

namespace App\Jobs;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class SomeJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct()
    {
        Log::debug(Carbon::now()->format('[Y-m-d H:i:s]') . ' This is the Job constructor.');
    }

    public function handle()
    {
        Log::debug(Carbon::now()->format('[Y-m-d H:i:s]') . ' This is the Job handler.');
    }
}

The logs only show the handler Log messages. What is going on here? Is there a way to circumvent the instance "caching"?

Please sign in or create an account to participate in this conversation.