Using laravel Queues on shared hosting: A simple guide

Elusoji Sodeeq
Dec 2, 2017 · 2 min read

If you’re like me (and every other developer), you must have now realized the importance of using queues. Queues give you the power of async programming by allowing you run some important (any) task in the background.

Let’s think about it this way: in a typical web app, we have the usual request-response cycle where your user sends a request to your server, your server processes the request, when it’s done, it sends back a response to the user (browser) — this is the way it works.

Now, there is no problem with this, except in a situation whereby during the request, your server needs to send mails (to one or more users), generate pdf files, make calls to 4 other external APIs and so on, that’s getting too long right?

In a typical request-response, your user has to wait until your server is done with all those tasks before they get a feedback. These tasks can take anywhere between 30 seconds to forever, which is a lot, if you ask me.

Instead, how about, we do it such that user sends request, and get feedback immediately? cool right? But I hear you ask, what happens to our tasks? Well, we are not discarding them, we are just pushing them to background workers, so our users do not have to wait.

Setting up queue workers with laravel is quite easy, on your local server and when deployed to a cloud hosting environment, but not quite so when deploying to a shared hosting environment. However, there’s a simple solution for this as well.

For this post, I’m assuming you already have your queue configured (the drivers and all).
We will be combining the power of Laravel Task Scheduling, Laravel Queues and Cron Jobs

Steps:

  • Set up a cron job on your server like such: * * * * * php /path-to-project/artisan schedule:run >> /dev/null 2>&1
    The above is telling your cron to run once every minute. So, once every minutes, it runs your scheduled laravel task.
  • Now, in your code, in the Kernel.php file: App\Console\Kernel, add the following line: $schedule->command('queue:work --tries=3')
    ->cron('* * * * * *')
    ->withoutOverlapping();
  • So that your Kernel.php now looks like this:
Image for post
Image for post
  • You should be all set up now. Dispatch new jobs to your queue, and see them all executed every minute.

Quite easy, eh!
That’d be all from me, for now. Bye!

Originally published at sdktalks.blogspot.com on December 2, 2017.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store