/// <summary> /// Creates a job in the specified pool. /// </summary> /// <param name="batchClient">A BatchClient object.</param> /// <param name="jobId">ID of the job to create.</param> /// <param name="poolId">ID of the CloudPool object in which to create the job.</param> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId) { Console.WriteLine("Creating job [{0}]...", jobId); CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; // Commit the Job try { await job.CommitAsync(); } catch (BatchException be) { // Catch specific error code JobExists as that is expected if the job already exists if (be.RequestInformation.BatchError.Code == BatchErrorCodeStrings.JobExists) { Console.WriteLine("Job [{0}] already exists.", jobId); Console.WriteLine("Deleting job [{0}]...", jobId); await batchClient.JobOperations.DeleteJobAsync(JobId); // try creating a job again bool tryAgain = true; while (tryAgain) { try { tryAgain = false; var n = 10; Thread.Sleep(n * 1000); Console.WriteLine($"Creating job [{jobId}] after {n} sec."); await job.CommitAsync(); } catch (BatchException innerException) { if (innerException.RequestInformation.BatchError.Code == BatchErrorCodeStrings.JobBeingDeleted) { Console.WriteLine("Job is being deleted.. Try again.."); tryAgain = true; } } } } else { // Any other exception is unexpected Shared.Logger.Error($"CreateJobAsync(): BatchException: Code - {be.RequestInformation.BatchError.Code}"); throw; } } }
public async Task Job_CanAddJobWithJobManagerAndAllowLowPriorityTrue() { Func <Task> test = async() => { using (BatchClient batchCli = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false)) { string jobId = "TestJobWithLowPriJobManager-" + TestUtilities.GetMyName(); try { PoolInformation poolInfo = new PoolInformation() { PoolId = "Fake" }; CloudJob unboundJob = batchCli.JobOperations.CreateJob(jobId, poolInfo); unboundJob.JobManagerTask = new JobManagerTask("foo", "cmd /c echo hi") { AllowLowPriorityNode = true }; await unboundJob.CommitAsync().ConfigureAwait(false); await unboundJob.RefreshAsync().ConfigureAwait(false); Assert.True(unboundJob.JobManagerTask.AllowLowPriorityNode); } finally { await TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId); } } }; await SynchronizationContextHelper.RunTestAsync(test, TestTimeout); }
/// <summary> /// Creates a job in the specified pool. /// </summary> /// <param name="batchClient">A <see cref="BatchClient"/>.</param> /// <param name="jobId">The id of the job to be created.</param> /// <param name="poolId">The id of the <see cref="CloudPool"/> in which to create the job.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId, IList <ResourceFile> resourceFiles) { Console.WriteLine("Creating job [{0}]...", jobId); CloudPool pool = batchClient.PoolOperations.GetPool(PoolId); //pool.StartTask = new StartTask //{ // CommandLine = "cmd /c (robocopy %AZ_BATCH_TASK_WORKING_DIR% %AZ_BATCH_NODE_SHARED_DIR%) ^& IF %ERRORLEVEL% LEQ 1 exit 0", // ResourceFiles = resourceFiles, // WaitForSuccess = true //}; //if (pool.CurrentDedicatedComputeNodes == 0 && pool.CurrentLowPriorityComputeNodes == 0) //{ // Console.WriteLine("We are creating nodes"); // pool.Resize(0, 2, TimeSpan.FromMinutes(30), ComputeNodeDeallocationOption.Requeue); //} CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; //job.UsesTaskDependencies = true; JobConstraints nConst = new JobConstraints(TimeSpan.FromMinutes(30), 0); job.Constraints = nConst; await job.CommitAsync(); }
public async Task ApplicationPackageReferencesOnCloudTaskAreRoundtripped() { string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-APROnCloudTaskAreRoundtripped"; string taskId = "task-id"; const string applicationId = "blender"; const string applicationVerson = "beta"; Func <Task> test = async() => { using (BatchClient client = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false)) { var poolInfo = new PoolInformation { AutoPoolSpecification = new AutoPoolSpecification { PoolSpecification = new PoolSpecification { CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily), VirtualMachineSize = PoolFixture.VMSize, }, PoolLifetimeOption = PoolLifetimeOption.Job } }; try { CloudJob job = client.JobOperations.CreateJob(jobId, poolInfo); await job.CommitAsync().ConfigureAwait(false); var boundJob = await client.JobOperations.GetJobAsync(jobId).ConfigureAwait(false); CloudTask cloudTask = new CloudTask(taskId, "cmd /c ping 127.0.0.1") { ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = applicationId, Version = applicationVerson } } }; await boundJob.AddTaskAsync(cloudTask).ConfigureAwait(false); CloudTask boundCloudTask = await boundJob.GetTaskAsync(taskId).ConfigureAwait(false); Assert.Equal(applicationId, boundCloudTask.ApplicationPackageReferences.Single().ApplicationId); Assert.Equal(applicationVerson, boundCloudTask.ApplicationPackageReferences.Single().Version); } finally { TestUtilities.DeleteJobIfExistsAsync(client, jobId).Wait(); } } }; await SynchronizationContextHelper.RunTestAsync(test, TestTimeout); }
/// <summary> /// Creates a job in the specified pool. /// </summary> /// <param name="batchClient">A <see cref="BatchClient"/>.</param> /// <param name="jobId">The id of the job to be created.</param> /// <param name="poolId">The id of the <see cref="CloudPool"/> in which to create the job.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId) { Console.WriteLine("Creating job [{0}]...", jobId); try { CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; await job.CommitAsync(); } 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 } } }
/// <summary> /// Creates a job and adds a task to it. /// </summary> /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> /// <param name="configurationSettings">The configuration settings</param> /// <param name="jobId">The ID of the job.</param> /// <returns>An asynchronous <see cref="Task"/> representing the operation.</returns> private static async Task SubmitJobAsync(BatchClient batchClient, Settings configurationSettings, string jobId) { // create an empty unbound Job CloudJob unboundJob = batchClient.JobOperations.CreateJob(); unboundJob.Id = jobId; // For this job, ask the Batch service to automatically create a pool of VMs when the job is submitted. unboundJob.PoolInformation = new PoolInformation() { AutoPoolSpecification = new AutoPoolSpecification() { AutoPoolIdPrefix = "HelloWorld", PoolSpecification = new PoolSpecification() { TargetDedicated = configurationSettings.PoolTargetNodeCount, CloudServiceConfiguration = new CloudServiceConfiguration(configurationSettings.PoolOSFamily), VirtualMachineSize = configurationSettings.PoolNodeVirtualMachineSize, }, KeepAlive = false, PoolLifetimeOption = PoolLifetimeOption.Job } }; // Commit Job to create it in the service await unboundJob.CommitAsync(); // create a simple task. Each task within a job must have a unique ID await batchClient.JobOperations.AddTaskAsync(jobId, new CloudTask("task1", "cmd /c echo Hello world from the Batch Hello world sample!")); }
static async Task test() { using BatchClient batchCli = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false); string jobId = "TestJobWithLowPriJobManager-" + TestUtilities.GetMyName(); try { PoolInformation poolInfo = new PoolInformation() { PoolId = "Fake" }; CloudJob unboundJob = batchCli.JobOperations.CreateJob(jobId, poolInfo); unboundJob.JobManagerTask = new JobManagerTask("foo", "cmd /c echo hi") { AllowLowPriorityNode = true }; await unboundJob.CommitAsync().ConfigureAwait(false); await unboundJob.RefreshAsync().ConfigureAwait(false); Assert.True(unboundJob.JobManagerTask.AllowLowPriorityNode); } finally { await TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId); } }
/// <summary> /// Creates and adds a Job to the mentioned Pool. /// </summary> /// <param name="jobId">Identifier for the Job</param> /// <param name="poolId">Identifier of the pool to which the Job should be added</param> /// <returns></returns> public async Task CreateJobAsync(string jobId, string poolId, IList <ResourceFile> resourceFiles, string outputContainerSasUrl, int numberOfTasks, int timeoutInHours) { Console.WriteLine("Creating job [{0}]...", jobId); CloudJob job = batchClient.JobOperations.CreateJob(); // Job is not created until it is commited. Therefore we can modify its properties. job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; // Mentions the pool to which the job is tied. string jobManagerID = jobId + "ManagerTask"; string jobManagerTaskCommandLine = $"cmd /c PSharpBatchJobManager.exe \"{BatchAccountName}\" \"{BatchAccountKey}\" \"{BatchAccountUrl}\" \"{jobId}\" \"{jobManagerID}\" \"{outputContainerSasUrl}\" \"{numberOfTasks}\" \"{timeoutInHours}\" "; //string jobManagerTaskCommandLine = string.Format("cmd /c PSharpBatchJobManager.exe \"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\" \"{5}\" \"{6}\" ", BatchAccountName, BatchAccountKey, BatchAccountUrl, jobId, jobManagerID, outputContainerSasUrl); job.JobManagerTask = new JobManagerTask ( id: jobManagerID, commandLine: jobManagerTaskCommandLine ); job.JobManagerTask.ResourceFiles = resourceFiles; await job.CommitAsync(); }
public async Task UnboundJobCommitAndRefreshWorks() { using (BatchClient batchClient = ClientUnitTestCommon.CreateDummyClient()) { const string id = "Bar"; const string displayName = "Baz"; var prepTask = new Protocol.Models.JobPreparationTask(id); Protocol.Models.CloudJob protoJob = new Protocol.Models.CloudJob( id: id, displayName: displayName, jobPreparationTask: prepTask); CloudJob job = batchClient.JobOperations.CreateJob(id, new PoolInformation() { PoolId = "Foo" }); await job.CommitAsync(additionalBehaviors : InterceptorFactory.CreateAddJobRequestInterceptor()); await job.RefreshAsync(additionalBehaviors : InterceptorFactory.CreateGetJobRequestInterceptor(protoJob)); Assert.Equal(id, job.Id); Assert.Equal(displayName, job.DisplayName); Assert.Null(job.PoolInformation); Assert.NotNull(job.JobPreparationTask); Assert.Equal(prepTask.Id, job.JobPreparationTask.Id); } }
/// <summary> /// Creates a job and adds a task to it. The task is a /// custom executable which has a resource file associated with it. /// </summary> /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> /// <param name="cloudStorageAccount">The storage account to upload the files to.</param> /// <param name="jobId">The ID of the job.</param> /// <returns>The set of container names containing the jobs input files.</returns> private async Task <HashSet <string> > SubmitJobAsync(BatchClient batchClient, CloudStorageAccount cloudStorageAccount, string jobId) { // create an empty unbound Job CloudJob unboundJob = batchClient.JobOperations.CreateJob(); unboundJob.Id = jobId; unboundJob.PoolInformation = new PoolInformation() { PoolId = this.poolsAndResourceFileSettings.PoolId }; // Commit Job to create it in the service await unboundJob.CommitAsync(); List <CloudTask> tasksToRun = new List <CloudTask>(); // Create a task which requires some resource files CloudTask taskWithFiles = new CloudTask("task_with_file1", SimpleTaskExe); // Set up a collection of files to be staged -- these files will be uploaded to Azure Storage // when the tasks are submitted to the Azure Batch service. taskWithFiles.FilesToStage = new List <IFileStagingProvider>(); // generate a local file in temp directory string localSampleFile = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "HelloWorld.txt"); File.WriteAllText(localSampleFile, "hello from Batch PoolsAndResourceFiles sample!"); StagingStorageAccount fileStagingStorageAccount = new StagingStorageAccount( storageAccount: this.accountSettings.StorageAccountName, storageAccountKey: this.accountSettings.StorageAccountKey, blobEndpoint: cloudStorageAccount.BlobEndpoint.ToString()); // add the files as a task dependency so they will be uploaded to storage before the task // is submitted and downloaded to the node before the task starts execution. FileToStage helloWorldFile = new FileToStage(localSampleFile, fileStagingStorageAccount); FileToStage simpleTaskFile = new FileToStage(SimpleTaskExe, fileStagingStorageAccount); // When this task is added via JobOperations.AddTaskAsync below, the FilesToStage are uploaded to storage once. // The Batch service does not automatically delete content from your storage account, so files added in this // way must be manually removed when they are no longer used. taskWithFiles.FilesToStage.Add(helloWorldFile); taskWithFiles.FilesToStage.Add(simpleTaskFile); tasksToRun.Add(taskWithFiles); var fileStagingArtifacts = new ConcurrentBag <ConcurrentDictionary <Type, IFileStagingArtifact> >(); // Use the AddTask method which takes an enumerable of tasks for best performance, as it submits up to 100 // tasks at once in a single request. If the list of tasks is N where N > 100, this will correctly parallelize // the requests and return when all N tasks have been added. await batchClient.JobOperations.AddTaskAsync(jobId, tasksToRun, fileStagingArtifacts : fileStagingArtifacts); // Extract the names of the blob containers from the file staging artifacts HashSet <string> blobContainerNames = GettingStartedCommon.ExtractBlobContainerNames(fileStagingArtifacts); return(blobContainerNames); }
/// <summary> /// Vytvoření úlohy v batch /// </summary> /// <param name="batchClient"></param> /// <param name="jobId"></param> /// <param name="poolId"></param> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId) { //Vytvoření úlohy do které bude přidána úloha pro více instancí Console.WriteLine($"Creating job [{jobId}]..."); CloudJob unboundJob = batchClient.JobOperations.CreateJob(jobId, new PoolInformation() { PoolId = poolId }); await unboundJob.CommitAsync(); }
/// <summary> /// Creates a job in Batch service with the specified id and associated with the specified pool. /// </summary> /// <param name="batchClient"></param> /// <param name="jobId"></param> /// <param name="poolId"></param> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId) { // Create the job to which the multi-instance task will be added. Console.WriteLine($"Creating job [{jobId}]..."); CloudJob unboundJob = batchClient.JobOperations.CreateJob(jobId, new PoolInformation() { PoolId = poolId }); await unboundJob.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() { 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 in the specified pool. /// </summary> /// <param name="batchClient">A BatchClient object.</param> /// <param name="jobId">ID of the job to create.</param> /// <param name="poolId">ID of the CloudPool object in which to create the job.</param> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId) { Console.WriteLine("Creating job [{0}]...", jobId); CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; await job.CommitAsync(); }
public async Task CanCreateJobAndAutoPoolWithAppPackageReferences() { var jobId = Guid.NewGuid().ToString(); const string applicationId = "blender"; Func <Task> test = async() => { using (BatchClient client = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false)) { var poolInfo = new PoolInformation { AutoPoolSpecification = new AutoPoolSpecification { PoolSpecification = new PoolSpecification { ApplicationPackageReferences = new[] { new ApplicationPackageReference { ApplicationId = applicationId, Version = PoolFixture.VMSize, } }, CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily), VirtualMachineSize = PoolFixture.VMSize, }, PoolLifetimeOption = PoolLifetimeOption.Job } }; CloudJob response = null; try { CloudJob job = client.JobOperations.CreateJob(jobId, poolInfo); await job.CommitAsync().ConfigureAwait(false); response = await client.JobOperations.GetJobAsync(jobId).ConfigureAwait(false); Assert.Equal(response.PoolInformation.AutoPoolSpecification.PoolSpecification.ApplicationPackageReferences.First().ApplicationId, applicationId); } finally { if (response != null) { TestUtilities.DeleteJobIfExistsAsync(client, jobId).Wait(); } } } }; await SynchronizationContextHelper.RunTestAsync(test, TestTimeout); }
private async Task SubmitJobAsync(BatchClient batchClient, string jobId, string poolId) { //create an empty unbound Job CloudJob unboundJob = batchClient.JobOperations.CreateJob(); unboundJob.Id = jobId; unboundJob.PoolInformation = new PoolInformation { PoolId = poolId }; //Commit Job to create it in the service await unboundJob.CommitAsync(); }
private static async Task JobCreation(BatchClient p_batchClient, string p_jobId, string p_poolId) { Console.WriteLine("Creating the job"); CloudJob demo_job = p_batchClient.JobOperations.CreateJob(); demo_job.Id = p_jobId; demo_job.PoolInformation = new PoolInformation { PoolId = p_poolId }; await demo_job.CommitAsync(); }
public async Task Job_GetTaskCounts_ReturnsCorrectCountNonZeroTaskSlots() { Func <Task> test = async() => { using (BatchClient batchCli = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false)) { string jobId = "NonZeroTaskSlots-" + TestUtilities.GetMyName(); try { PoolInformation poolInfo = new PoolInformation() { PoolId = "Fake" }; CloudJob unboundJob = batchCli.JobOperations.CreateJob(jobId, poolInfo); await unboundJob.CommitAsync().ConfigureAwait(false); await unboundJob.RefreshAsync().ConfigureAwait(false); CloudTask t1 = new CloudTask("t1", "cmd /c dir"); t1.RequiredSlots = 2; CloudTask t2 = new CloudTask("t2", "cmd /c ping 127.0.0.1 -n 4"); t2.RequiredSlots = 3; await unboundJob.AddTaskAsync(new[] { t1, t2 }).ConfigureAwait(false); await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false); // Give the service some time to get the counts var counts = await unboundJob.GetTaskCountsAsync().ConfigureAwait(false); var retTask1 = await unboundJob.GetTaskAsync(t1.Id); Assert.Equal(t1.RequiredSlots, retTask1.RequiredSlots); var retTask2 = await unboundJob.GetTaskAsync(t1.Id); Assert.Equal(t1.RequiredSlots, retTask2.RequiredSlots); Assert.Equal(2, counts.TaskCounts.Active); // Task slots counts is currently broken // Assert.Equal(5, counts.TaskSlotCounts.Active); } finally { await TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId); } } }; await SynchronizationContextHelper.RunTestAsync(test, TestTimeout); }
/// <summary> /// Creates a job in the specified pool. /// </summary> /// <param name="batchClient">A <see cref="BatchClient"/>.</param> /// <param name="jobId">The id of the job to be created.</param> /// <param name="poolId">The id of the <see cref="CloudPool"/> in which to create the job.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId, CloudStorageAccount linkedStorageAccount) { Console.WriteLine("Creating job [{0}]...", jobId); CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; job.UsesTaskDependencies = true; await job.CommitAsync(); }
private async Task CreateJobTask(BatchClient batchClient) { Console.WriteLine("Creating job [{0}]...", PoolKeys.JobId); CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = PoolKeys.JobId; job.Priority = 1000; job.PoolInformation = new PoolInformation { PoolId = PoolKeys.PoolId }; await job.CommitAsync(); }
private static async Task MutateJobAsync(string jobId, Func <CloudJob, Task> jobAction) { using (BatchClient batchCli = await TestUtilities.OpenBatchClientFromEnvironmentAsync().ConfigureAwait(false)) { const string newPoolId = "Bar"; const string metadataKey = "Foo"; const string metadataValue = "Bar"; TimeSpan newMaxWallClockTime = TimeSpan.FromDays(1); try { CloudJob job = batchCli.JobOperations.CreateJob( jobId, new PoolInformation() { PoolId = "Temp" }); await job.CommitAsync().ConfigureAwait(false); await job.RefreshAsync().ConfigureAwait(false); //Disable the job so that we can update the pool info await job.DisableAsync(DisableJobOption.Requeue).ConfigureAwait(false); await TestUtilities.WaitForJobStateAsync(job, TimeSpan.FromMinutes(1), JobState.Disabled).ConfigureAwait(false); job.Constraints = new JobConstraints(maxWallClockTime: newMaxWallClockTime); job.Metadata = new List <MetadataItem>() { new MetadataItem(metadataKey, metadataValue) }; job.PoolInformation.PoolId = newPoolId; await jobAction(job).ConfigureAwait(false); await job.RefreshAsync().ConfigureAwait(false); Assert.Equal(newMaxWallClockTime, job.Constraints.MaxWallClockTime); Assert.Equal(newPoolId, job.PoolInformation.PoolId); Assert.Equal(metadataKey, job.Metadata.Single().Name); Assert.Equal(metadataValue, job.Metadata.Single().Value); } finally { await TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).ConfigureAwait(false); } } }
/// <summary> /// Creates a job and adds a task to it. The task is a /// custom executable which has a resource file associated with it. /// </summary> /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> /// <param name="storageAccount">The cloud storage account to upload files to.</param> /// <param name="jobId">The ID of the job.</param> /// <returns>An asynchronous <see cref="Task"/> representing the operation.</returns> private async Task SubmitJobAsync(BatchClient batchClient, CloudStorageAccount storageAccount, string jobId) { // create an empty unbound Job CloudJob unboundJob = batchClient.JobOperations.CreateJob(); unboundJob.Id = jobId; unboundJob.PoolInformation = new PoolInformation() { PoolId = this.jobManagerSettings.PoolId }; // Upload the required files for the job manager task await SampleHelpers.UploadResourcesAsync(storageAccount, this.jobManagerSettings.BlobContainer, JobManagerRequiredFiles); List <ResourceFile> jobManagerResourceFiles = await SampleHelpers.UploadResourcesAndCreateResourceFileReferencesAsync( storageAccount, this.jobManagerSettings.BlobContainer, JobManagerRequiredFiles); // Set up the JobManager environment settings List <EnvironmentSetting> jobManagerEnvironmentSettings = new List <EnvironmentSetting>() { // No need to pass the batch account name as an environment variable since the batch service provides // an environment variable for each task which contains the account name new EnvironmentSetting("SAMPLE_BATCH_KEY", this.accountSettings.BatchAccountKey), new EnvironmentSetting("SAMPLE_BATCH_URL", this.accountSettings.BatchServiceUrl), new EnvironmentSetting("SAMPLE_STORAGE_ACCOUNT", this.accountSettings.StorageAccountName), new EnvironmentSetting("SAMPLE_STORAGE_KEY", this.accountSettings.StorageAccountKey), new EnvironmentSetting("SAMPLE_STORAGE_URL", this.accountSettings.StorageServiceUrl), }; unboundJob.JobManagerTask = new JobManagerTask() { Id = JobManagerTaskId, CommandLine = JobManagerTaskExe, ResourceFiles = jobManagerResourceFiles, KillJobOnCompletion = true, EnvironmentSettings = jobManagerEnvironmentSettings }; // Commit Job to create it in the service await unboundJob.CommitAsync(); }
private static async Task CreateJob(BatchClient batchClient, string jobId, string poolId) { try { CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; await job.CommitAsync(); } catch (BatchException ex) { if (ex.RequestInformation?.BatchError?.Code == BatchErrorCodeStrings.JobExists) { Console.WriteLine("The job already existed when we tried to create it"); } } }
public async Task CreateJobAsync(string jobId, string poolId) { Console.WriteLine("Creating job [{0}]...", jobId); // ToDo: Create the job definition CloudJob job = _batchClient.JobOperations.CreateJob(); job.Id = jobId; // ToDo: Specify the pool job.PoolInformation = new PoolInformation { PoolId = poolId }; // ToDo: Set job to completed state when all tasks are complete job.OnAllTasksComplete = OnAllTasksComplete.TerminateJob; // ToDo: Commit the job await job.CommitAsync(); }
/// <summary> /// Creates a CloudJob in the specified pool if a job with the specified ID is not found /// in the pool, otherwise returns the existing job. /// </summary> /// <param name="batchClient">A fully initialized <see cref="BatchClient"/>.</param> /// <param name="poolId">The ID of the CloudPool in which the job should be created.</param> /// <param name="jobId">The ID of the CloudJob.</param> /// <returns>A bound version of the newly created CloudJob.</returns> public static async Task <CloudJob> CreateJobIfNotExistAsync(BatchClient batchClient, string poolId, string jobId) { CloudJob job = await SampleHelpers.GetJobIfExistAsync(batchClient, jobId).ConfigureAwait(continueOnCapturedContext: false); if (job == null) { Console.WriteLine("Job {0} not found, creating...", jobId); CloudJob unboundJob = batchClient.JobOperations.CreateJob(jobId, new PoolInformation() { PoolId = poolId }); await unboundJob.CommitAsync().ConfigureAwait(continueOnCapturedContext: false); // Get the bound version of the job with all of its properties populated job = await batchClient.JobOperations.GetJobAsync(jobId).ConfigureAwait(continueOnCapturedContext: false); } return(job); }
/// <summary> /// Creates a job and adds a task to it. /// </summary> /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> /// <param name="configurationSettings">The configuration settings</param> /// <param name="jobId">The ID of the job.</param> /// <returns>An asynchronous <see cref="Task"/> representing the operation.</returns> public static async Task SubmitJobAsync(String jobId, String poolId, String imageName, int completions, String destinationStorageAccount, String destinationStorageAccountKey, String destinationContainerName, String destinationPrefixName, String aggregatorImageName) { BatchClient batchClient = PrepareConnection(); System.Console.WriteLine("Connected to Batch service..."); CloudJob newJob = batchClient.JobOperations.CreateJob(); newJob.Id = jobId; newJob.PoolInformation = new PoolInformation { PoolId = poolId }; newJob.UsesTaskDependencies = true; newJob.OnAllTasksComplete = OnAllTasksComplete.TerminateJob; await newJob.CommitAsync(); List <EnvironmentSetting> taskEnvironmentSettings = new List <EnvironmentSetting>() { new EnvironmentSetting("STORAGEACCOUNT", destinationStorageAccount), new EnvironmentSetting("STORAGEKEY", destinationStorageAccountKey), new EnvironmentSetting("CONTAINER", destinationContainerName), new EnvironmentSetting("BLOBPREFIX", destinationPrefixName), }; List <String> jobIds = new List <String>(); for (int i = 0; i < completions; i++) { CreateTask(jobId, imageName, batchClient, taskEnvironmentSettings, i.ToString(), null); jobIds.Add(i.ToString()); System.Console.WriteLine("Created Task " + i.ToString()); } System.Console.WriteLine("Creating aggregator..."); CreateTask(jobId, imageName, batchClient, taskEnvironmentSettings, "aggregator", jobIds); while (batchClient.JobOperations.GetJob(jobId).State != JobState.Completed) { Console.WriteLine(System.DateTime.Now + ": In waiting state for job: " + jobId + ", current state: " + batchClient.JobOperations.GetJob(jobId).State); Thread.Sleep(5000); } Console.WriteLine("Job finished"); }
private static async Task TestAutoPoolCreateAndUpdateWithCertificateReferencesAsync( BatchClient batchCli, IList <CertificateReference> certificateReferences) { string jobId = "TestAutoPoolCreateAndUpdateWithCertificateReferences-" + TestUtilities.GetMyName(); try { PoolInformation poolInformation = new PoolInformation { AutoPoolSpecification = new AutoPoolSpecification { PoolSpecification = new PoolSpecification { CertificateReferences = certificateReferences, CloudServiceConfiguration = new CloudServiceConfiguration(PoolFixture.OSFamily), TargetDedicatedComputeNodes = 0, VirtualMachineSize = PoolFixture.VMSize, }, AutoPoolIdPrefix = TestUtilities.GetMyName(), PoolLifetimeOption = PoolLifetimeOption.Job } }; CloudJob unboundJob = batchCli.JobOperations.CreateJob(jobId, poolInformation); await unboundJob.CommitAsync().ConfigureAwait(false); CloudJob boundJob = await batchCli.JobOperations.GetJobAsync(jobId).ConfigureAwait(false); AssertCertificateReferenceCollectionsAreSame( certificateReferences, boundJob.PoolInformation.AutoPoolSpecification.PoolSpecification.CertificateReferences); } finally { TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).Wait(); } }
public static async Task <CloudJob> CreateJobIfNotExistAsync( BatchClient batchClient, string poolId, string jobId, bool usesTaskDependencies = false, JobPreparationTask prepTask = null, JobReleaseTask releaseTask = null) { CloudJob job = await GetJobIfExistAsync(batchClient, jobId).ConfigureAwait(continueOnCapturedContext: false); if (job == null) { Console.WriteLine("Job {0} not found, creating...", jobId); CloudJob unboundJob = batchClient.JobOperations.CreateJob(jobId, new PoolInformation() { PoolId = poolId }); unboundJob.UsesTaskDependencies = usesTaskDependencies; if (prepTask != null) { unboundJob.JobPreparationTask = prepTask; } if (releaseTask != null) { unboundJob.JobReleaseTask = releaseTask; } await unboundJob.CommitAsync().ConfigureAwait(continueOnCapturedContext: false); // Get the bound version of the job with all of its properties populated job = await batchClient.JobOperations.GetJobAsync(jobId).ConfigureAwait(continueOnCapturedContext: false); } return(job); }
/// <summary> /// Create job /// </summary> /// <param name="batchClient"></param> /// <param name="jobId"></param> /// <param name="poolId"></param> /// <returns></returns> private static async Task CreateJob(BatchClient batchClient, string jobId, string poolId) { var pool = await batchClient.PoolOperations.GetPoolAsync(PoolId); targetVMs = pool.TargetDedicated.Value; Console.WriteLine("Creating job [{0}]...", jobId); CloudJob job = batchClient.JobOperations.CreateJob(); job.Id = jobId; job.PoolInformation = new PoolInformation { PoolId = poolId }; job.UsesTaskDependencies = true; job.JobPreparationTask = new JobPreparationTask() { Id = $"ExtractZip-{Guid.NewGuid().ToString()}", CommandLine = String.Format($"cmd /c %AZ_BATCH_NODE_SHARED_DIR%\\7z.exe x -aoa -o%AZ_BATCH_NODE_SHARED_DIR% %AZ_BATCH_NODE_SHARED_DIR%\\{encoderZip}") }; await job.CommitAsync(); }
public static async Task <CloudJob> CreateBatchJob(BatchClient batchClient, string jobId, ILogger log) { // Create a Batch job log.LogInformation("Creating job [{0}]...", jobId); CloudJob job = null; try { job = batchClient.JobOperations.CreateJob(jobId, new PoolInformation { PoolId = PoolId }); job.OnAllTasksComplete = OnAllTasksComplete.TerminateJob; // Commit the job to the Batch service await job.CommitAsync(); log.LogInformation($"Created job {jobId}"); // Obtain the bound job from the Batch service await job.RefreshAsync(); } 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) { log.LogWarning("The job {0} already existed when we tried to create it", jobId); } else { log.LogError("Exception creating job: {0}", be.Message); throw be; // Any other exception is unexpected } } return(job); }