public async Task CreateJobAsync(CreateJobOptions createJobOptions) { CloudJob unboundJob = this.Client.JobOperations.CreateJob(); unboundJob.Id = createJobOptions.JobId; unboundJob.Priority = createJobOptions.Priority; unboundJob.Constraints = new JobConstraints(createJobOptions.MaxWallClockTime, createJobOptions.MaxRetryCount); PoolInformation poolInformation = new PoolInformation(); if (createJobOptions.AutoPoolOptions.UseAutoPool.HasValue && createJobOptions.AutoPoolOptions.UseAutoPool.Value) { AutoPoolSpecification autoPoolSpecification = new AutoPoolSpecification() { AutoPoolIdPrefix = createJobOptions.AutoPoolOptions.AutoPoolPrefix, KeepAlive = createJobOptions.AutoPoolOptions.KeepAlive, PoolLifetimeOption = (PoolLifetimeOption)Enum.Parse(typeof(PoolLifetimeOption), createJobOptions.AutoPoolOptions.LifeTimeOption), PoolSpecification = new PoolSpecification() { CloudServiceConfiguration = new CloudServiceConfiguration(createJobOptions.AutoPoolOptions.OSFamily), VirtualMachineSize = createJobOptions.AutoPoolOptions.VirutalMachineSize, TargetDedicatedComputeNodes = createJobOptions.AutoPoolOptions.TargetDedicated, TargetLowPriorityComputeNodes = createJobOptions.AutoPoolOptions.TargetLowPriority } }; poolInformation.AutoPoolSpecification = autoPoolSpecification; } else { poolInformation.PoolId = createJobOptions.PoolId; } unboundJob.PoolInformation = poolInformation; if (createJobOptions.CreateJobManager.HasValue && createJobOptions.CreateJobManager.Value == true) { JobManagerTask jobManager = new JobManagerTask() { CommandLine = createJobOptions.JobManagerOptions.CommandLine, KillJobOnCompletion = createJobOptions.JobManagerOptions.KillOnCompletion, Id = createJobOptions.JobManagerOptions.JobManagerId }; jobManager.Constraints = new TaskConstraints( createJobOptions.JobManagerOptions.MaxTaskWallClockTime, createJobOptions.JobManagerOptions.RetentionTime, createJobOptions.JobManagerOptions.MaxTaskRetryCount); unboundJob.JobManagerTask = jobManager; } await unboundJob.CommitAsync(); }
/// <summary> /// Creates a job schedule with the specified options. /// </summary> /// <param name="options">The options describing the job schedule to create.</param> /// <returns></returns> public async Task CreateJobScheduleAsync(CreateJobScheduleOptions options) { CloudJobSchedule unboundJobSchedule = this.Client.JobScheduleOperations.CreateJobSchedule(); unboundJobSchedule.Id = options.JobScheduleId; PoolInformation poolInformation = new PoolInformation(); if (options.AutoPoolOptions.UseAutoPool.HasValue && options.AutoPoolOptions.UseAutoPool.Value) { AutoPoolSpecification autoPoolSpecification = new AutoPoolSpecification() { AutoPoolIdPrefix = options.AutoPoolOptions.AutoPoolPrefix, KeepAlive = options.AutoPoolOptions.KeepAlive, PoolLifetimeOption = (PoolLifetimeOption)Enum.Parse(typeof(PoolLifetimeOption), options.AutoPoolOptions.LifeTimeOption), PoolSpecification = new PoolSpecification() { CloudServiceConfiguration = new CloudServiceConfiguration(options.AutoPoolOptions.OSFamily), VirtualMachineSize = options.AutoPoolOptions.VirutalMachineSize, TargetDedicated = options.AutoPoolOptions.TargetDedicated } }; poolInformation.AutoPoolSpecification = autoPoolSpecification; } else { poolInformation.PoolId = options.PoolId; } unboundJobSchedule.JobSpecification = new JobSpecification() { Priority = options.Priority, PoolInformation = poolInformation }; // TODO: These are read only unboundJobSchedule.JobSpecification.Constraints = new JobConstraints(options.MaxWallClockTime, options.MaxRetryCount); Schedule schedule = new Schedule() { DoNotRunAfter = options.DoNotRunAfter, DoNotRunUntil = options.DoNotRunUntil, RecurrenceInterval = options.RecurrenceInterval, StartWindow = options.StartWindow }; unboundJobSchedule.Schedule = schedule; if (options.CreateJobManager.HasValue && options.CreateJobManager.Value == true) { JobManagerTask jobManager = new JobManagerTask() { CommandLine = options.JobManagerOptions.CommandLine, KillJobOnCompletion = options.JobManagerOptions.KillOnCompletion, Id = options.JobManagerOptions.JobManagerId }; jobManager.Constraints = new TaskConstraints(options.JobManagerOptions.MaxTaskWallClockTime, options.JobManagerOptions.RetentionTime, options.JobManagerOptions.MaxTaskRetryCount); unboundJobSchedule.JobSpecification.JobManagerTask = jobManager; } await unboundJobSchedule.CommitAsync(); }
/// <summary> /// Creates a job schedule with the specified options. /// </summary> /// <param name="options">The options describing the job schedule to create.</param> /// <returns></returns> public async Task CreateJobScheduleAsync(CreateJobScheduleOptions options) { CloudJobSchedule unboundJobSchedule = this.Client.JobScheduleOperations.CreateJobSchedule(); unboundJobSchedule.Id = options.JobScheduleId; PoolInformation poolInformation = new PoolInformation(); if (options.AutoPoolOptions.UseAutoPool.HasValue && options.AutoPoolOptions.UseAutoPool.Value) { AutoPoolSpecification autoPoolSpecification = new AutoPoolSpecification() { AutoPoolIdPrefix = options.AutoPoolOptions.AutoPoolPrefix, KeepAlive = options.AutoPoolOptions.KeepAlive, PoolLifetimeOption = (PoolLifetimeOption)Enum.Parse(typeof(PoolLifetimeOption), options.AutoPoolOptions.LifeTimeOption), PoolSpecification = new PoolSpecification() { OSFamily = options.AutoPoolOptions.OSFamily, VirtualMachineSize = options.AutoPoolOptions.VirutalMachineSize, TargetDedicated = options.AutoPoolOptions.TargetDedicated } }; poolInformation.AutoPoolSpecification = autoPoolSpecification; } else { poolInformation.PoolId = options.PoolId; } unboundJobSchedule.JobSpecification = new JobSpecification() { Priority = options.Priority, PoolInformation = poolInformation }; // TODO: These are read only unboundJobSchedule.JobSpecification.Constraints = new JobConstraints(options.MaxWallClockTime, options.MaxRetryCount); Schedule schedule = new Schedule() { DoNotRunAfter = options.DoNotRunAfter, DoNotRunUntil = options.DoNotRunUntil, RecurrenceInterval = options.RecurrenceInterval, StartWindow = options.StartWindow }; unboundJobSchedule.Schedule = schedule; if (options.CreateJobManager.HasValue && options.CreateJobManager.Value == true) { JobManagerTask jobManager = new JobManagerTask() { CommandLine = options.JobManagerOptions.CommandLine, KillJobOnCompletion = options.JobManagerOptions.KillOnCompletion, Id = options.JobManagerOptions.JobManagerId }; jobManager.Constraints = new TaskConstraints(options.JobManagerOptions.MaxTaskWallClockTime, options.JobManagerOptions.RetentionTime, options.JobManagerOptions.MaxTaskRetryCount); unboundJobSchedule.JobSpecification.JobManagerTask = jobManager; } await unboundJobSchedule.CommitAsync(); }
public async Task CreateJobAsync(CreateJobOptions createJobOptions) { CloudJob unboundJob = this.Client.JobOperations.CreateJob(); unboundJob.Id = createJobOptions.JobId; unboundJob.Priority = createJobOptions.Priority; unboundJob.Constraints = new JobConstraints(createJobOptions.MaxWallClockTime, createJobOptions.MaxRetryCount); PoolInformation poolInformation = new PoolInformation(); if (createJobOptions.AutoPoolOptions.UseAutoPool.HasValue && createJobOptions.AutoPoolOptions.UseAutoPool.Value) { AutoPoolSpecification autoPoolSpecification = new AutoPoolSpecification() { AutoPoolIdPrefix = createJobOptions.AutoPoolOptions.AutoPoolPrefix, KeepAlive = createJobOptions.AutoPoolOptions.KeepAlive, PoolLifetimeOption = (PoolLifetimeOption)Enum.Parse(typeof(PoolLifetimeOption), createJobOptions.AutoPoolOptions.LifeTimeOption), PoolSpecification = new PoolSpecification() { OSFamily = createJobOptions.AutoPoolOptions.OSFamily, VirtualMachineSize = createJobOptions.AutoPoolOptions.VirutalMachineSize, TargetDedicated = createJobOptions.AutoPoolOptions.TargetDedicated } }; poolInformation.AutoPoolSpecification = autoPoolSpecification; } else { poolInformation.PoolId = createJobOptions.PoolId; } unboundJob.PoolInformation = poolInformation; if (createJobOptions.CreateJobManager.HasValue && createJobOptions.CreateJobManager.Value == true) { JobManagerTask jobManager = new JobManagerTask() { CommandLine = createJobOptions.JobManagerOptions.CommandLine, KillJobOnCompletion = createJobOptions.JobManagerOptions.KillOnCompletion, Id = createJobOptions.JobManagerOptions.JobManagerId }; jobManager.Constraints = new TaskConstraints( createJobOptions.JobManagerOptions.MaxTaskWallClockTime, createJobOptions.JobManagerOptions.RetentionTime, createJobOptions.JobManagerOptions.MaxTaskRetryCount); unboundJob.JobManagerTask = jobManager; } await unboundJob.CommitAsync(); }
public static void DisplayJobScheduleLong(ITestOutputHelper testOutputHelper, CloudJobSchedule curWI) { // job schedule top level simple properties testOutputHelper.WriteLine("Id: " + curWI.Id); testOutputHelper.WriteLine(" State: " + curWI.State.ToString()); testOutputHelper.WriteLine(" " + "URL: " + curWI.Url); testOutputHelper.WriteLine(" " + "LastModified: " + (curWI.LastModified.HasValue ? curWI.LastModified.Value.ToLongDateString() : "<null>")); // execution INFO { JobScheduleExecutionInformation wiExInfo = curWI.ExecutionInformation; testOutputHelper.WriteLine(" ExeInfo:"); testOutputHelper.WriteLine(" LastUpdateTime: " + (wiExInfo.EndTime.HasValue ? wiExInfo.EndTime.Value.ToLongDateString() : "<null>")); testOutputHelper.WriteLine(" NextRuntime: " + (wiExInfo.NextRunTime.HasValue ? wiExInfo.NextRunTime.Value.ToLongDateString() : "<null>")); testOutputHelper.WriteLine(" RecentJob:"); // RecentJob RecentJob rj = wiExInfo.RecentJob; if (null == rj) { testOutputHelper.WriteLine(" <null>"); } else { testOutputHelper.WriteLine(" Id: " + rj.Id); testOutputHelper.WriteLine(" Url: " + rj.Url); } } // JobSpecification JobSpecification jobSpec = curWI.JobSpecification; testOutputHelper.WriteLine(" JobSpecification:"); if (null == jobSpec) { testOutputHelper.WriteLine(" <null>"); } else { testOutputHelper.WriteLine(""); testOutputHelper.WriteLine(" Priority: " + (jobSpec.Priority.HasValue ? jobSpec.Priority.ToString() : "<null>")); JobConstraints jobCon = jobSpec.Constraints; testOutputHelper.WriteLine(" Constraints: "); if (null == jobCon) { testOutputHelper.WriteLine("null"); } else { testOutputHelper.WriteLine(""); testOutputHelper.WriteLine(" MaxTaskRetryCount: " + (jobCon.MaxTaskRetryCount.HasValue ? jobSpec.Constraints.MaxTaskRetryCount.Value.ToString() : "<null>")); testOutputHelper.WriteLine(" MaxWallClockTime: " + (jobCon.MaxWallClockTime.HasValue ? jobSpec.Constraints.MaxWallClockTime.Value.TotalMilliseconds.ToString() : "<null>")); } JobManagerTask ijm = jobSpec.JobManagerTask; if (null == ijm) { testOutputHelper.WriteLine("<null>"); } else { testOutputHelper.WriteLine(" JobManagerTask:"); testOutputHelper.WriteLine(" CommandLine : " + ijm.CommandLine); testOutputHelper.WriteLine(" KillJobOnCompletion: " + (ijm.KillJobOnCompletion.HasValue ? ijm.KillJobOnCompletion.Value.ToString() : "<null>")); testOutputHelper.WriteLine(" Id : " + ijm.Id); testOutputHelper.WriteLine(" RunExclusive : " + (ijm.RunExclusive.HasValue ? ijm.RunExclusive.Value.ToString() : "<null>")); IEnumerable <EnvironmentSetting> envSettings = ijm.EnvironmentSettings; if (null != envSettings) { List <EnvironmentSetting> envSettingsList = new List <EnvironmentSetting>(ijm.EnvironmentSettings); testOutputHelper.WriteLine(" EnvironmentSettings.count:" + envSettingsList.Count); } else { testOutputHelper.WriteLine(" EnvironmentSettings: <null>"); } IEnumerable <ResourceFile> resFilesProp = ijm.ResourceFiles; if (null != resFilesProp) { List <ResourceFile> resFiles = new List <ResourceFile>(); testOutputHelper.WriteLine(" ResourceFiles.count:" + resFiles.Count); } else { testOutputHelper.WriteLine(" ResourceFiles: <null>"); } TaskConstraints tc = ijm.Constraints; if (null == tc) { testOutputHelper.WriteLine(" TaskConstraints: <null>"); } else { testOutputHelper.WriteLine(" TaskConstraints: "); testOutputHelper.WriteLine(" MaxTaskRetryCount: " + (tc.MaxTaskRetryCount.HasValue ? tc.MaxTaskRetryCount.Value.ToString() : "<null>")); testOutputHelper.WriteLine(" MaxWallClockTime: " + (tc.MaxWallClockTime.HasValue ? tc.MaxWallClockTime.Value.TotalMilliseconds.ToString() : "<null>")); testOutputHelper.WriteLine(" RetentionTime: " + (tc.RetentionTime.HasValue ? tc.RetentionTime.Value.TotalMilliseconds.ToString() : "<null>")); } if (ijm.UserIdentity != null) { testOutputHelper.WriteLine(" UserIdentity: "); testOutputHelper.WriteLine(" UserName: "******" ElevationLevel: ", ijm.UserIdentity.AutoUser?.ElevationLevel); testOutputHelper.WriteLine(" Scope: ", ijm.UserIdentity.AutoUser?.Scope); } } } // metadata { IEnumerable <MetadataItem> mdis = curWI.Metadata; testOutputHelper.WriteLine(" Metadata: "); if (null == mdis) { testOutputHelper.WriteLine("<null>"); } else { List <MetadataItem> meta = new List <MetadataItem>(curWI.Metadata); testOutputHelper.WriteLine(" count:" + meta.Count); } } // schedule Schedule sched = curWI.Schedule; if (null == sched) { testOutputHelper.WriteLine(" Schedule: <null>"); } else { testOutputHelper.WriteLine(" Schedule:"); testOutputHelper.WriteLine(" DoNotRunAfter:" + (sched.DoNotRunAfter.HasValue ? sched.DoNotRunAfter.Value.ToLongDateString() : "<null>")); testOutputHelper.WriteLine(" DoNotRunUntil: " + (sched.DoNotRunUntil.HasValue ? sched.DoNotRunUntil.Value.ToLongDateString() : "<null>")); testOutputHelper.WriteLine(" RecurrenceInterval: " + (sched.RecurrenceInterval.HasValue ? sched.RecurrenceInterval.Value.TotalMilliseconds.ToString() : "<null>")); testOutputHelper.WriteLine(" StartWindow :" + (sched.StartWindow.HasValue ? sched.StartWindow.Value.TotalMilliseconds.ToString() : "<null>")); } // stats JobScheduleStatistics stats = curWI.Statistics; if (null == stats) { testOutputHelper.WriteLine(" Stats: <null>"); } else { testOutputHelper.WriteLine(" Stats:"); testOutputHelper.WriteLine(" LastUpdateTime: " + stats.LastUpdateTime.ToLongDateString()); testOutputHelper.WriteLine(" KernelCPUTime: " + stats.KernelCpuTime.TotalMilliseconds.ToString()); testOutputHelper.WriteLine(" NumFailedTasks: " + stats.FailedTaskCount.ToString()); testOutputHelper.WriteLine(" NumTimesCalled : " + stats.TaskRetryCount); testOutputHelper.WriteLine(" NumSucceededTasks: " + stats.SucceededTaskCount); testOutputHelper.WriteLine(" ReadIOGiB : " + stats.ReadIOGiB); testOutputHelper.WriteLine(" ReadIOps : " + stats.ReadIOps); testOutputHelper.WriteLine(" StartTime : " + stats.StartTime.ToLongDateString()); testOutputHelper.WriteLine(" Url : " + stats.Url); testOutputHelper.WriteLine(" UserCpuTime : " + stats.UserCpuTime.TotalMilliseconds.ToString()); testOutputHelper.WriteLine(" WaitTime : " + stats.WaitTime.TotalMilliseconds.ToString()); testOutputHelper.WriteLine(" WallClockTime : " + stats.WallClockTime.TotalMilliseconds.ToString()); testOutputHelper.WriteLine(" WriteIOGiB : " + stats.WriteIOGiB); testOutputHelper.WriteLine(" WriteIOps : " + stats.WriteIOps); } }
public void SampleCreateJobScheduleAutoPool() { Action test = () => { using (BatchClient batchCli = TestUtilities.OpenBatchClientAsync(TestUtilities.GetCredentialsFromEnvironment()).Result) { string jsId = Microsoft.Azure.Batch.Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-CreateWiAutoPoolTest"; try { CloudJobSchedule newJobSchedule = batchCli.JobScheduleOperations.CreateJobSchedule(jsId, null, null); { newJobSchedule.Metadata = MakeMetaData("onCreateName", "onCreateValue"); PoolInformation poolInformation = new PoolInformation(); AutoPoolSpecification iaps = new AutoPoolSpecification(); Schedule schedule = new Schedule() { RecurrenceInterval = TimeSpan.FromMinutes(18) }; poolInformation.AutoPoolSpecification = iaps; iaps.AutoPoolIdPrefix = Microsoft.Azure.Batch.Constants.DefaultConveniencePrefix + TestUtilities.GetMyName(); iaps.PoolLifetimeOption = Microsoft.Azure.Batch.Common.PoolLifetimeOption.Job; iaps.KeepAlive = false; PoolSpecification ps = new PoolSpecification(); iaps.PoolSpecification = ps; ps.TargetDedicated = 1; ps.VirtualMachineSize = PoolFixture.VMSize; ps.CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily); ps.Metadata = MakeMetaData("pusMDIName", "pusMDIValue"); JobSpecification jobSpec = newJobSchedule.JobSpecification; Assert.Null(jobSpec); jobSpec = new JobSpecification(poolInformation); JobManagerTask jobMgr = jobSpec.JobManagerTask; Assert.Null(jobMgr); jobMgr = new JobManagerTask(TestUtilities.GetMyName() + "-JobManagerTest", "hostname"); jobMgr.KillJobOnCompletion = false; // set the JobManagerTask on the JobSpecification jobSpec.JobManagerTask = jobMgr; // set the JobSpecifcation on the Job Schedule newJobSchedule.JobSpecification = jobSpec; newJobSchedule.Schedule = schedule; newJobSchedule.Commit(); } CloudJobSchedule jobSchedule = batchCli.JobScheduleOperations.GetJobSchedule(jsId); { TestUtilities.DisplayJobScheduleLong(this.testOutputHelper, jobSchedule); List <MetadataItem> mdi = new List <MetadataItem>(jobSchedule.Metadata); // check the values specified for AddJobSchedule are correct. foreach (MetadataItem curIMDI in mdi) { Assert.Equal("onCreateName", curIMDI.Name); Assert.Equal("onCreateValue", curIMDI.Value); } // add metadata items mdi.Add(new MetadataItem("modifiedName", "modifiedValue")); jobSchedule.Metadata = mdi; jobSchedule.Commit(); // confirm metadata updated correctly CloudJobSchedule jsUpdated = batchCli.JobScheduleOperations.GetJobSchedule(jsId); { List <MetadataItem> updatedMDI = new List <MetadataItem>(jsUpdated.Metadata); Assert.Equal(2, updatedMDI.Count); Assert.Equal("onCreateName", updatedMDI[0].Name); Assert.Equal("onCreateValue", updatedMDI[0].Value); Assert.Equal("modifiedName", updatedMDI[1].Name); Assert.Equal("modifiedValue", updatedMDI[1].Value); } jobSchedule.Refresh(); TestUtilities.DisplayJobScheduleLong(this.testOutputHelper, jobSchedule); } } finally { // clean up TestUtilities.DeleteJobScheduleIfExistsAsync(batchCli, jsId).Wait(); } } }; SynchronizationContextHelper.RunTest(test, TestTimeout); }
static void Main(string[] args) { // Create the virtual machine image reference ImageReference imageReference = new ImageReference( publisher: "MicrosoftWindowsServer", offer: "WindowsServer", sku: "2016-datacenter-smalldisk", version: "latest"); // Create the virtual machine configuration for the pool VirtualMachineConfiguration virtualMachineConfiguration = new VirtualMachineConfiguration( imageReference: imageReference, nodeAgentSkuId: "batch.node.windows amd64"); BatchSharedKeyCredentials cred = new BatchSharedKeyCredentials(BatchAccountUrl, BatchAccountName, BatchAccountKey); using (BatchClient batchClient = BatchClient.Open(cred)) { Console.WriteLine("Creating pool [{0}]...", PoolId); try { CloudPool pool = batchClient.PoolOperations.CreatePool( poolId: PoolId, targetDedicatedComputeNodes: PoolNodeCount, virtualMachineSize: PoolVMSize, virtualMachineConfiguration: virtualMachineConfiguration); // Specify the application packages for the tasks pool.ApplicationPackageReferences = new List <ApplicationPackageReference> { new ApplicationPackageReference { ApplicationId = "QueueProcessingJobManager", Version = "1" }, new ApplicationPackageReference { ApplicationId = "ProcessMessage", Version = "1" }, new ApplicationPackageReference { ApplicationId = "ReadWriteFile", Version = "1" }, }; pool.MaxTasksPerComputeNode = 4; pool.Commit(); } catch (BatchException be) { // Accept the specific error code PoolExists as that is expected if the pool already exists if (be.RequestInformation?.BatchError?.Code == BatchErrorCodeStrings.PoolExists) { Console.WriteLine("The pool {0} already existed when we tried to create it", PoolId); } else { throw; // Any other exception is unexpected } } Console.WriteLine("Creating job [{0}]...", JobId); try { CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = $"{JobId}{Guid.NewGuid()}"; job.PoolInformation = new PoolInformation { PoolId = PoolId }; // *** Add Job Manager Task *** JobManagerTask jobManagerTask = new JobManagerTask() { Id = String.Format("TaskJobManager{0}", Guid.NewGuid()), CommandLine = string.Format("cmd /c %AZ_BATCH_APP_PACKAGE_QUEUEPROCESSINGJOBMANAGER%\\QueueProcessingJobManager.exe {0}", job.Id), DisplayName = "Queue Processing Job Manager" }; job.JobManagerTask = jobManagerTask; job.Commit(); } catch (BatchException be) { // Accept the specific error code JobExists as that is expected if the job already exists if (be.RequestInformation?.BatchError?.Code == BatchErrorCodeStrings.JobExists) { Console.WriteLine("The job {0} already existed when we tried to create it", JobId); } else { throw; // Any other exception is unexpected } } } }