In my latest game Truck Trials (read the Making of Guide) I’m running a special championship every weekend, named Weekend Champion Cup.
To get this cup started I need to do some simple things in the database:
1 – Friday at 17:00 GMT, Reset the data from the previous competition
2 – Friday at 18:00 GMT, Start the competition
3 – Sunday at 18:00 GMT, End the competition
4 – Sunday at 18:30 GMT, Award the winner
In the early days of this project, I did this manually because I had to prioritize other development tasks. But this meant that I had to make sure this was done every Friday evening, and stress about this during dinner or at a get together with the guys – and the same every Sunday.
Now this has change since Azure WebJobs is doing all of this for me!
It’s very simple – just create a Console application, zip it and upload it to a WebJob on your Azure Web Site project. When creating the WebJob, you can specify to run the application continuously or at a schedule.
Developing the Console App:
First of all, you need to develop the console app that will execute the task you want to perform.
To do this, start Visual Studio 2013 and create a new Console project (I used C#). In my case, I added the EntityFramework NuGet package since I was going to do some operations on the games database.
This Solution contains all of my WebJobs, one project pr. job I want to perform. In this post, I’m just going to focus on step 1, resetting the players score. All other steps are implemented in the same way.
It starts a Stopwatch (nice to track the time of the operation), resets the scores and writes the output to the Console.
The cool thing here is that all output from the Console app is displayed on the WebJobs management page, so I can simply just log in to the Microsoft Azure portal, and see the results there. More on this soon!
Creating the Web Job
Let’s make the cool stuff happen!
1. Log in to Microsoft Azure, go to your Azure Web Site project and click Web Jobs:
Checking the output
By clicking the WebJob, you can dive in to the details, and also see the console output:
And that’s it, you got a WebJob up and running, doing critical tasks for you
Thanks to Pedro Dias for helping me with this!