Do you use the sync queue connection ? It is defined in the .env file.
Laravel schedule cron() problem
Hey all,
I'm having trouble scheduling jobs from a task.
$this->schedule->job(new MyJob, 'default')->cron('0 0 15 12 *');
One would think this would schedule a job to run at midnight, 15th December.
However I can't see this Job appearing in the jobs table. So I assume it won't appear until that time.
Then I wondered if I can see the list by simple doing a dd($schedule-events()) . It gives me an empty array [].
Is this expected? How can I see my pending schedule job? crontab -l doesn't help me either.
Hmm, so I tried the following code:
resolve(Illuminate\Console\Scheduling\Schedule::class)->events();
And it indeed lists all the scheduled commands.
But by reading your later posts I got a better picture on your problem.
In your job you are calling $this-schedule->job(...)->cron(...) hoping that Laravel will persist on the scheduler to be ran later, right?
Unfortunately this is not how it works :(
The schedule:run run command will only check for commands defined in the schedule method on your Console\Kernel file. You can see this statement on the docs:
https://laravel.com/docs/6.x/scheduling#defining-schedules
You may define all of your scheduled tasks in the schedule method of the App\Console\Kernel class.
So when you inject the Schedule object in your job and try to schedule a new job through it, nothing happens in practice.
To add jobs dynamically you could create a Command that runs every minute that checks a table for future jobs and run them on your expected time.
Luckily there is a package for that:
https://github.com/codestudiohq/laravel-totem
This package provides a dashboard so you can monitor (add/edit/delete) scheduled jobs.
As you need to add jobs programmatically take a look in its TasksController and TaskRequestto add new scheduled tasks manually.
Please or to participate in this conversation.