private static void ListPools(IBatchClient client) { using (var pm = client.OpenPoolManager()) { Console.WriteLine("Listing Pools\n============="); // Using optional select clause to return only the name and state. IEnumerable <ICloudPool> pools = pm.ListPools(new ODATADetailLevel(selectClause: "name,state")); foreach (var p in pools) { Console.WriteLine("pool " + p.Name + " is " + p.State); } Console.WriteLine(); } }
private void CopyRDPStream(Stream destinationStream, IBatchClient client, string poolName, string vmName, PSVM vm, IEnumerable <BatchClientBehavior> additionalBehaviors = null) { if (vm == null) { using (IPoolManager poolManager = client.OpenPoolManager()) { poolManager.GetRDPFile(poolName, vmName, destinationStream, additionalBehaviors); } } else { vm.omObject.GetRDPFile(destinationStream, additionalBehaviors); } }
private static void DeletePool(IBatchClient client, string poolName) { using (var pm = client.OpenPoolManager()) { try { pm.DeletePool(poolName); } catch (AggregateException ex) { ex.Handle(x => (0 == string.Compare((x as BatchException)?.RequestInformation?.AzureError?.Code ?? "", "PoolNotFound", StringComparison.InvariantCulture))); } } }
private static void CreatePoolIfNotExist(IBatchClient client, string poolName) { // All Pool and VM operation starts from PoolManager using (var pm = client.OpenPoolManager()) { var found = false; try { var p = pm.GetPool(poolName, new ODATADetailLevel(selectClause: "name")); found = true; if (!p.ListVMs(new ODATADetailLevel(selectClause: "name")).Any()) { Console.WriteLine( "There are no VMs in this pool. No tasks will be run until at least one VM has been added via resizing."); Console.WriteLine("Resizing pool to add 3 VMs. This might take a while..."); p.Resize(3); } } catch (AggregateException ex) { ex.Handle(x => (0 == string.Compare((x as BatchException)?.RequestInformation?.AzureError?.Code ?? "", "PoolNotFound", StringComparison.InvariantCulture))); } if (!found) { Console.WriteLine("Creating pool: {0}", poolName); // if pool not found, call CreatePool // You can learn more about os families and versions at: // http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx var pool = pm.CreatePool(poolName, targetDedicated: 5, vmSize: "small", osFamily: "3"); pool.Commit(); } } }
private static void CreatePoolIfNotExist(IBatchClient client, string poolName) { // All Pool and VM operation starts from PoolManager using (IPoolManager pm = client.OpenPoolManager()) { // go through all the pools and see if it already exists bool found = false; foreach (ICloudPool p in pm.ListPools()) { // pools are uniquely identified by their name if (string.Equals(p.Name, poolName)) { Console.WriteLine("Using existing pool {0}", poolName); found = true; if (!p.ListVMs().Any <IVM>()) { Console.WriteLine("There are no VMs in this pool. No tasks will be run until at least one VM has been added via resizing."); Console.WriteLine("Resizing pool to add 3 VMs. This might take a while..."); p.Resize(3); } break; } } if (!found) { Console.WriteLine("Creating pool: {0}", poolName); // if pool not found, call CreatePool //You can learn more about os families and versions at: //http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx ICloudPool pool = pm.CreatePool(poolName, targetDedicated: 3, vmSize: "small", osFamily: "3"); pool.Commit(); } } }
private static void CreatePoolIfNotExist(IBatchClient client, string poolName) { // All Pool and VM operation starts from PoolManager using (IPoolManager pm = client.OpenPoolManager()) { // go through all the pools and see if it already exists bool found = false; foreach (ICloudPool p in pm.ListPools()) { // pools are uniquely identified by their name if (string.Equals(p.Name, poolName)) { Console.WriteLine("Using existing pool {0}", poolName); found = true; if (!p.ListVMs().Any()) { Console.WriteLine("There are no VMs in this pool. No tasks will be run until at least one VM has been added via resizing."); Console.WriteLine("Resizing pool to add 3 VMs. This might take a while..."); p.Resize(3); } break; } } if (!found) { Console.WriteLine("Creating pool: {0}", poolName); // if pool not found, call CreatePool //You can learn more about os families and versions at: //http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx ICloudPool pool = pm.CreatePool(poolName, targetDedicated: 3, vmSize: "small", osFamily: "3"); pool.Commit(); } } }
private static bool WaitAllJobPrepTask( IBatchClient client, string pool, string workitem, string job, Func <int, int, bool> stop, Func <int, int, bool> succeed, TimeSpan timeout, TimeSpan freq, int parallelism = 5) { using (var pm = client.OpenPoolManager()) { using (var vms = new BlockingCollection <string>()) { var cts = new CancellationTokenSource(); var ct = cts.Token; var tasks = new List <Task>(); var succeededJobPrepTasks = 0; var failedJobPrepTasks = 0; // find all VM in the Pool and put them in a queue tasks.Add(Task.Run(async() => { Log("Start adding vms to query queue."); // list all vms and enqueue // pool manager currently does nothing in Dispose() // ReSharper disable once AccessToDisposedClosure var p = pm.GetPool(PoolName); var en = p.ListVMs(new ODATADetailLevel(selectClause: "name")).GetAsyncEnumerator(); while (await en.MoveNextAsync()) { Log("Add vm to query queue.", en.Current.Name); // If vms is disposed, exception will be thrown and the thread is aborted // ReSharper disable once AccessToDisposedClosure vms.Add(en.Current.Name, ct); if (ct.IsCancellationRequested) { break; } } }, ct)); // launch N threads to get job prep task status of the nodes in the queue for (var i = 0; i < parallelism; i++) { tasks.Add(Task.Run(() => { while (!ct.IsCancellationRequested) { string vm; try { // If vms is disposed, exception will be thrown and the thread is aborted // ReSharper disable once AccessToDisposedClosure vm = vms.Take(ct); } catch (ObjectDisposedException) // if vms is disposed, abort the thread { break; } catch (OperationCanceledException) // if token cancelled, abort the thread { break; } Log("Start polling vm", vm); try { // pool manager currently does nothing in Dispose() // ReSharper disable once AccessToDisposedClosure var state = pm.GetJobPreparationTaskStatus(pool, vm, workitem, job); Log("got vm state", vm); if (state.State == JobPreparationTaskState.Completed) // job prep task completed on the thread { if (state.ExitCode == 0) { Interlocked.Increment(ref succeededJobPrepTasks); } else { Interlocked.Increment(ref failedJobPrepTasks); } Log("==== job prep done ====", vm); if (stop(succeededJobPrepTasks, failedJobPrepTasks)) { Log("==== polling done, cancel all threads. ===="); cts.Cancel(); } } else // job prep still running { Log("State is not completed", vm); // Sleep for some time and put the VM back to the queue Task.Run(() => { Task.Delay(freq, ct); // if we hit TaskCanceledException here, abort the thread if (!ct.IsCancellationRequested) { Log("Add vm back to query queue", vm); vms.Add(vm, ct); // if we hit OperationCanceledException here, abort the thread } }, ct); } } catch (AggregateException ex) { // Error happens when query job prep task info. Ignore JobPreparationTaskNotRunOnTVM // sinc it means the task has not been run. Add the vm back to the query queue and continue. // Other exception will be re-thrown. Log("Exception happend when querying prep task status.", vm); ex.Handle(x => { if (0 != string.Compare( (x as BatchException)?.RequestInformation?.AzureError?.Code ?? "", "JobPreparationTaskNotRunOnTVM", StringComparison.InvariantCulture)) { return(false); } Task.Run(() => { Task.Delay(freq, ct); // if we hit TaskCanceledException here, abort the thread if (!ct.IsCancellationRequested) { Log("Add vm back to query queue", vm); vms.Add(vm, ct); // if we hit OperationCanceledException here, abort the thread } }, ct); return(true); }); } } Log("Thread cancel."); }, ct)); } Task.WaitAll(tasks.ToArray(), timeout); cts.Cancel(); Log("All done."); return(succeed(succeededJobPrepTasks, failedJobPrepTasks)); } } }
public static void JobMain(string[] args) { //Load the configuration TopNWordsConfiguration configuration = TopNWordsConfiguration.LoadConfigurationFromAppConfig(); StagingStorageAccount stagingStorageAccount = new StagingStorageAccount( configuration.StorageAccountName, configuration.StorageAccountKey, configuration.StorageAccountBlobEndpoint); IBatchClient client = BatchClient.Connect(configuration.BatchServiceUrl, new BatchCredentials(configuration.BatchAccountName, configuration.BatchAccountKey)); string stagingContainer = null; //Create a pool (if user hasn't provided one) if (configuration.ShouldCreatePool) { using (IPoolManager pm = client.OpenPoolManager()) { //OSFamily 4 == OS 2012 R2 //You can learn more about os families and versions at: //http://msdn.microsoft.com/en-us/library/azure/ee924680.aspx ICloudPool pool = pm.CreatePool(configuration.PoolName, targetDedicated: configuration.PoolSize, osFamily: "4", vmSize: "small"); Console.WriteLine("Adding pool {0}", configuration.PoolName); pool.Commit(); } } try { using (IWorkItemManager wm = client.OpenWorkItemManager()) { IToolbox toolbox = client.OpenToolbox(); //Use the TaskSubmissionHelper to help us create a WorkItem and add tasks to it. ITaskSubmissionHelper taskSubmissionHelper = toolbox.CreateTaskSubmissionHelper(wm, configuration.PoolName); taskSubmissionHelper.WorkItemName = configuration.WorkItemName; FileToStage topNWordExe = new FileToStage(TopNWordsExeName, stagingStorageAccount); FileToStage storageDll = new FileToStage(StorageClientDllName, stagingStorageAccount); string bookFileUri = UploadBookFileToCloudBlob(configuration, configuration.BookFileName); Console.WriteLine("{0} uploaded to cloud", configuration.BookFileName); for (int i = 1; i <= configuration.NumberOfTasks; i++) { ICloudTask task = new CloudTask("task_no_" + i, String.Format("{0} --Task {1} {2} {3} {4}", TopNWordsExeName, bookFileUri, configuration.NumberOfTopWords, configuration.StorageAccountName, configuration.StorageAccountKey)); //This is the list of files to stage to a container -- for each TaskSubmissionHelper one container is created and //files all resolve to Azure Blobs by their name (so two tasks with the same named file will create just 1 blob in //the TaskSubmissionHelper's container). task.FilesToStage = new List <IFileStagingProvider> { topNWordExe, storageDll }; taskSubmissionHelper.AddTask(task); } //Commit all the tasks to the Batch Service. IJobCommitUnboundArtifacts artifacts = taskSubmissionHelper.Commit() as IJobCommitUnboundArtifacts; foreach (var fileStagingArtifact in artifacts.FileStagingArtifacts) { SequentialFileStagingArtifact stagingArtifact = fileStagingArtifact.Value as SequentialFileStagingArtifact; if (stagingArtifact != null) { stagingContainer = stagingArtifact.BlobContainerCreated; Console.WriteLine("Uploaded files to container: {0} -- you will be charged for their storage unless you delete them.", stagingArtifact.BlobContainerCreated); } } //Get the job to monitor status. ICloudJob job = wm.GetJob(artifacts.WorkItemName, artifacts.JobName); Console.Write("Waiting for tasks to complete ..."); // Wait 1 minute for all tasks to reach the completed state client.OpenToolbox().CreateTaskStateMonitor().WaitAll(job.ListTasks(), TaskState.Completed, TimeSpan.FromMinutes(20)); Console.WriteLine("Done."); foreach (ICloudTask task in job.ListTasks()) { Console.WriteLine("Task " + task.Name + " says:\n" + task.GetTaskFile(Constants.StandardOutFileName).ReadAsString()); Console.WriteLine(task.GetTaskFile(Constants.StandardErrorFileName).ReadAsString()); } } } finally { //Delete the pool that we created if (configuration.ShouldCreatePool) { using (IPoolManager pm = client.OpenPoolManager()) { Console.WriteLine("Deleting pool: {0}", configuration.PoolName); pm.DeletePool(configuration.PoolName); } } //Delete the workitem that we created if (configuration.ShouldDeleteWorkItem) { using (IWorkItemManager wm = client.OpenWorkItemManager()) { Console.WriteLine("Deleting work item: {0}", configuration.WorkItemName); wm.DeleteWorkItem(configuration.WorkItemName); } } //Delete the containers we created if (configuration.ShouldDeleteContainer) { DeleteContainers(configuration, stagingContainer); } } }
private static bool WaitAllJobPrepTask( IBatchClient client, string pool, string workitem, string job, Func<int, int, bool> stop, Func<int, int, bool> succeed, TimeSpan timeout, TimeSpan freq, int parallelism = 5) { using (var pm = client.OpenPoolManager()) { using (var vms = new BlockingCollection<string>()) { var cts = new CancellationTokenSource(); var ct = cts.Token; var tasks = new List<Task>(); var succeededJobPrepTasks = 0; var failedJobPrepTasks = 0; // find all VM in the Pool and put them in a queue tasks.Add(Task.Run(async () => { Log("Start adding vms to query queue."); // list all vms and enqueue // pool manager currently does nothing in Dispose() // ReSharper disable once AccessToDisposedClosure var p = pm.GetPool(PoolName); var en = p.ListVMs(new ODATADetailLevel(selectClause: "name")).GetAsyncEnumerator(); while (await en.MoveNextAsync()) { Log("Add vm to query queue.", en.Current.Name); // If vms is disposed, exception will be thrown and the thread is aborted // ReSharper disable once AccessToDisposedClosure vms.Add(en.Current.Name, ct); if (ct.IsCancellationRequested) break; } }, ct)); // launch N threads to get job prep task status of the nodes in the queue for (var i = 0; i < parallelism; i++) { tasks.Add(Task.Run(() => { while (!ct.IsCancellationRequested) { string vm; try { // If vms is disposed, exception will be thrown and the thread is aborted // ReSharper disable once AccessToDisposedClosure vm = vms.Take(ct); } catch (ObjectDisposedException) // if vms is disposed, abort the thread { break; } catch (OperationCanceledException) // if token cancelled, abort the thread { break; } Log("Start polling vm", vm); try { // pool manager currently does nothing in Dispose() // ReSharper disable once AccessToDisposedClosure var state = pm.GetJobPreparationTaskStatus(pool, vm, workitem, job); Log("got vm state", vm); if (state.State == JobPreparationTaskState.Completed) // job prep task completed on the thread { if (state.ExitCode == 0) { Interlocked.Increment(ref succeededJobPrepTasks); } else { Interlocked.Increment(ref failedJobPrepTasks); } Log("==== job prep done ====", vm); if (stop(succeededJobPrepTasks, failedJobPrepTasks)) { Log("==== polling done, cancel all threads. ===="); cts.Cancel(); } } else // job prep still running { Log("State is not completed", vm); // Sleep for some time and put the VM back to the queue Task.Run(() => { Task.Delay(freq, ct); // if we hit TaskCanceledException here, abort the thread if (!ct.IsCancellationRequested) { Log("Add vm back to query queue", vm); vms.Add(vm, ct); // if we hit OperationCanceledException here, abort the thread } }, ct); } } catch (AggregateException ex) { // Error happens when query job prep task info. Ignore JobPreparationTaskNotRunOnTVM // sinc it means the task has not been run. Add the vm back to the query queue and continue. // Other exception will be re-thrown. Log("Exception happend when querying prep task status.", vm); ex.Handle(x => { if (0 != string.Compare( (x as BatchException)?.RequestInformation?.AzureError?.Code ?? "", "JobPreparationTaskNotRunOnTVM", StringComparison.InvariantCulture)) { return false; } Task.Run(() => { Task.Delay(freq, ct); // if we hit TaskCanceledException here, abort the thread if (!ct.IsCancellationRequested) { Log("Add vm back to query queue", vm); vms.Add(vm, ct); // if we hit OperationCanceledException here, abort the thread } }, ct); return true; }); } } Log("Thread cancel."); }, ct)); } Task.WaitAll(tasks.ToArray(), timeout); cts.Cancel(); Log("All done."); return succeed(succeededJobPrepTasks, failedJobPrepTasks); } } }
private static void ListPools(IBatchClient client) { using (var pm = client.OpenPoolManager()) { Console.WriteLine("Listing Pools\n============="); // Using optional select clause to return only the name and state. IEnumerable<ICloudPool> pools = pm.ListPools(new ODATADetailLevel(selectClause: "name,state")); foreach (var p in pools) { Console.WriteLine("pool " + p.Name + " is " + p.State); } Console.WriteLine(); } }
private void CopyRDPStream(Stream destinationStream, IBatchClient client, string poolName, string vmName, PSVM vm, IEnumerable<BatchClientBehavior> additionalBehaviors = null) { if (vm == null) { using (IPoolManager poolManager = client.OpenPoolManager()) { poolManager.GetRDPFile(poolName, vmName, destinationStream, additionalBehaviors); } } else { vm.omObject.GetRDPFile(destinationStream, additionalBehaviors); } }