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 Using Laravel's Markdown parser for a blog - and adding lazy images Read more
Written 2 months ago
7 min read

Using Laravel's Markdown parser for a blog - and adding lazy images

I found out Laravel 6.x already ships with a pretty powerful package for parsing Markdown, and it can be extended and customized.

After a few headaches I was able to configure it to my liking for this blog, and even create an extension that adds lazy images.

Read more
List image of the post Teams should design in the cloud... and why Figma stole my heart Read more
Written 3 months ago
7 min read

Teams should design in the cloud... and why Figma stole my heart

I strongly believe that all developers are only as good as their teams.

Let's have a little "meta monologue" about why I think design in the cloud is a low hanging fruit for team productivity - and why every developer should care.

Read more
Back to all posts