Azure Webjobs - Deploy and manage them from your Node App
Have you ever wanted to manage your Webjobs directly from your Node app without having to browse the Azure portal?
You can also define if they would be triggered or continuous and also set the CRON expression for the triggered one.
Folder structure for your webjobs
There is a specific folder structure you should follow so that Azure can pick up your Javascript files and show them in the Webjobs settings.
App_Data
│
└──jobs
│
└───triggered
│
└───demo-webjob
│
└─run.js
Create your run.js
file in the above structure and push your changes. Refresh your Webjobs in Azure portal and you should be able to see it. If not, refresh again.
Add the CRON expression
Sweet. We now have a triggered webjob but we haven’t scheduled any CRON expression for it yet. We just need to add a settings.job
file in our webjob folder.
//settings.job
{
"schedule": "0 */15 * * * *"
}
The six fields of a CRON expression are:
{second} {minute} {hour} {day} {month} {day-of-week}
The above CRON expression will run the webjob every 15 minutes. Check this link for some CRON examples.
After adding the settings.job
you should be able to see your CRON expression appear under the SCHEDULE
column.
Continuous Webjob
Great, we have added a triggered webjob and set up how often it will run. You can follow the same process to add a continuous webjob. The difference between the two is that the continuous one will run only once its created. If you want it to run endlessly you have to handle that inside your code.
So the full folder structure for our two webjobs would be:
App_Data
│
└──jobs
│
|───triggered
| │
| └───demo-webjob
| │
| └─run.js
└───continuous
|
└───continuous-webjob
|
└─anotherWebjob.js
And the final result in Azure portal would be:
Notes - Links
Folder structure on GitHub: https://github.com/vaskort/vaskort.github.io/tree/second-post/App_Data
Azure Webjobs documentation: https://docs.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs
Cron examples: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer#cron-examples