Search code examples
laravellaravel-5.1azure-web-app-serviceazure-webjobsazure-scheduler

Laravel schedule on Azure WebApp


I am looking for a way to link an azure scheduler or web job to the Laravel schedule.

My understanding is, to set up an Azure schedule I would need an end point to link to my Laravel, which I am not sure how to achieve that.


Solution

  • TL;DR

    You can use the WebJobs under WebApps with an commandline script to trigger the Laravel scheduler.

    Full reference

    Azure providers WebJobs that can fire various triggers including Cron-like schedulers. In order to run the Laravel scheduler you need to trigger the schedule:run command every minute. For now I'll assume artisan lives in D:\home\site\wwwroot\artisan which is the default location for PHP based deployments.

    1. Create a new file called runsched.cmd or anything else als long as it has the .cmd extension. Edit the file with notepad and add:

      php %HOME%\site\wwwroot\artisan schedule:run

      Save the file and go to the Azure portal.

    2. Select you WebApp and find WebJobs under the application settings. Click Add and a sidepanel will appear.

    3. Give the WebJob a name, for example LaravelSchulder and upload the runsched.cmd file from the first step.

    4. Set Type to Triggered and make sure Triggers is set to Scheduled.

    5. Now you can specify how often the command must be triggered. Even the portal says 'CRON Expression' the cron format is not the same as the Linux notation. If you set the expression to all asterisks as shown in the Laravel documentation you're command will be triggered every second, which is way too often for most applications. The correct CRON Expression is:

      0 * * * * *

    6. If you're Job looks something like this click OK.

    Azure WebJob Laravel Schuduler

    1. The Laravel scheduler will now be triggered every minute. To verify that everything is working correctly you can trigger the job once yourself (Select the LaravelSchulder job and click Run) and check the job status. If Azure reports Failed under status check the logs and make sure you've entered the correct paths'

    Hope that explains it.

    Quick note: Microsoft likes to change Azure Portal on a regular basis, so any of these instructions may have changed by now.