Windows Azure Scheduler allows you to perform various actions, such as HTTP / S requests or sending messages to the storage queue, on a schedule. With the help of the scheduler, you can create tasks in the cloud that are guaranteed to cause services both inside the cloud infrastructure and outside it. You can perform these tasks on demand or on a regular basis on a schedule, as well as schedule a performance on some date in the future.
PM> Install-Package Microsoft.WindowsAzure.Management.Scheduler –Pre
public static class CertificateCloudCredentialsFactory { public static CertificateCloudCredentials FromPublishSettingsFile(string path, string subscriptionName) { var profile = XDocument.Load(path); var subscriptionId = profile.Descendants("Subscription") .First(element => element.Attribute("Name").Value == subscriptionName) .Attribute("Id").Value; var certificate = new X509Certificate2( Convert.FromBase64String(profile.Descendants("PublishProfile").Descendants("Subscription").Single().Attribute("ManagementCertificate").Value)); return new CertificateCloudCredentials(subscriptionId, certificate); } }
var publishSettingsFilePath = @"D:\\azdem.publishsettings"; var subscriptionName = "Azdem194D92901Y"; var credentials = CertificateCloudCredentialsFactory .FromPublishSettingsFile(publishSettingsFilePath, subscriptionName);
var cloudServiceClient = new CloudServiceManagementClient(credentials); var result = cloudServiceClient.CloudServices.Create("sandrino-cs1", new CloudServiceCreateParameters() { Description = "sandrino-cs1", GeoRegion = "north europe", Label = "sandrino-cs1" }); Console.WriteLine(result.Status); Console.WriteLine(result.HttpStatusCode);
An unhandled exception of type 'Microsoft.WindowsAzure.CloudException' occurred in Microsoft.WindowsAzure.Management.Scheduler.dllNow let's register the task scheduler provider (registration is valid for the entire subscription):
Additional information: ForbiddenError:
var schedulerServiceClient = new SchedulerManagementClient(credentials); var result = schedulerServiceClient.RegisterResourceProvider(); Console.WriteLine(result.RequestId); Console.WriteLine(result.StatusCode); Console.ReadLine();
var schedulerServiceClient = new SchedulerManagementClient(credentials); var result = schedulerServiceClient.GetResourceProviderProperties(); foreach (var prop in result.Properties) { Console.WriteLine(prop.Key + ": " + prop.Value); } Console.ReadLine();
A task collection contains a group of tasks and manages settings, quotas, and restrictions that are common to all tasks in the collection. The collection of tasks is created by the owner of the subscription, it combines tasks together based on the consumption or boundaries of the application. The collection of tasks is limited to one region. It also allows you to set MaxJobs and MaxRecurrence quotas on the consumption metrics of all tasks in the collection.More information on quotas that can be used on both plans can be found at this link: http://msdn.microsoft.com/en-us/library/windowsazure/dn479786.aspx .
var schedulerServiceClient = new SchedulerManagementClient(credentials); var result = schedulerServiceClient.JobCollections.Create("sandrino-cs2", "jobcoll001", new JobCollectionCreateParameters() { Label = "jobcoll001", IntrinsicSettings = new JobCollectionIntrinsicSettings() { Plan = JobCollectionPlan.Standard, Quota = new JobCollectionQuota() { MaxJobCount = 100, MaxJobOccurrence = 100, MaxRecurrence = new JobCollectionMaxRecurrence() { Frequency = JobCollectionRecurrenceFrequency.Minute, Interval = 1 } } } }); Console.WriteLine(result.RequestId); Console.WriteLine(result.StatusCode); Console.ReadLine();
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = new JobAction() { Type = JobActionType.Http, Request = new JobHttpRequest() { Body = "customer=sandrino&command=sendnewsletter", Headers = new Dictionary() { { "Content-Type", "application/x-www-form-urlencoded" }, { "x-something", "value123" } }, Method = "POST", Uri = new Uri("http://postcatcher.in/catchers/527af9acfe325802000001cb") } }, StartTime = DateTime.UtcNow, Recurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Minute, Interval = 1, Count = 5 } }); Console.WriteLine(result.RequestId); Console.WriteLine(result.StatusCode); Console.ReadLine();
{ "connection": "close", "content-length": "40", "content-type": "application/x-www-form-urlencoded", "host": "postcatcher.in", "x-forwarded-for": "137.116.241.137", "x-ms-client-request-id": "988c7a64-55e1-41e4-8cf0-ce1eeca240ac", "x-ms-execution-tag": "0726fa245447c91674c75db3f3564d63", "x-ms-scheduler-execution-region": "North Europe", "x-ms-scheduler-expected-execution-time": "2013-11-07T02:39:27", "x-ms-scheduler-jobcollectionid": "jobcoll001", "x-ms-scheduler-jobid": "7ce6971c-5aa1-4701-b6bd-02f63ee82d17", "x-real-ip": "137.116.241.137", "x-request-start": "1383791968800", "x-something": "value123" }
var storageAccount = new CloudStorageAccount(new StorageCredentials("labdrino", ""), true); var queueClient = storageAccount.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference("scheduled-tasks"); queue.CreateIfNotExists(); var perm = new QueuePermissions(); var policy = new SharedAccessQueuePolicy { SharedAccessExpiryTime = DateTime.MaxValue, Permissions = SharedAccessQueuePermissions.Add }; perm.SharedAccessPolicies.Add("jobcoll001policy", policy); queue.SetPermissions(perm); var sas = queue.GetSharedAccessSignature(new SharedAccessQueuePolicy(), "jobcoll001policy"); var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = new JobAction() { Type = JobActionType.StorageQueue, QueueMessage = new JobQueueMessage() { Message = "hello there!", QueueName = "scheduled-tasks", SasToken = sas, StorageAccountName = "labdrino" } }, StartTime = DateTime.UtcNow, Recurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Minute, Interval = 1, Count = 5 } }); Console.WriteLine(result.RequestId); Console.WriteLine(result.StatusCode); Console.ReadLine();
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); foreach (var job in schedulerClient.Jobs.List(new JobListParameters() { State = JobState.Enabled })) { Console.WriteLine("Job: {0} - Action: {1} - State: {2} - Status: {3}", job.Id, job.Action, job.State, job.Status); foreach (var history in schedulerClient.Jobs.GetHistory(job.Id, new JobGetHistoryParameters())) { Console.WriteLine(" > {0} - {1}: {2}", history.StartTime, history.EndTime, history.Message); } } Console.ReadLine();
Job: 34851054-f576-48b8-8c77-73b62b502022 - Action: Microsoft.WindowsAzure.Scheduler.Models.JobAction - State: Faulted - Status: Microsoft.WindowsAzure.Scheduler.Models.JobStatusTask history can provide interesting information on task execution. If something goes wrong during the execution of a task, history is the first place where you should start looking for the source of the problem.
> 7/11/2013 2:52:18 - 7/11/2013 2:52:19: StorageQueue Action - The provided queue: "scheduled-tasks" to the given queue
> 7/11/2013 2:52:48 - 7/11/2013 2:52:50: StorageQueue Action - The provided queue: "scheduled-tasks" doesn’t have to be to the given queue
> 7/11/2013 2:53:19 - 7/11/2013 2:53:19: StorageQueue Action - The provided queue: "scheduled-tasks" does not exist to the given queue
> 7/11/2013 2:53:48 - 7/11/2013 2:53:50: StorageQueue Action - The provided queue: "scheduled-tasks" does not exist to the given queue
> 7/11/2013 2:54:20 - 7/11/2013 2:54:20: StorageQueue Action - The provided queue: "scheduled-tasks" does not exist to the given queue
> 7/11/2013 3:05:19 - 7/11/2013 3:05:19: StorageQueue Action - The provided queue: "scheduled-tasks" does not exist to the given queue
> 7/11/2013 3:05:49 AM - 7/11/2013 3:05:49 AM: StorageQueue Action - The provided queue: "scheduled-tasks" doesn’t have to be to the given queue
> 7/11/2013 3:06:18 - 7/11/2013 3:06:19: StorageQueue Action - The provided queue: "scheduled-tasks" doesn’t exist to the given queue
Job: 4db6da21-af4a-4703-b988-671cbb6d5fd5 - Action: Microsoft.WindowsAzure.Scheduler.Models.JobAction - State: Completed - Status: Microsoft.WindowsAzure.Scheduler.Models.JobStatus
> 7/11/2013 2:32:13 - 7/11/2013 2:32:15: Http Action - Response from host 'postcatcher.in': 'Created' Response Headers: Connection: keep-alive
X-Response-Time: 6ms
Date: Thu, 07 Nov 2013 02:32:14 GMT
Set-Cookie: connect.sid = 8SxhjZXandfZQc158Ng2tiYs.kyW9OSZGymzcIJW1eTJJ2MIACyhSyK6mfHVVqqj2r0E; path = /; expires = Thu, 07 Nov 2013 06:32:14 GMT; httpOnly
Server: nginx
X-Powered-By: Express
Body: Created
> 7/11/2013 2:33:14 - 7/11/2013 2:33:15: Http Action - Response from host 'postcatcher.in': 'Created' Response Headers: Connection: keep-alive
X-Response-Time: 18ms
Date: Thu, 07 Nov 2013 02:33:15 GMT
Set-Cookie: connect.sid = BJYkjeu3m26wBfr6G2SDgXZl.nhXEo24T3AVHEMYe4xJIm7gjDmhZvj69edIv4bui% 2Bzs; path = /; expires = Thu, 07 Nov 2013 06:33:15 GMT; httpOnly
Server: nginx
X-Powered-By: Express
Body: Created
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = new JobAction() { Type = JobActionType.Http, Request = ..., RetryPolicy = new RetryPolicy() { RetryCount = 5, RetryInterval = TimeSpan.FromMinutes(1), RetryType = RetryType.Fixed } }, StartTime = DateTime.UtcNow, Recurrence = ... });
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = new JobAction() { Type = JobActionType.StorageQueue, QueueMessage = new JobQueueMessage() { Message = "hello there!", QueueName = "scheduled-tasks", SasToken = "not working", StorageAccountName = "labdrino" }, ErrorAction = new JobErrorAction() { Type = JobActionType.Http, Request = new JobHttpRequest() { Uri = new Uri("http://postcatcher.in/catchers/527b0b75fe325802000002b6"), Body = "type=somethingiswrong", Headers = new Dictionary() { { "Content-Type", "application/x-www-form-urlencoded" }, { "x-something", "value123" } }, Method = "POST" } } }, StartTime = DateTime.UtcNow, Recurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Minute, Interval = 1, Count = 5 } }); Console.WriteLine(result.RequestId); Console.WriteLine(result.StatusCode); Console.ReadLine();
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = ..., Recurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Day, Interval = 1, Count = 10 } });
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = ..., Recurrence = new JobRecurrence() { Frequency = JobRecurrenceFrequency.Day, Interval = 1, EndTime = new DateTime(2013, 12, 31) } });
var schedulerClient = new SchedulerClient(credentials, "sandrino-cs2", "jobcoll001"); var result = schedulerClient.Jobs.Create(new JobCreateParameters() { Action = new JobAction() { Type = JobActionType.Http, Request = new JobHttpRequest() { Body = "customers=Europe-West", Headers = new Dictionary() { { "Content-Type", "application/x-www-form-urlencoded" }, }, Method = "POST", Uri = new Uri("http://postcatcher.in/catchers/527af9acfe325802000001cb") } }, StartTime = DateTime.UtcNow, Recurrence = new JobRecurrence() { // Frequency = JobRecurrenceFrequency.None, Schedule = new JobRecurrenceSchedule() { Days = new List() { JobScheduleDay.Monday }, Hours = new List() { 9 }, Minutes = new List() { 11 } } } });
Source: https://habr.com/ru/post/201840/