Header image of Use the Laravel Scheduler without access to cronjobs

The Laravel Scheduler is an awesome tool since it greatly simplifies the usage of confusing cron job definitions I am not smart enough for.

But in all seriousness: I like the fact that moves schedules in the project's version control, and it also offers great readability πŸ™‚

The setup seems to be really simple, see the docs

When using the scheduler, you only need to add the following Cron entry to your server

Hm, what's a "Cron"? (looks it up) Nooo! I cannot do that since I am using a cheap hosting service!

I have no direct access to my server πŸ₯Ί

I personally still have some projects where I have no root access and either can't setup cron jobs at all ... or have a (usually light speed ugly) interface to set up cron jobs on only a route to visit periodically, maybe not even every minute.

Some customers still want to use cheap hosting, and that should be okay πŸ€·β€β™‚οΈ

I lately found a pretty approachable solution to this!

Setup a GET route for the scheduler

The first and central thing you should do is to set up a /scheduler route that will serve as an alternative to running the command php artisan schedule:run from the command-line.

Route::get('/scheduler', function() {
    Artisan::call('schedule:run');
});

This makes the command accessible via a simple GET request. Hooray, we are almost done already! Most cheap hosters offer some way of periodically visit certain URLs, so why not set what up with your hoster of choice?

Just keep in mind that some of them won't allow calling your/scheduler route every minute, some even do it at more or less random intervals.

The problem is that Laravel Scheduler works in a special way: Say you schedule things for every 15 minutes, if your hoster never calls the route exactly at the full hour or fifteen, thirty or forty-five minutes after a full hour, your schedule might never be called.

You could resort to minutely or daily scheduled tasks, but if there are many of them this might cause the PHP process to time out since it is technically not run on the commandline. It's bound to the execution time and memory limit.

Don't despair if your hoster does not offer the "one minute cron job": There is another solution!

Using external services

Since visiting the new route yourself every minute would make you a modern-day Sisyphus - Why not use a product that offers "cron jobs as a service"?

CaaS, I even like how that sounds. Almost like the Dutch word for cheese. Hmmm cheese! πŸ§€

Some of them are listed at cronjobservices.com, they did good work of comparing some of them. But hey, other options are just a googly search away πŸ™‚

Screenshot from https://www.cronjobservices.com/

I personally tried cron-job.org and I am really happy with them, it is free and allows cron jobs to run every minute.

Hey, that's all I wanted to say already πŸ™‚

Bonus: Testing dat route tho

Just a little bonus: If you want to test if your scheduler command gets run when you hit the route it is really straightforward

/** @test */ 
public function the_scheduler_route_works() {
  Artisan::shouldReceive('call')
    ->once()
    ->with('schedule:run');

  $this->get('/scheduler')->assertStatus(200); 
}

Testing that certain commands are run is that easy. Now you have a test in place that "communicates" the importance of that route.

Hooray, I hope this was helpful! Happy hacking and see you soon. πŸ™‚

Read more posts
List image of the post How to use Spatie's response cache with Statamic Read more
Written 4 months ago
2 min read

How to use Spatie's response cache with Statamic

Spatie's response cache package speeds up your page quite considerably.

This is fine for static pages, but I found a quick way how to use the library with the awesome Laravel CMS Statamic

Read more
List image of the post Testing Vue in Laravel with Jest - let's make it a little more easy Read more
Written 10 months ago
8 min read

Testing Vue in Laravel with Jest - let's make it a little more easy

Laravel offers an awesome testing environment for PHP, but if use Vue.js components in your frontend and don't test them, you have a big blindspot.

Since it is quite a struggle to even get started, I wrote a pacakge to simplfy it. In this post, I also nerd out about the detais that went into writing the package.

Read more
Back to all posts