azure scheduled jobs

my experience scheduling a job to call an api in azure

Tags: Gaming, Technology, Azure
Posted by mjourdan on December 27, 2019

In a previous post I wrote about my experience with the azure data factory. I thought I would share with you an example where I used the data stored in my factory and a azure scheduled job to fulfill another manual task. Since we now had daily data we could easily track when members join or leave the clan. With that data we can now track members anniversaries with the clan. Announcing someone’s anniversary is just a nice way to recognize our members.

We use discord to communicate within our clan. So, it was natural to setup a new channel in discord to make announcements to the clan.

Here is what I needed to do

  1. Build a RESTful API method on my website which I can call from an Azure scheduler job. The method will call the discord api and post messages to the channel.
  2. Build a Scheduler Job in Azure
  3. Schedule the job to run on the 1st of every month
RESTful API method

I built a RESTful API method. I will get a collection of members who joined on this month in the past years. Next, loop through the members to generate the message. Finally call the discord api to deliver the message.

foreach (var clanMemberServiceTracker in members)
{
	var years = DateTime.Now.Year - clanMemberServiceTracker.StartDate.Year;
	message = $"{message}  **{clanMemberServiceTracker.Name}** {years} Year! \n";
}

var contentBody = new WebHookFormParams
{
	content = message,
	username = "Anniversary Announcement"
};

using (var client = new HttpClient())
{
	var url = "https://discordapp.com/api/webhooks/XXX/XXX-XXX";
	var uri = new Uri(url);
	
	var json = new JavaScriptSerializer().Serialize(contentBody);
	var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
	var response = await client.PostAsync(uri, stringContent);

	return response.IsSuccessStatusCode;
}

Here is a screenshot of discord.

Scheduler Job

I logged into my Azure portal and purchased a new scheduler job. Look at how easy this job is going to be. All I need to do is make a request, setup a retry and schedule the job! I had the scheduler job setup in less than 2 minutes!

Here is my job, simple JSON to set it up.

Here is the schedule setup, triger to run on the first of the month and only the first day.