/// <summary>
        /// Create a Job Collection (if not exists, otheriwse, returns null)
        /// </summary>
        /// <param name="cloudServiceName"></param>
        /// <param name="jobCollectionName"></param>
        public async Task <SchedulerOperationStatusResponse> CreateJobCollectionIfNotExistsAsync(string cloudServiceName, string jobCollectionName)
        {
            // Check name availability
            var availabilityResponse = await SchedulerManagementClient.JobCollections.CheckNameAvailabilityAsync(cloudServiceName, jobCollectionName);

            if (availabilityResponse.IsAvailable)
            {
                var jobCollectionParameters = new JobCollectionCreateParameters
                {
                    Label             = jobCollectionName,
                    IntrinsicSettings = new JobCollectionIntrinsicSettings()
                    {
                        Plan  = JobCollectionPlan.Standard,
                        Quota = new JobCollectionQuota()
                        {
                            MaxJobCount   = 50,
                            MaxRecurrence = new JobCollectionMaxRecurrence()
                            {
                                Frequency = JobCollectionRecurrenceFrequency.Minute,
                                Interval  = 1
                            }
                        }
                    }
                };

                return(await SchedulerManagementClient.JobCollections.CreateAsync(cloudServiceName, jobCollectionName, jobCollectionParameters));
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        private static void CreateJobCollection()
        {
            if (JobCollectionAlreadyExists())
            {
                return;
            }

            var schedulerManagementClient = new SchedulerManagementClient(Credentials);

            var eventJobCollection = new JobCollectionCreateParameters
            {
                Label             = JobsCollectionLabel,
                IntrinsicSettings = new JobCollectionIntrinsicSettings
                {
                    Plan  = JobCollectionPlan.Standard,
                    Quota = new JobCollectionQuota
                    {
                        MaxJobCount      = 50,
                        MaxJobOccurrence = 5,
                        MaxRecurrence    = new JobCollectionMaxRecurrence
                        {
                            Frequency = JobCollectionRecurrenceFrequency.Minute,
                            Interval  = 5
                        }
                    }
                }
            };

            var jobCollectionCreationResult = schedulerManagementClient.JobCollections.Create(JobsCloudServiceName, JobsCollectionName, eventJobCollection);

            if (jobCollectionCreationResult.StatusCode != HttpStatusCode.OK)
            {
                Logger.Error(new JobSchedulerException(jobCollectionCreationResult));
            }
        }
Beispiel #3
0
        public override void ExecuteCommand()
        {
            var cred   = new CertificateCloudCredentials(SubscriptionId, ManagementCertificate);
            var schman = CloudContext.Clients.CreateSchedulerManagementClient(cred);

            var createParams = new JobCollectionCreateParameters()
            {
                Label             = Label,
                IntrinsicSettings = new JobCollectionIntrinsicSettings()
                {
                    Plan  = Plan,
                    Quota = new JobCollectionQuota()
                    {
                        MaxJobCount      = MaxJobCount,
                        MaxJobOccurrence = MaxJobOccurrence,
                        MaxRecurrence    = MaxRecurrenceFrequency.HasValue ? new JobCollectionMaxRecurrence()
                        {
                            Frequency = MaxRecurrenceFrequency.Value,
                            Interval  = MaxRecurrenceInterval.Value
                        } : null
                    }
                }
            };

            if (WhatIf)
            {
                Log.Info("Would create job collection {0} in {1} with the following params:", Arguments[0], CloudService);
                Log.Info(JsonConvert.SerializeObject(createParams, new StringEnumConverter()));
            }
            else
            {
                Log.Info("Creating job collection {0} in {1}...", Arguments[0], CloudService);
                schman.JobCollections.CreateAsync(
                    CloudService,
                    Arguments[0],
                    createParams,
                    CancellationToken.None).Wait();
            }
        }
 /// <summary>
 /// Create a job collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.IJobCollectionOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The name of the cloud service containing the job
 /// collection.
 /// </param>
 /// <param name='jobCollectionName'>
 /// Required. The name of the job collection to create.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Create Job Collection
 /// operation.
 /// </param>
 /// <returns>
 /// The Create Job Collection operation response.
 /// </returns>
 public static Task <JobCollectionCreateResponse> BeginCreatingAsync(this IJobCollectionOperations operations, string cloudServiceName, string jobCollectionName, JobCollectionCreateParameters parameters)
 {
     return(operations.BeginCreatingAsync(cloudServiceName, jobCollectionName, parameters, CancellationToken.None));
 }
 /// <summary>
 /// Create a job collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.IJobCollectionOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The name of the cloud service containing the job
 /// collection.
 /// </param>
 /// <param name='jobCollectionName'>
 /// Required. The name of the job collection to create.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Create Job Collection
 /// operation.
 /// </param>
 /// <returns>
 /// The Create Job Collection operation response.
 /// </returns>
 public static JobCollectionCreateResponse BeginCreating(this IJobCollectionOperations operations, string cloudServiceName, string jobCollectionName, JobCollectionCreateParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IJobCollectionOperations)s).BeginCreatingAsync(cloudServiceName, jobCollectionName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
 /// <summary>
 /// Create a job collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.IJobCollectionOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// Required. The name of the cloud service containing the job
 /// collection.
 /// </param>
 /// <param name='jobCollectionName'>
 /// Required. The name of the job collection to create.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the Create Job Collection
 /// operation.
 /// </param>
 /// <returns>
 /// The response body contains the status of the specified asynchronous
 /// operation, indicating whether it has succeeded, is inprogress, or
 /// has failed. Note that this status is distinct from the HTTP status
 /// code returned for the Get Operation Status operation itself.  If
 /// the asynchronous operation succeeded, the response body includes
 /// the HTTP status code for the successful request.  If the
 /// asynchronous operation failed, the response body includes the HTTP
 /// status code for the failed request, and also includes error
 /// information regarding the failure.
 /// </returns>
 public static Task <SchedulerOperationStatusResponse> CreateAsync(this IJobCollectionOperations operations, string cloudServiceName, string jobCollectionName, JobCollectionCreateParameters parameters)
 {
     return(operations.CreateAsync(cloudServiceName, jobCollectionName, parameters, CancellationToken.None));
 }
        /// <summary>
        /// Creates a new Scheduler Job Collection
        /// </summary>
        /// <param name="jobCollectionRequest">Request values</param>
        /// <param name="status">Status of create action</param>
        /// <returns>Created Scheduler Job Collection</returns>
        public PSJobCollection CreateJobCollection(PSCreateJobCollectionParams jobCollectionRequest, out string status)
        {
            if (!this.AvailableRegions.Contains(jobCollectionRequest.Region, StringComparer.OrdinalIgnoreCase))
            {
                throw new Exception(Resources.SchedulerInvalidLocation);
            }

            //Only one free job collection can exist in a subscription
            if (this.HasFreeJobCollections(jobCollectionRequest.Region) && !string.IsNullOrEmpty(jobCollectionRequest.JobCollectionPlan) && jobCollectionRequest.JobCollectionPlan.Equals("Free"))
            {
                throw new Exception(Resources.SchedulerNoMoreFreeJobCollection);
            }

            //Job collection name should be unique in a region
            if (this.HasJobCollection(jobCollectionRequest.Region, jobCollectionRequest.JobCollectionName))
            {
                throw new Exception(string.Format(Resources.SchedulerExistingJobCollection, jobCollectionRequest.JobCollectionName, jobCollectionRequest.Region));
            }

            //Create cloud service, if not available
            if (!IsCloudServiceCreated(jobCollectionRequest.Region))
            {
                csmClient.CloudServices.Create(
                    cloudServiceName: jobCollectionRequest.Region.ToCloudServiceName(),
                    parameters: new CloudServiceCreateParameters
                {
                    Description = "Cloud service created by scheduler powershell",
                    GeoRegion   = jobCollectionRequest.Region,
                    Label       = "Cloud service created by scheduler powershell"
                });
            }

            JobCollectionCreateParameters jobCollectionCreateParams = new JobCollectionCreateParameters
            {
                IntrinsicSettings = new JobCollectionIntrinsicSettings
                {
                    Plan = !string.IsNullOrWhiteSpace(jobCollectionRequest.JobCollectionPlan) ?
                           (JobCollectionPlan)Enum.Parse(typeof(JobCollectionPlan), jobCollectionRequest.JobCollectionPlan) : JobCollectionPlan.Standard
                }
            };

            switch (jobCollectionCreateParams.IntrinsicSettings.Plan)
            {
            case JobCollectionPlan.Free:
                jobCollectionCreateParams.IntrinsicSettings.Quota = new JobCollectionQuota
                {
                    MaxJobCount   = jobCollectionRequest.MaxJobCount.HasValue ? jobCollectionRequest.MaxJobCount : this.FreeMaxJobCountValue,
                    MaxRecurrence = new JobCollectionMaxRecurrence
                    {
                        Interval  = jobCollectionRequest.MaxJobInterval.HasValue ? jobCollectionRequest.MaxJobInterval.Value : this.FreeMinRecurrenceValue.GetInterval(),
                        Frequency = !string.IsNullOrWhiteSpace(jobCollectionRequest.MaxJobFrequency)
                                ? (JobCollectionRecurrenceFrequency)Enum.Parse(typeof(JobCollectionRecurrenceFrequency), jobCollectionRequest.MaxJobFrequency)
                                : this.FreeMinRecurrenceValue.GetFrequency()
                    }
                };
                break;

            case JobCollectionPlan.Standard:
            default:
                jobCollectionCreateParams.IntrinsicSettings.Quota = new JobCollectionQuota
                {
                    MaxJobCount   = jobCollectionRequest.MaxJobCount.HasValue ? jobCollectionRequest.MaxJobCount : this.StandardMaxJobCountValue,
                    MaxRecurrence = new JobCollectionMaxRecurrence
                    {
                        Interval  = jobCollectionRequest.MaxJobInterval.HasValue ? jobCollectionRequest.MaxJobInterval.Value : this.StandardMinRecurrenceValue.GetInterval(),
                        Frequency = !string.IsNullOrWhiteSpace(jobCollectionRequest.MaxJobFrequency)
                                ? (JobCollectionRecurrenceFrequency)Enum.Parse(typeof(JobCollectionRecurrenceFrequency), jobCollectionRequest.MaxJobFrequency)
                                : this.StandardMinRecurrenceValue.GetFrequency()
                    }
                };
                break;

            case JobCollectionPlan.Premium:
                jobCollectionCreateParams.IntrinsicSettings.Quota = new JobCollectionQuota
                {
                    MaxJobCount   = jobCollectionRequest.MaxJobCount.HasValue ? jobCollectionRequest.MaxJobCount : this.PremiumMaxJobCountValue,
                    MaxRecurrence = new JobCollectionMaxRecurrence
                    {
                        Interval  = jobCollectionRequest.MaxJobInterval.HasValue ? jobCollectionRequest.MaxJobInterval.Value : this.PremiumMinRecurrenceValue.GetInterval(),
                        Frequency = !string.IsNullOrWhiteSpace(jobCollectionRequest.MaxJobFrequency)
                                ? (JobCollectionRecurrenceFrequency)Enum.Parse(typeof(JobCollectionRecurrenceFrequency), jobCollectionRequest.MaxJobFrequency)
                                : this.PremiumMinRecurrenceValue.GetFrequency()
                    }
                };
                break;
            }

            SchedulerOperationStatusResponse jobCollectionCreateResponse = schedulerManagementClient.JobCollections.Create(
                cloudServiceName: jobCollectionRequest.Region.ToCloudServiceName(),
                jobCollectionName: jobCollectionRequest.JobCollectionName,
                parameters: jobCollectionCreateParams);

            status = jobCollectionCreateResponse.StatusCode.ToString().Equals("OK") ? "Job Collection has been created" : jobCollectionCreateResponse.StatusCode.ToString();

            return(GetJobCollection(jobCollectionRequest.Region, jobCollectionRequest.JobCollectionName).FirstOrDefault());
        }
        // retrieve the windows azure managment certificate from current user
        public ApprenticeShipSchedular()
        {
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadWrite);
            var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "CA4716F85EC0962E2B69BB7D626CC16B924E550D", false)[0];

            store.Close();
            //
            // create management credencials and cloud service management client
            var credentials        = new CertificateCloudCredentials("bd27a79c-de25-4097-a874-3bb35f2b926a", certificate);
            var cloudServiceMgmCli = new CloudServiceManagementClient(credentials);

            // create cloud service
            var cloudServiceCreateParameters = new CloudServiceCreateParameters
            {
                Description = "sfaarmscheduler",
                Email       = "*****@*****.**",
                GeoRegion   = "West Europe",
                Label       = "sfaarmscheduler"
            };
            var cloudService = cloudServiceMgmCli.CloudServices.Create("sfaarmscheduler", cloudServiceCreateParameters);
            // create job collection
            var schedulerMgmCli = new SchedulerManagementClient(credentials);
            var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings
            {
                //Add Credentials for X.509
                Plan  = JobCollectionPlan.Free,
                Quota = new JobCollectionQuota
                {
                    MaxJobCount      = 5,
                    MaxJobOccurrence = 1,
                    MaxRecurrence    = new JobCollectionMaxRecurrence
                    {
                        Frequency = JobCollectionRecurrenceFrequency.Hour,
                        Interval  = 1
                    }
                }
            };
            var jobCollectionCreateParameters = new JobCollectionCreateParameters
            {
                IntrinsicSettings = jobCollectionIntrinsicSettings,
                Label             = "sfajobcollection"
            };
            var jobCollectionCreateResponse =
                schedulerMgmCli.JobCollections.Create("sfaarmscheduler", "sfajobcollection", jobCollectionCreateParameters);

            var schedulerClient = new SchedulerClient("sfaarmscheduler", "sfajobcollection", credentials);
            var jobAction       = new JobAction
            {
                Type         = JobActionType.StorageQueue,
                QueueMessage = new JobQueueMessage()
                {
                    Message = "test", QueueName = "vacancysummaryqueueitem", SasToken = "?sv=2017-04-17&ss=bfqt&srt=sco&sp=rwdlacup&se=2017-09-01T21:58:06Z&st=2017-09-01T13:58:06Z&spr=https&sig=r0u1A01ytcN213lsWO47Td7DUaU7lo3aQTPNTCrHRxU%3D", StorageAccountName = "sfabetastorage"
                }
                //Request = new JobHttpRequest
                //{
                //    Uri = new Uri("http://blog.shaunxu.me"),
                //    Method = "GET"
                //}
            };
            var jobRecurrence = new JobRecurrence
            {
                Frequency = JobRecurrenceFrequency.Hour,
                Interval  = 1
            };
            var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters
            {
                Action     = jobAction,
                Recurrence = jobRecurrence
            };
            var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate("poll_blog", jobCreateOrUpdateParameters);

            var jobGetHistoryParameters = new JobGetHistoryParameters
            {
                Skip = 0,
                Top  = 100
            };
            var history = schedulerClient.Jobs.GetHistory("poll_blog", jobGetHistoryParameters);

            foreach (var action in history)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", action.Status, action.Message, action.RetryCount,
                                  action.RepeatCount, action.Timestamp);
            }
        }
