/// <summary> /// Creates a new task /// </summary> /// <param name="parameters">The parameters to use when creating the task</param> public void CreateTask(NewTaskParameters parameters) { if (parameters == null) { throw new ArgumentNullException("parameters"); } CloudTask task = new CloudTask(parameters.TaskName, parameters.CommandLine); task.RunElevated = parameters.RunElevated; if (parameters.EnvironmentSettings != null) { task.EnvironmentSettings = new List <IEnvironmentSetting>(); foreach (DictionaryEntry d in parameters.EnvironmentSettings) { EnvironmentSetting setting = new EnvironmentSetting(d.Key.ToString(), d.Value.ToString()); task.EnvironmentSettings.Add(setting); } } if (parameters.ResourceFiles != null) { task.ResourceFiles = new List <IResourceFile>(); foreach (DictionaryEntry d in parameters.ResourceFiles) { ResourceFile file = new ResourceFile(d.Value.ToString(), d.Key.ToString()); task.ResourceFiles.Add(file); } } if (parameters.AffinityInformation != null) { task.AffinityInformation = parameters.AffinityInformation.omObject; } if (parameters.TaskConstraints != null) { task.TaskConstraints = parameters.TaskConstraints.omObject; } WriteVerbose(string.Format(Resources.NBT_CreatingTask, parameters.TaskName)); if (parameters.Job != null) { parameters.Job.omObject.AddTask(task, parameters.AdditionalBehaviors); } else { using (IWorkItemManager wiManager = parameters.Context.BatchOMClient.OpenWorkItemManager()) { wiManager.AddTask(parameters.WorkItemName, parameters.JobName, task, parameters.AdditionalBehaviors); } } }
/// <summary> /// Creates a test Task for use in Scenario tests. /// TODO: Replace with new Task client method when it exists. /// </summary> public static void CreateTestTask(BatchAccountContext context, string workItemName, string jobName, string taskName) { if (HttpMockServer.Mode == HttpRecorderMode.Record) { using (IWorkItemManager wiManager = context.BatchOMClient.OpenWorkItemManager()) { ICloudTask task = new CloudTask(taskName, "cmd /c dir /s"); wiManager.AddTask(workItemName, jobName, task); } } }
/// <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); } }