/// <summary> /// Asynchronous method that delays execution until the specified pool reaches the specified state. /// </summary> /// <param name="client">A fully intitialized <see cref="BatchClient"/>.</param> /// <param name="poolId">The ID of the pool to monitor for the specified <see cref="AllocationState"/>.</param> /// <param name="targetAllocationState">The allocation state to monitor.</param> /// <param name="timeout">The maximum time to wait for the pool to reach the specified state.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> public static async Task WaitForPoolToReachStateAsync(BatchClient client, string poolId, AllocationState targetAllocationState, TimeSpan timeout) { Console.WriteLine("Waiting for pool {0} to reach allocation state {1}", poolId, targetAllocationState); DateTime startTime = DateTime.UtcNow; DateTime timeoutAfterThisTimeUtc = startTime.Add(timeout); ODATADetailLevel detail = new ODATADetailLevel(selectClause: "id,allocationState"); CloudPool pool = await client.PoolOperations.GetPoolAsync(poolId, detail); while (pool.AllocationState != targetAllocationState) { Console.Write("."); await Task.Delay(TimeSpan.FromSeconds(10)).ConfigureAwait(continueOnCapturedContext: false); await pool.RefreshAsync(detail).ConfigureAwait(continueOnCapturedContext: false); if (DateTime.UtcNow > timeoutAfterThisTimeUtc) { throw new TimeoutException(string.Format("Timed out waiting for pool {0} to reach state {1}", poolId, targetAllocationState)); } } Console.WriteLine(); }
public async Task UnboundPoolCommitAndRefreshWorks() { using (BatchClient batchClient = ClientUnitTestCommon.CreateDummyClient()) { const string id = "Bar"; const string displayName = "Baz"; var startTask = new Protocol.Models.StartTask("cmd /c dir"); var protoPool = new Protocol.Models.CloudPool( id: id, displayName: displayName, startTask: startTask); CloudPool pool = batchClient.PoolOperations.CreatePool(id, "Woo", new CloudServiceConfiguration("4")); await pool.CommitAsync(additionalBehaviors : InterceptorFactory.CreateAddPoolRequestInterceptor()); await pool.RefreshAsync(additionalBehaviors : InterceptorFactory.CreateGetPoolRequestInterceptor(protoPool)); Assert.Equal(id, pool.Id); Assert.Equal(displayName, pool.DisplayName); Assert.Null(pool.CloudServiceConfiguration); Assert.NotNull(pool.StartTask); Assert.Equal(startTask.CommandLine, pool.StartTask.CommandLine); } }
// Create the Compute Pool of the Batch Account public static async Task CreateBatchPoolIfNotExist(BatchClient batchClient, VirtualMachineConfiguration vmConfiguration, string vnetSubnetId) { Console.WriteLine("Creating pool [{0}]...", PoolId); try { CloudPool pool = batchClient.PoolOperations.CreatePool( poolId: PoolId, targetDedicatedComputeNodes: PoolNodeCount, virtualMachineSize: PoolVMSize, virtualMachineConfiguration: vmConfiguration); // Specify the application and version to install on the compute nodes pool.ApplicationPackageReferences = new List <ApplicationPackageReference> { new ApplicationPackageReference { ApplicationId = AppPackageName, Version = AppPackageVersion } }; // Initial the first data disk for each VM in the pool StartTask startTask = new StartTask("cmd /c Powershell -command \"Get-Disk | Where partitionstyle -eq 'raw' | sort number | Select-Object -first 1 |" + " Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -UseMaximumSize -DriveLetter F |" + " Format-Volume -FileSystem NTFS -NewFileSystemLabel data1 -Confirm:$false -Force\""); startTask.MaxTaskRetryCount = 1; startTask.UserIdentity = new UserIdentity(new AutoUserSpecification(AutoUserScope.Pool, ElevationLevel.Admin)); startTask.WaitForSuccess = true; pool.StartTask = startTask; // Create the Pool within the vnet subnet if it's specified. if (vnetSubnetId != null) { pool.NetworkConfiguration = new NetworkConfiguration(); pool.NetworkConfiguration.SubnetId = vnetSubnetId; } await pool.CommitAsync(); await pool.RefreshAsync(); } 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 } } }
/// <summary> /// Method to wait until desired pool state is achieved /// </summary> /// <param name="pool">The pool</param> /// <param name="desiredState">The desired state of the pool</param> /// <returns>Returns a boolean when the pool is ready</returns> public async Task <bool> AwaitDesiredPoolState(CloudPool pool, AllocationState desiredState) { var watch = System.Diagnostics.Stopwatch.StartNew(); watch.Start(); do { await Task.Delay(5000); // Refresh pool to get latest state await pool.RefreshAsync(); // Timeout after 30 minutes if (watch.ElapsedTicks > TimeSpan.TicksPerHour / 2) { return(false); } }while (pool.AllocationState != desiredState); return(true); }
private static async Task WaitForPoolToBeReady(CloudPool pool) { while (true) { await pool.RefreshAsync(); Log.Logger.Information("Pool {PoolId} is {AllocationState}.", pool.Id, pool.AllocationState); if (pool.AllocationState == AllocationState.Steady) { var nodes = await pool.ListComputeNodes().ToListAsync(); foreach (var node in nodes) { Log.Logger.Information("Node {NodeId} is {NodeState}.", node.Id, node.State); } if (nodes.All(x => x.State == ComputeNodeState.Idle)) { break; } } await Task.Delay(TimeSpan.FromSeconds(5)); } }
public async Task UnboundPoolDirectRefreshFailsWithMissingPathVariables() { using (BatchClient batchClient = ClientUnitTestCommon.CreateDummyClient()) { CloudPool pool = batchClient.PoolOperations.CreatePool(); ValidationException e = await Assert.ThrowsAsync <ValidationException>(async() => await pool.RefreshAsync()); Assert.Contains("'poolId' cannot be null", e.Message); } }