Beispiel #9
0
 /// <summary>
 /// Create a job collection.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.IJobCollectionOperations.
 /// </param>
 /// <param name='cloudServiceName'>
 /// The name of the cloud service containing the job collection.
 /// </param>
 /// <param name='jobCollectionName'>
 /// The name of the job collection to create.
 /// </param>
 /// <param name='parameters'>
 /// Parameters supplied to the Create Job Collection operation.
 /// </param>
 /// <returns>
 /// The Create Job Collection operation response.
 /// </returns>
 public static JobCollectionCreateResponse BeginCreating(this IJobCollectionOperations operations, string cloudServiceName, string jobCollectionName, JobCollectionCreateParameters parameters)
 {
     try
     {
         return(operations.BeginCreatingAsync(cloudServiceName, jobCollectionName, parameters).Result);
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
Beispiel #10
0
        // retrieve the windows azure managment certificate from current user
        public Schedular()
        {
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadWrite);
            var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "<ThumbPrint>", false)[0];

            store.Close();
            //
            // create management credencials and cloud service management client
            var credentials        = new CertificateCloudCredentials("8cb918af-59d0-4b24-893c-15661d048f16", certificate);
            var cloudServiceMgmCli = new CloudServiceManagementClient(credentials);

            // create cloud service
            var cloudServiceCreateParameters = new CloudServiceCreateParameters()
            {
                Description = "schedulerdemo1",
                Email       = "microsoft.com",
                GeoRegion   = "Southeast Asia",
                Label       = "schedulerdemo1"
            };
            var cloudService = cloudServiceMgmCli.CloudServices.Create("schedulerdemo1", cloudServiceCreateParameters);
            // create job collection
            var schedulerMgmCli = new SchedulerManagementClient(credentials);
            var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings()
            {
                Plan  = JobCollectionPlan.Free,
                Quota = new JobCollectionQuota()
                {
                    MaxJobCount      = 5,
                    MaxJobOccurrence = 1,
                    MaxRecurrence    = new JobCollectionMaxRecurrence()
                    {
                        Frequency = JobCollectionRecurrenceFrequency.Hour,
                        Interval  = 1
                    }
                }
            };
            var jobCollectionCreateParameters = new JobCollectionCreateParameters()
            {
                IntrinsicSettings = jobCollectionIntrinsicSettings,
                Label             = "jc1"
            };
            var jobCollectionCreateResponse = schedulerMgmCli.JobCollections.Create("schedulerdemo1", "jc1", jobCollectionCreateParameters);

            var schedulerClient = new SchedulerClient("schedulerdemo1", "jc1", credentials);
            var jobAction       = new JobAction()
            {
                Type    = JobActionType.Http,
                Request = new JobHttpRequest()
                {
                    Uri    = new Uri("http://blog.shaunxu.me"),
                    Method = "GET"
                }
            };
            var jobRecurrence = new JobRecurrence()
            {
                Frequency = JobRecurrenceFrequency.Hour,
                Interval  = 1
            };
            var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters()
            {
                Action     = jobAction,
                Recurrence = jobRecurrence
            };
            var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate("poll_blog", jobCreateOrUpdateParameters);

            var jobGetHistoryParameters = new JobGetHistoryParameters()
            {
                Skip = 0,
                Top  = 100
            };
            var history = schedulerClient.Jobs.GetHistory("poll_blog", jobGetHistoryParameters);

            foreach (var action in history)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", action.Status, action.Message, action.RetryCount, action.RepeatCount, action.Timestamp);
            }
        }
        private static void CreateSchedulerJob(AzureDetails details)
        {
            // retrieve the windows azure managment certificate from current user
            var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadWrite);
            var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, "<THUMBPRINT of CERTIFICATE>", false)[0];

            store.Close();

            // create management credencials and cloud service management client
            var credentials        = new CertificateCloudCredentials("<Your subscription ID>", certificate);
            var cloudServiceMgmCli = new CloudServiceManagementClient(credentials);

            // create job collection
            var schedulerMgmCli = new SchedulerManagementClient(credentials);
            var jobCollectionIntrinsicSettings = new JobCollectionIntrinsicSettings()
            {
                Plan  = JobCollectionPlan.Free,
                Quota = new JobCollectionQuota()
                {
                    MaxJobCount      = 5,
                    MaxJobOccurrence = 1,
                    MaxRecurrence    = new JobCollectionMaxRecurrence()
                    {
                        Frequency = JobCollectionRecurrenceFrequency.Hour,
                        Interval  = 24
                    }
                }
            };

            var jobCollectionCreateParameters = new JobCollectionCreateParameters()
            {
                IntrinsicSettings = jobCollectionIntrinsicSettings,
                Label             = "RunBackupJob"
            };

            var jobCollectionCreateResponse = schedulerMgmCli.JobCollections.Create(details.ResourceGroup, "RunBackupJob", jobCollectionCreateParameters);
            //generate sas token
            var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));

            //Set the expiry time and permissions for the container.
            //In this case no start time is specified, so the shared access signature becomes valid immediately.
            var sasConstraints = new SharedAccessAccountPolicy
            {
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddYears(1),
                Permissions            = SharedAccessAccountPermissions.Add
                                         | SharedAccessAccountPermissions.Create
                                         | SharedAccessAccountPermissions.Delete
                                         | SharedAccessAccountPermissions.ProcessMessages
                                         | SharedAccessAccountPermissions.Read
                                         | SharedAccessAccountPermissions.Write
            };

            var token = storageAccount.GetSharedAccessSignature(sasConstraints);

            // create job
            var schedulerClient = new SchedulerClient(details.ResourceGroup, "RunBackupJob", credentials);
            var jobAction       = new JobAction()
            {
                Type         = JobActionType.StorageQueue,
                QueueMessage = new JobQueueMessage
                {
                    Message            = details.PairName,
                    QueueName          = "get-containers",
                    SasToken           = sasConstraints.ToString(),
                    StorageAccountName = "queuefunction"
                }
            };
            var jobRecurrence = new JobRecurrence()
            {
                Frequency = JobRecurrenceFrequency.Hour,
                Interval  = 24
            };
            var jobCreateOrUpdateParameters = new JobCreateOrUpdateParameters()
            {
                Action     = jobAction,
                Recurrence = jobRecurrence
            };
            var jobCreateResponse = schedulerClient.Jobs.CreateOrUpdate(details.ResourceGroup, jobCreateOrUpdateParameters);
        }