Moving to Azure: Worker Roles for Nothing and Tasks for Free

If you’ve been to at least one Azure presentation you’ve probably heard Azure has two primary roles: Web Roles and Worker Roles.  Web Roles run websites, and Worker Roles run tasks.  The two sites I’m moving, XboxIndies.com (which is moved, yay!) and GameMarx.com, use a few automation scripts to update the database and aggregate news feeds.  This sounds like a perfect excuse for a Worker Role, but there is a draw back to spinning up another server: cost.  There is a cheaper alternative though…

Roles in Azure are really customized VMs running some flavor of Windows Server 2008.  This means there is a Task Scheduler we can use to run automation tasks!

The easy way to setup a scheduled task in an Azure Web Role is to enable Remote Desktop, log in and add a task.  I won’t cover details, but setting up Remote Desktop access with the Azure SDK 1.5 is simple and 100% GUI supported.  You will have to start the Task Scheduler service as by default the service is set to start up “Manual”.

Setting up the task manually is good for development, but you’ll probably want to automate the process to simplify deployments.  Note:  Any startup scripts will be run per instance of your Web Role, so make sure there is no harm in the application by having multiple servers run the same script.

Now, before I show you my solution, I want to say I tried to use PowerShell like the cool kids.  An hour into to trying to figure how the Task Scheduler API works inside of PowerShell I decided to scrap that approach and go with what I know works without fuss – Windows Shell Scripts!

Here is my startup script:

net start "task scheduler"
net user autoguy n0t@RF45$_ /add
net localgroup Administrators autoguy /add
schtasks /create /SC DAILY /MO 1 /ST 08:00 /TN DBUpdate /TR %~dp0XBLIGUpdate.exe /F /RU autoguy /RP n0t@RF45$_ /RL HIGHEST
schtasks /create /SC MINUTE /MO 15 /ST 00:00 /TN StoryUpdate /TR "%~dp0SitePing.exe http://www.xboxindies.com/hiddenupdateurl" /F /RU autoguy /RP n0t@RF45$_ /RL HIGHEST

I saved this script as “startup.cmd” in a folder called “ServerScripts” in the root of my WebRole.  I also placed a web.config file in this folder set to deny all access from the web.  </>

A quick rundown of the commands in the script:

  • “net start” starts the task scheduler service.
  • ”net user” creates a new user on the server.  The arguments are the username and password.
  • “net localgroup” adds the new user to a local group on the server.  I am adding the new “autoguy” user to the Administrators group.
  • The last two lines add the scheduled tasks.

You can get help on the various task creation switches by running “schtasks /create /?” at your local command prompt.  The only magic in these line is the command script variable %~dp0 which is the folder the script is in.  I added the programs XBLIGUpdate.exe and SitePing.exe to the same folder so it would be easy to reference in the script, since I can’t make full path assumptions in Azure.

To run this script when the role is instanced, add the following to the <WebRole> section of your ServiceDefinition.csdef file:

<Startup>
  <Task taskType="background" commandLine="ServerScripts\startup.cmd" executionContext="elevated"/>      
</Startup>

I chose to place the task in the background so that it doesn’t block the startup of the Web Role.  If you set the script to “simple” and it fails, your Web Role could fail to start.

Posted By Mike On Monday, October 24, 2011
Filed under azure | No Comments

Submit this story to DotNetKicks   

Leave a comment



Your name:
 

Your email (not shown):
 
Will display your Gravatar image.

Your website (optional):



About Michael

Michael C. Neel, born 1976 in Houston, TX and now live in Knoxvile, TN. Software developer, currently .Net focused. Board member of ETNUG and organizes CodeStock, East Tennessee's annual developers conference. .Net speaker, a Microsoft ASP.NET MVP and ASPInsider. Co-Founder of FuncWorks, LLC and GameMarx.

Proud father of two amazing girls, Rachel and Hannah, and loving husband to Cicelie who inflates and pops his ego as necessary.

 Subscribe to ViNull.com |  Comments

Follow me on Twitter | Contact Me

Related Posts

Moving to Azure: Static Assets

You may not have noticed, but this image came from the cloud! The next stage in migrating GameMarx.com and XboxIndies.com to Azure is moving the static ... Read more

Moving to Azure: The Database

Azure has two main services for persistent data storage for application: Azure Table Storage and SQL Azure.  The really boil down to a normal SQL ... Read more

Moving to Azure: The Plan and Start

After a conversation with my former webhost about the meaning of "Recurring Annual Discount" (we have different definitions of the word "Recurring"), ... Read more

Debugging Azure WebRoles with Multiple Sites

Microsoft Azure has added a cool ability to allow you to host more than one website in a WebRole.  You may know this feature better as named based ... Read more

"Quest" Post Mortem - Ludum Dare #26

This past weekend I took part in my first ever Ludum Dare, a competition to complete a game in 48 hours.  There is no prize for first place, there ... Read more

XNA 3D Primer by Michael C. Neel

XNA 3D Primer by Michael C. Neel
Buy Now: [ Amazon ] [ Wrox ]

GameMarx

CodeStock

ASPInsiders Member

ETNUG Member