Microsoft Azure Webjobs
The long awaited webjobs is now in preview for Windows Azure! How many times have you deployed a website, then after a couple months requirements change and they want you to run a daily or continuous job that processes files or re-size images or works a queue, now with web jobs in Windows Azure it is easy.
What is webjob?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites. It is kind of a lightweight Worker Role associated with web site. Process life cycle is strictly connected with web sites, so restarting a web site will cause a Web Job restart.
Each Web job can be configured to run in one of the following modes:
1. Continuous – runs all the time, analogy to Windows Service configured with auto start option is in place. Hosting environment monitors the job status and brings it up when process is down. (NOTE: With the current preview, this works properly on standard web sites with ‘always on’ setting. Caution is required when we scale the site up from Standard mode – Web Jobs will be terminated when there is no request for 20 minutes)
2. Scheduled – runs at particular times.
3. On Demand – runs when started manually.
Creating WebJobs:
A simple console application will work for you as a webjob.
Deploying Webjobs:
We can deploy webjob in either two ways -
1. Automatic deployment - When you publish web application, webjobs get deployed automatically.
2. Manual deployment - Manually deploy code file to portal.
Manual Deployment:
1. To manually deploy our application as new Web Job – we need to create a zip file that contains our project output (in our case standalone exe file is enough). It is important to remember that the main executable file needs to be on the root level of that zip file. Name of the file is irrelevant. The current limit size for one zip file is 100 MB.
2. Open Azure Management Portal and select destination web site.
3. On the web site details page go to Web Jobs tab and click New
4. Now we can configure our new Web Job.I want to run this one every 5 minutes:
Automatic Deployment:
2. Select when the WebJob shall run and click OK. In this example, the job shall be run on demand.
3. Select a publish target, in this example the target shall be an Azure WebSite
What is webjob?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites. It is kind of a lightweight Worker Role associated with web site. Process life cycle is strictly connected with web sites, so restarting a web site will cause a Web Job restart.
Each Web job can be configured to run in one of the following modes:
1. Continuous – runs all the time, analogy to Windows Service configured with auto start option is in place. Hosting environment monitors the job status and brings it up when process is down. (NOTE: With the current preview, this works properly on standard web sites with ‘always on’ setting. Caution is required when we scale the site up from Standard mode – Web Jobs will be terminated when there is no request for 20 minutes)
2. Scheduled – runs at particular times.
3. On Demand – runs when started manually.
Azure WebJobs can be created using the following scripts:
- .cmd, .bat, .exe (using windows cmd)
- .ps1 (using powershell)
- .sh (using bash)
- .php (using php)
- .py (using python)
- .js (using node)
Creating WebJobs:
A simple console application will work for you as a webjob.
Deploying Webjobs:
We can deploy webjob in either two ways -
1. Automatic deployment - When you publish web application, webjobs get deployed automatically.
2. Manual deployment - Manually deploy code file to portal.
Manual Deployment:
1. To manually deploy our application as new Web Job – we need to create a zip file that contains our project output (in our case standalone exe file is enough). It is important to remember that the main executable file needs to be on the root level of that zip file. Name of the file is irrelevant. The current limit size for one zip file is 100 MB.
2. Open Azure Management Portal and select destination web site.
3. On the web site details page go to Web Jobs tab and click New
4. Now we can configure our new Web Job.I want to run this one every 5 minutes:
5. Now, our job is up and running. To verify it, we can check log file produced by Web Job (link in Logs columns) – this file contains diagnostic information about run and application output that was redirected automatically to that file (the same goes for error stream, so if, by any chance, program throws exception that is unhandled it will also be logged in that file and job run status will be set as Failed)
And the output:
And the output:
Automatic Deployment:
1. To deploy the WebJob, right-click on the project and select Publish as Azure WebJob.
2. Select when the WebJob shall run and click OK. In this example, the job shall be run on demand.
3. Select a publish target, in this example the target shall be an Azure WebSite
You shall be required to sign-in and enter your Microsoft Azure credentials
4. Click on OK, then select Publish.
Now, if you go the the Microsoft Azure portal, and navigate to your WebSite, you shall see a new WebJob created and deployed.
Now, if you go the the Microsoft Azure portal, and navigate to your WebSite, you shall see a new WebJob created and deployed.
Make webjob settings configurable:
It attempts to read configuration settings in this prioritized order:
- Try to find the configuration value in the Web Site's online configuration (see below).
- Try to find the configuration value in the .cscfg file.
- Try to find the configuration value in the app.config file or web.config file.
Exception in webjobs which I have faced:
Azure WebJob that fails after only 20 minutes of running with the exception "Thread was being aborted".
Cause: By default, web apps are unloaded if they are idle for some period of time. This lets the system conserve resources.
Resolution: In Basic or Standard mode, you can enable Always On to keep the app loaded all the time. If your app runs continuous web jobs, you should enable Always On, or the web jobs may not run reliably.
Also add few settings to your webjob which are mentioned below -
1. WEBJOBS_IDLE_TIMEOUT = 14400
2. STOPPING_WAIT_TIME = 14400
The time is specified in seconds