/// <summary> /// Lists the workitems matching the specified filter options /// </summary> /// <param name="options">The options to use when querying for workitems</param> /// <returns>The workitems matching the specified filter options</returns> public IEnumerable <PSCloudWorkItem> ListWorkItems(ListWorkItemOptions options) { if (options == null) { throw new ArgumentNullException("options"); } // Get the single WorkItem matching the specified name if (!string.IsNullOrWhiteSpace(options.WorkItemName)) { WriteVerbose(string.Format(Resources.GBWI_GetByName, options.WorkItemName)); using (IWorkItemManager wiManager = options.Context.BatchOMClient.OpenWorkItemManager()) { ICloudWorkItem workItem = wiManager.GetWorkItem(options.WorkItemName, additionalBehaviors: options.AdditionalBehaviors); PSCloudWorkItem psWorkItem = new PSCloudWorkItem(workItem); return(new PSCloudWorkItem[] { psWorkItem }); } } // List WorkItems using the specified filter else { ODATADetailLevel odata = null; string verboseLogString = null; if (!string.IsNullOrEmpty(options.Filter)) { verboseLogString = Resources.GBWI_GetByOData; odata = new ODATADetailLevel(filterClause: options.Filter); } else { verboseLogString = Resources.GBWI_NoFilter; } WriteVerbose(verboseLogString); using (IWorkItemManager wiManager = options.Context.BatchOMClient.OpenWorkItemManager()) { IEnumerableAsyncExtended <ICloudWorkItem> workItems = wiManager.ListWorkItems(odata, options.AdditionalBehaviors); Func <ICloudWorkItem, PSCloudWorkItem> mappingFunction = w => { return(new PSCloudWorkItem(w)); }; return(PSAsyncEnumerable <PSCloudWorkItem, ICloudWorkItem> .CreateWithMaxCount( workItems, mappingFunction, options.MaxCount, () => WriteVerbose(string.Format(Resources.MaxCount, options.MaxCount)))); } } }
/// <summary> /// Submit a large number of tasks to the Batch Service. /// </summary> /// <param name="client">The batch client.</param> private static void SubmitLargeNumberOfTasks(IBatchClient client) { const int taskCountToCreate = 5000; // In order to simulate a "large" task object which has many properties set (such as resource files, environment variables, etc) // we create a big environment variable so we have a big task object. char[] env = new char[2048]; for (int i = 0; i < env.Length; i++) { env[i] = 'a'; } string envStr = new string(env); using (IWorkItemManager wm = client.OpenWorkItemManager()) { //Create a work item string workItemName = Environment.GetEnvironmentVariable("USERNAME") + DateTime.Now.ToString("yyyyMMdd-HHmmss"); Console.WriteLine("Creating work item {0}", workItemName); ICloudWorkItem cloudWorkItem = wm.CreateWorkItem(workItemName); cloudWorkItem.JobExecutionEnvironment = new JobExecutionEnvironment() { PoolName = PoolName }; //Specify the pool to run on cloudWorkItem.Commit(); //Wait for an active job TimeSpan maxJobCreationTimeout = TimeSpan.FromSeconds(90); DateTime jobCreationStartTime = DateTime.Now; DateTime jobCreationTimeoutTime = jobCreationStartTime.Add(maxJobCreationTimeout); cloudWorkItem = wm.GetWorkItem(workItemName); Console.WriteLine("Waiting for a job to become active..."); while (cloudWorkItem.ExecutionInformation == null || cloudWorkItem.ExecutionInformation.RecentJob == null) { cloudWorkItem.Refresh(); if (DateTime.Now > jobCreationTimeoutTime) { throw new Exception("Timed out waiting for job."); } Thread.Sleep(TimeSpan.FromSeconds(5)); } string jobName = cloudWorkItem.ExecutionInformation.RecentJob.Name; Console.WriteLine("Found job {0}. Adding task objects...", jobName); //Generate a large number of tasks to submit List <ICloudTask> tasksToSubmit = new List <ICloudTask>(); for (int i = 0; i < taskCountToCreate; i++) { ICloudTask task = new CloudTask("echo" + i.ToString("D5"), "echo"); List <IEnvironmentSetting> environmentSettings = new List <IEnvironmentSetting>(); environmentSettings.Add(new EnvironmentSetting("envone", envStr)); task.EnvironmentSettings = environmentSettings; tasksToSubmit.Add(task); } BatchClientParallelOptions parallelOptions = new BatchClientParallelOptions() { //This will result in at most 10 simultaneous Bulk Add requests to the Batch Service. MaxDegreeOfParallelism = 10 }; Console.WriteLine("Submitting {0} tasks to work item: {1}, job: {2}, on pool: {3}", taskCountToCreate, cloudWorkItem.Name, jobName, cloudWorkItem.JobExecutionEnvironment.PoolName); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //Use the AddTask overload which supports a list of tasks for best AddTask performence - internally this method performs an //intelligent submission of tasks in batches in order to limit the number of REST API calls made to the Batch Service. wm.AddTask(cloudWorkItem.Name, jobName, tasksToSubmit, parallelOptions); stopwatch.Stop(); Console.WriteLine("Submitted {0} tasks in {1}", taskCountToCreate, stopwatch.Elapsed); //Delete the work item to ensure the tasks are cleaned up wm.DeleteWorkItem(workItemName); } }