Running Scheduled Jobs on Windows Azure

Osmosis Support posted this on technical

Windows Azure is Microsoft’s cloud hosting platform, and appears to be aimed as a competitor to AWS, or other hosting products. While Windows Server has traditionally been popular as a server OS in some parts of the Enterprise space, it’s interesting to see it used as an affordably pricing hosting product.

As a .NET based product, Osmosis currently uses Azure to manage job scheduling, running a Windows VM to run some background tasks. This gives us the advantage of using some of the nice tools that come with .NET; C# for instance, is a very nice language, combining both the strong history of C/C++ based syntax, the lessons of Java, while being part of the functional renaissance.

For instance, one handy thing you can set up as a background process is daily sales reports, which are emailed to the various people who need them. These might consist of tables that show the results of various database queries, like how many trial accounts are active, who sent them, and so on.

You can define a job similarly to how you might define a Thread:

namespace Sample.Tasks
    interface ITask
        void Start();

You can then define a main program that knows how to call each task - with this setup, you have a utility that can now run from the command line, which is handy for testing. Later on, we’ll configure Azure to run this the same way.

namespace JobProcess
    class Program
        static void Main(string[] args)
            Tasks.ITaskRunner task = null;
            if (args[0] == "-sales")
                task = new Tasks.SalesTask();

Given this, all you need now is a simple class to send emails (you can use SendGrid [], or any other product that suits your needs)/

class SalesReports : ITaskRunner
    public void Start()
        var mailer = new Mailer();

        string body = "";

        var sales = new SalesRepository();
        var leads = sales.getLeads(Account.Statuses.Trial);

        if (leads != null && leads.Count > 0)
            body += "Trial Accounts: \r\n";
            foreach (var e in leads)
               body += string.Format("{0} (Created: {3}, Last Login: {4} days) \r\n", 
                                                 e.Email, e.Created, e.LastSignIn);

              body += "\r\n";

        if (!string.IsNullOrEmpty(body))
            SendGrid myMessage = SendGrid.GetInstance();

            myMessage.From = new MailAddress(@"Osmosis Support");
            myMessage.Html = body;
            myMessage.Subject = "Sales Report";

            ...send email...

In your Azure VM, you can then simply open the Task Scheduler, and create a task which runs on a schedule:

A few things to note here:

  • Make sure to set the job to run whether the user is logged in or not
  • You can easily remote desktop to an Azure VM. When you do so, your local drives are automatically mapped on the remote one, which makes deployments simple.
  • If you set up a free trial of Azure, pay attention to the billing notices. If they lock your account, their support will be very helpful at letting you pay, but it is a fairly complex product and can be difficult to figure out the right point of contact.
  • For more complex products, you may find this is too simplistic to meet your needs, but don’t overlook how simple solutions can easily get you what you need.

14-day free trial. No credit card required. Sign Up for a Free Trial!