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.
So basically, this is what this application does:
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!
Now that the console app is done, it’s time to create the Web Job. First of all, we need to ZIP the program. This will then be uploaded as the WebJob package:
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:
3. Fill out the information on page 1 and click next
4. Fill out in the information on page 2 and click next
5. Wait for the WebJob upload to complete
6. Test it by clicking RUN ONCE:
Checking the output
The cool thing about this is that you can check the console output. Just click the LOGS link on your WebJob.
You will be redirected to the WebJobs page on Microsoft Azure, where you can see a list of web jobs, status and how long time it has gone since it was last run:
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!
Please fill out the survey on WebJobs here: https://microsoft.qualtrics.com/SE/?SID=SV_8iRbWLZVMIhjqTj