/// <summary> /// Submits a job to the Azure Batch service, and waits for it to complete /// </summary> private static async Task HelloWorldAsync(AccountSettings accountSettings, Settings helloWorldConfigurationSettings) { Console.WriteLine("Running with the following settings: "); Console.WriteLine("-------------------------------------"); Console.WriteLine(helloWorldConfigurationSettings.ToString()); Console.WriteLine(accountSettings.ToString()); // Set up the Batch Service credentials used to authenticate with the Batch Service. BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials( accountSettings.BatchServiceUrl, accountSettings.BatchAccountName, accountSettings.BatchAccountKey); // Get an instance of the BatchClient for a given Azure Batch account. using (BatchClient batchClient = await BatchClient.OpenAsync(credentials)) { // add a retry policy. The built-in policies are No Retry (default), Linear Retry, and Exponential Retry batchClient.CustomBehaviors.Add(RetryPolicyProvider.LinearRetryProvider(TimeSpan.FromSeconds(10), 3)); string jobId = GettingStartedCommon.CreateJobId("HelloWorldJob"); try { // Submit the job await SubmitJobAsync(batchClient, helloWorldConfigurationSettings, jobId); // Wait for the job to complete await WaitForJobAndPrintOutputAsync(batchClient, jobId); } finally { // Delete the job to ensure the tasks are cleaned up if (!string.IsNullOrEmpty(jobId) && helloWorldConfigurationSettings.ShouldDeleteJob) { Console.WriteLine("Deleting job: {0}", jobId); batchClient.JobOperations.DeleteJob(jobId); } } } }
public static void Main(string[] args) { try { Settings helloWorldConfigurationSettings = Settings.Default; AccountSettings accountSettings = AccountSettings.Default; HelloWorldAsync(accountSettings, helloWorldConfigurationSettings).Wait(); } catch (AggregateException aggregateException) { // Go through all exceptions and dump useful information foreach (Exception exception in aggregateException.InnerExceptions) { Console.WriteLine(exception.ToString()); Console.WriteLine(); } throw; } Console.WriteLine("Press return to exit..."); Console.ReadLine(); }
/// <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, AccountSettings accountSettings, string jobId) { // create an empty unbound Job CloudJob unboundJob = batchClient.JobOperations.CreateJob(); unboundJob.Id = jobId; unboundJob.CommonEnvironmentSettings = new List <EnvironmentSetting> { new EnvironmentSetting("AZURE_STORAGE_CONNECTION_STRING", accountSettings.StorageConnectionString) }; // 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() { TargetDedicatedComputeNodes = configurationSettings.PoolTargetNodeCount, VirtualMachineConfiguration = new VirtualMachineConfiguration( new ImageReference( "UbuntuServer", "Canonical", "18.04-LTS", "latest" ), "batch.node.ubuntu 18.04" ), TaskSlotsPerNode = configurationSettings.TaskSlotsPerNode, TaskSchedulingPolicy = new TaskSchedulingPolicy(ComputeNodeFillType.Spread), VirtualMachineSize = configurationSettings.PoolNodeVirtualMachineSize, ApplicationPackageReferences = new List <ApplicationPackageReference> { new ApplicationPackageReference { ApplicationId = configurationSettings.ApplicationId, Version = configurationSettings.ApplicationVersion } }, }, KeepAlive = configurationSettings.PoolKeepAlive, 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("task-env", "env")); //await batchClient.JobOperations.AddTaskAsync(jobId, new CloudTask("task-hostname", "/bin/sh -c \"hostname\"")); // Task could should equal poolTargetNodeCount * # of cores in VM SKU //TODO https://docs.microsoft.com/en-us/azure/batch/large-number-tasks#example-batch-net // Create list of CloudTasks and addTaskAysnc in single operation. int i; List <CloudTask> tasksToAdd = new List <CloudTask>(); // Populate with your tasks //string cmd = "/bin/sh -c \"$AZ_BATCH_APP_PACKAGE_sleeper_1_2/sleeper-queue consume\""; for (i = 0; i < configurationSettings.TaskCount; i++) { tasksToAdd.Add(new CloudTask($"{configurationSettings.ApplicationId}-{i}", configurationSettings.TaskCommand)); } // https://docs.microsoft.com/en-us/azure/batch/large-number-tasks#example-batch-net BatchClientParallelOptions parallelOptions = new BatchClientParallelOptions() { MaxDegreeOfParallelism = configurationSettings.MaxDegreeOfParallelism }; await batchClient.JobOperations.AddTaskAsync(jobId, tasksToAdd, parallelOptions); }