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 10-23-2011 11:14 PM by Michael C. Neel

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)