In Azure Scheduler, how to programmatically create recurring monthly job?

3.2k Views Asked by At

I've been able to successfully schedule jobs in Azure Scheduler using Microsoft Azure Scheduler Management Library for all intervals except monthly on a specific day of the week. For example, I need to schedule a recurring job that runs every 1 month on the first Thursday of the month. The Azure Scheduler portal allows for this but I can't figure out how to code this using the Azure library.

Below is the latest code I've tried. Azure Scheduler ends up creating a monthly recurring job (viewed in the Azure portal) but it doesn't show any selections for day-of-week (they're all unchecked) so the code did not work.

I searched exhaustively online for documentation or examples for using the scheduler library for this scenario but came up empty. I am seeking a working code example for this monthly recurrence.

var monthlyOccurrence = new List<JobScheduleMonthlyOccurrence>();
monthlyOccurrence.Add(new JobScheduleMonthlyOccurrence() { Day = JobScheduleDay.Thursday, Occurrence = 1 });

JobCreateOrUpdateResponse jobResp = schedClient.Jobs.CreateOrUpdate("testRecurrenceIssue", new JobCreateOrUpdateParameters
{
    Action = new JobAction
    {
        Request = new JobHttpRequest { Uri = new Uri("http://www.myservice.com"), Method = "GET" },
    },
    Recurrence = new JobRecurrence
    {
        Frequency = JobRecurrenceFrequency.Month,
        Interval = 1,
        EndTime = new DateTime(2014, 12, 31),
        Schedule = new JobRecurrenceSchedule
        {
            Days = null,
            Hours = null,
            Minutes = null,
            MonthDays = null,
            MonthlyOccurrences = monthlyOccurrence,
            Months = null
        }
    }
});

Note that I've been able to schedule a monthly recurrence for specific days of the month, such as "run monthly on days 1, 14, 21, and 28" but can't figure out how to code the specific day of week scenario I mentioned above. Thanks for your help!

2

There are 2 best solutions below

3
On

I'm one of the PM's responsible for the Management Libraries, inclusive of the Scheduler SDK. They're one of our partner teams, and we support them in the development of their SDK. At this point the Scheduler SDK is functional, and we're using their SDK during development of some other upcoming features in other products we're releasing so I can attest to it being functional at this point in most of the use-case situations. I'm going to ask the Scheduler PM to take a look at this post, too, as he might have other information on the state of the SDK and may have other information that could aid in your development.

Take a look at this repo on GitHub. I worked this up as a prototype for setting up scheduled WebJobs. Since the WebJobs functionality actually makes use of the Scheduler to schedule the jobs' execution, this seemed like a pretty good demo case showing how 2 different Azure assets could be used together within MAML.

https://github.com/bradygaster/maml-demo-scheduled-webjob-creator

3
On

Here is a code snippet that works for me. There is an issue, as you've mentioned, with JobScheduleDay.Sunday. We are actively looking into the issue.

{
JobCreateOrUpdateResponse jobResp = schedClient.Jobs.CreateOrUpdate("testComplexRecurrenceTwoDays", new JobCreateOrUpdateParameters
            {
                Action = new JobAction
                {
                    Request = new JobHttpRequest { Uri = new Uri("http://www.bing.com"), Method = "GET" },
                },
            Recurrence = new JobRecurrence
            {
                Frequency = JobRecurrenceFrequency.Month,
                Schedule = new JobRecurrenceSchedule
                {
                    Days = null,
                    Hours = null,
                    Minutes = null,
                    MonthDays = null,
                    MonthlyOccurrences = new List<JobScheduleMonthlyOccurrence> { 
                        new JobScheduleMonthlyOccurrence { Day = JobScheduleDay.Thursday, Occurrence = 1}
                        },
                    Months = null
                }
            }
            });
}