8
votes

I feel a little bit silly for asking this question but I can't seem to find an answer on the internet for this problem. After searching for several hours I figured out that on a linux server you use Supervisor to run "php artisan queue:listen" (either with or without daemon) continuously on your website to handle jobs pushed to the queue. This is all well and good, but what if I want to do this on a Windows Azure web app? After searching around the solutions I found were:

  • Make a chron job to run "php artisan queue:listen" every minute (or every X minutes), I really dislike this solution and wanted to avoid it specially if the site gets more traffic;
  • Add a WebJob that runs "php artisan queue:listen" continuously (the problem here is I don't know how to write the script for the WebJob...);

I want to ask you guys for help on to know which of these is the correct solution, if there is a better one and if the WebJob is the best one how do I write the script for this? Thanks in advance.

3

3 Answers

7
votes

In short, Supervisor is a modern alternative to nohup (no hang up) with a few other bits and pieces tacked on. In short, there's other resources that can keep a task running in the background (daemon) and the solution I use for Windows based projects (very few tbh) is Forever which I discovered via: https://stackoverflow.com/a/18226392/5912664

C:\myprojectroot > forever -c php artisan queue:listen --queue=some_nice_queue --tries=3

How?

Install node for Windows, then with npm install Forever

C:\myprojectroot > npm install -g forever

If you're stuck for getting Node running on Windows, I recommend the Windows Package Manager, Chocolatey

https://chocolatey.org/packages?q=node

Be sure to check for any logfiles that Forever creates, as I had left one long enough to consume 30Gb of disk space!

4
votes

For Azure you can make a new webjob to your web app, and upload a .cmd file including a command like this.

php %HOME%\site\wwwroot\artisan queue:work --daemon

and defining that as a triguered and 0 * * * * * frequency cron.

that way work for me.

best.

0
votes

First of all you cannot use a WebJob with Laravel on Azure. The Azure PHP Web App is hosted on Linux. WebJobs do not work with Linux at this moment.

The best way to do chron jobs in Laravel on Azure is to create an Azure Logic App. You use the Recurrence trigger and then a HTTP action to send a POST request to your Laravel Web App. You use this periodic heartbeat to run whatever actions you need to do. Be sure to add authentication to your POST request.

The next problem you will have is that POST will be synchronous so the work you are doing cannot be extensive or your HTTP request will time out or you will reach the time limit on PHP scripts (60 seconds).

The solution is not Laravel Jobs because here again you need something running in the background to process the queues.

The solution is also not PHP threads. The standard Azure PHP Web App does not support PHP Threads. You can of course build your own Web App and enable PHP threads, but this is really swimming upstream.

You simply have to live with synchronous logic. So the work you are doing with the heartbeat should take no more than about 60 seconds.

If you need more extensive processing then you really need to off load it to another place: another Web App, an Azure Function, etc.

But why not do that in the first place? The reason is cost and complexity. If you have something simple...like a daily report...you simply connect the report to the heartbeat and all the facilities for producing the report are right there in Laravel. To separate the daily report into its own container would require setup and the Web App it runs in would incur costs...not worth it in my view for something simple.