|
1 |
| -//Copyright (c) Microsoft Corporation |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 |
|
3 |
| -namespace Microsoft.Azure.Batch.Samples.HelloWorld |
4 |
| -{ |
5 |
| - using System; |
6 |
| - using System.IO; |
7 |
| - using System.Collections.Generic; |
8 |
| - using System.Threading.Tasks; |
9 |
| - using Auth; |
10 |
| - using Batch.Common; |
11 |
| - using Common; |
12 |
| - using Microsoft.Extensions.Configuration; |
| 3 | +using System; |
| 4 | +using System.Threading.Tasks; |
13 | 5 |
|
| 6 | +namespace Azure.Compute.Batch.Samples.HelloWorld |
| 7 | +{ |
14 | 8 | /// <summary>
|
15 | 9 | /// The main program of the HelloWorld sample
|
16 | 10 | /// </summary>
|
17 | 11 | public static class Program
|
18 | 12 | {
|
19 |
| - public static void Main(string[] args) |
20 |
| - { |
21 |
| - try |
22 |
| - { |
23 |
| - AccountSettings accountSettings = SampleHelpers.LoadAccountSettings(); |
24 |
| - Settings helloWorldSettings = new ConfigurationBuilder() |
25 |
| - .SetBasePath(Directory.GetCurrentDirectory()) |
26 |
| - .AddJsonFile("settings.json") |
27 |
| - .Build() |
28 |
| - .Get<Settings>(); |
29 |
| - |
30 |
| - HelloWorldAsync(accountSettings, helloWorldSettings).Wait(); |
31 |
| - } |
32 |
| - catch (AggregateException aggregateException) |
33 |
| - { |
34 |
| - // Go through all exceptions and dump useful information |
35 |
| - foreach (Exception exception in aggregateException.InnerExceptions) |
36 |
| - { |
37 |
| - Console.WriteLine(exception.ToString()); |
38 |
| - Console.WriteLine(); |
39 |
| - } |
| 13 | + private static string batchAccountResourceId = "your-batch-account-resource-id"; |
40 | 14 |
|
41 |
| - throw; |
42 |
| - } |
| 15 | + public async static Task Main(string[] args) |
| 16 | + { |
| 17 | + await new HelloWorldSample().Run(batchAccountResourceId); |
43 | 18 |
|
44 | 19 | Console.WriteLine("Press return to exit...");
|
45 | 20 | Console.ReadLine();
|
46 | 21 | }
|
47 |
| - |
48 |
| - /// <summary> |
49 |
| - /// Submits a job to the Azure Batch service, and waits for it to complete |
50 |
| - /// </summary> |
51 |
| - private static async Task HelloWorldAsync( |
52 |
| - AccountSettings accountSettings, |
53 |
| - Settings helloWorldConfigurationSettings) |
54 |
| - { |
55 |
| - Console.WriteLine("Running with the following settings: "); |
56 |
| - Console.WriteLine("-------------------------------------"); |
57 |
| - Console.WriteLine(helloWorldConfigurationSettings.ToString()); |
58 |
| - Console.WriteLine(accountSettings.ToString()); |
59 |
| - |
60 |
| - // Set up the Batch Service credentials used to authenticate with the Batch Service. |
61 |
| - BatchSharedKeyCredentials credentials = new BatchSharedKeyCredentials( |
62 |
| - accountSettings.BatchServiceUrl, |
63 |
| - accountSettings.BatchAccountName, |
64 |
| - accountSettings.BatchAccountKey); |
65 |
| - |
66 |
| - // Get an instance of the BatchClient for a given Azure Batch account. |
67 |
| - using (BatchClient batchClient = BatchClient.Open(credentials)) |
68 |
| - { |
69 |
| - // add a retry policy. The built-in policies are No Retry (default), Linear Retry, and Exponential Retry |
70 |
| - batchClient.CustomBehaviors.Add(RetryPolicyProvider.ExponentialRetryProvider(TimeSpan.FromSeconds(5), 3)); |
71 |
| - |
72 |
| - string jobId = GettingStartedCommon.CreateJobId("HelloWorldJob"); |
73 |
| - |
74 |
| - try |
75 |
| - { |
76 |
| - // Submit the job |
77 |
| - await SubmitJobAsync(batchClient, helloWorldConfigurationSettings, jobId); |
78 |
| - |
79 |
| - // Wait for the job to complete |
80 |
| - await WaitForJobAndPrintOutputAsync(batchClient, jobId); |
81 |
| - } |
82 |
| - finally |
83 |
| - { |
84 |
| - // Delete the job to ensure the tasks are cleaned up |
85 |
| - if (!string.IsNullOrEmpty(jobId) && helloWorldConfigurationSettings.ShouldDeleteJob) |
86 |
| - { |
87 |
| - Console.WriteLine("Deleting job: {0}", jobId); |
88 |
| - await batchClient.JobOperations.DeleteJobAsync(jobId); |
89 |
| - } |
90 |
| - } |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - /// <summary> |
95 |
| - /// Creates a job and adds a task to it. |
96 |
| - /// </summary> |
97 |
| - /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> |
98 |
| - /// <param name="configurationSettings">The configuration settings</param> |
99 |
| - /// <param name="jobId">The ID of the job.</param> |
100 |
| - /// <returns>An asynchronous <see cref="Task"/> representing the operation.</returns> |
101 |
| - private static async Task SubmitJobAsync( |
102 |
| - BatchClient batchClient, |
103 |
| - Settings configurationSettings, |
104 |
| - string jobId) |
105 |
| - { |
106 |
| - // create an empty unbound Job |
107 |
| - CloudJob unboundJob = batchClient.JobOperations.CreateJob(); |
108 |
| - unboundJob.Id = jobId; |
109 |
| - |
110 |
| - // For this job, ask the Batch service to automatically create a pool of VMs when the job is submitted. |
111 |
| - unboundJob.PoolInformation = new PoolInformation() |
112 |
| - { |
113 |
| - AutoPoolSpecification = new AutoPoolSpecification() |
114 |
| - { |
115 |
| - AutoPoolIdPrefix = "HelloWorld", |
116 |
| - PoolSpecification = new PoolSpecification() |
117 |
| - { |
118 |
| - TargetDedicatedComputeNodes = configurationSettings.PoolTargetNodeCount, |
119 |
| - VirtualMachineSize = configurationSettings.PoolNodeVirtualMachineSize, |
120 |
| - VirtualMachineConfiguration = new VirtualMachineConfiguration( |
121 |
| - imageReference: new ImageReference( |
122 |
| - publisher: configurationSettings.ImagePublisher, |
123 |
| - offer: configurationSettings.ImageOffer, |
124 |
| - sku: configurationSettings.ImageSku, |
125 |
| - version: configurationSettings.ImageVersion |
126 |
| - ), |
127 |
| - nodeAgentSkuId: configurationSettings.NodeAgentSkuId), |
128 |
| - }, |
129 |
| - KeepAlive = false, |
130 |
| - PoolLifetimeOption = PoolLifetimeOption.Job |
131 |
| - } |
132 |
| - }; |
133 |
| - |
134 |
| - // Commit Job to create it in the service |
135 |
| - await unboundJob.CommitAsync(); |
136 |
| - |
137 |
| - // create a simple task. Each task within a job must have a unique ID |
138 |
| - await batchClient.JobOperations.AddTaskAsync(jobId, new CloudTask("task1", "cmd /c echo Hello world from the Batch Hello world sample!")); |
139 |
| - } |
140 |
| - |
141 |
| - /// <summary> |
142 |
| - /// Waits for all tasks under the specified job to complete and then prints each task's output to the console. |
143 |
| - /// </summary> |
144 |
| - /// <param name="batchClient">The BatchClient to use when interacting with the Batch service.</param> |
145 |
| - /// <param name="jobId">The ID of the job.</param> |
146 |
| - /// <returns>An asynchronous <see cref="Task"/> representing the operation.</returns> |
147 |
| - private static async Task WaitForJobAndPrintOutputAsync(BatchClient batchClient, string jobId) |
148 |
| - { |
149 |
| - Console.WriteLine("Waiting for all tasks to complete on job: {0} ...", jobId); |
150 |
| - |
151 |
| - // We use the task state monitor to monitor the state of our tasks -- in this case we will wait for them all to complete. |
152 |
| - TaskStateMonitor taskStateMonitor = batchClient.Utilities.CreateTaskStateMonitor(); |
153 |
| - |
154 |
| - List<CloudTask> ourTasks = await batchClient.JobOperations.ListTasks(jobId).ToListAsync(); |
155 |
| - |
156 |
| - // Wait for all tasks to reach the completed state. |
157 |
| - // If the pool is being resized then enough time is needed for the nodes to reach the idle state in order |
158 |
| - // for tasks to run on them. |
159 |
| - await taskStateMonitor.WhenAll(ourTasks, TaskState.Completed, TimeSpan.FromMinutes(10)); |
160 |
| - |
161 |
| - // dump task output |
162 |
| - foreach (CloudTask t in ourTasks) |
163 |
| - { |
164 |
| - Console.WriteLine("Task {0}", t.Id); |
165 |
| - |
166 |
| - //Read the standard out of the task |
167 |
| - NodeFile standardOutFile = await t.GetNodeFileAsync(Constants.StandardOutFileName); |
168 |
| - string standardOutText = await standardOutFile.ReadAsStringAsync(); |
169 |
| - Console.WriteLine("Standard out:"); |
170 |
| - Console.WriteLine(standardOutText); |
171 |
| - |
172 |
| - Console.WriteLine(); |
173 |
| - } |
174 |
| - } |
175 | 22 | }
|
176 | 23 | }
|
0 commit comments