Microsoft Azure WebJobs in Truck Trials

In my latest game Truck Trials (read the Making of Guide) I’m running a special championship every weekend, named Weekend Champion Cup.
image

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!

How?
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.

 

The implementation

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.

image

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:
image

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:
image

image

 

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:
image

2. Click Add
image

 

3. Fill out the information on page 1 and click next
image

4. Fill out in the information on page 2 and click next
image

 

5. Wait for the WebJob upload to complete
image

6. Test it by clicking RUN ONCE:
image

 

Checking the output

The cool thing about this is that you can check the console output. Just click the LOGS link on your WebJob.
image

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:
image

By clicking the WebJob, you can dive in to the details, and also see the console output:

image

And that’s it, you got a WebJob up and running, doing critical tasks for you Smile 

Thanks to Pedro Dias for helping me with this!

This entry was posted in Azure, Truck Trials, Tutorial. Bookmark the permalink.

1 Response to Microsoft Azure WebJobs in Truck Trials

  1. jaime3espinosa says:

    Please fill out the survey on WebJobs here: https://microsoft.qualtrics.com/SE/?SID=SV_8iRbWLZVMIhjqTj

    Thanks!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.