Ejemplo n.º 1
0
        public static CloudTask WithOutputFile(
            this CloudTask task,
            string pattern,
            string containerUrl,
            TaskOutputKind outputKind,
            OutputFileUploadCondition uploadCondition)
        {
            var  outputBase = task.GetOutputStoragePath(outputKind);
            bool patternContainsWildcard = pattern.Contains("*");
            var  path = patternContainsWildcard ? outputBase : $"{outputBase}/{pattern}";

            return(task.WithOutputFile(
                       pattern,
                       containerUrl,
                       path,
                       uploadCondition));
        }
Ejemplo n.º 2
0
        public static CloudTask WithOutputFile(
            this CloudTask task,
            string pattern,
            string containerUrl,
            TaskOutputKind outputKind,
            OutputFileUploadCondition uploadCondition)
        {
            Func <string> pathFunc = () =>
            {
                bool patternContainsWildcard = pattern.Contains("*");

                return(patternContainsWildcard ? $"{task.Id}\\${outputKind}" : $"{task.Id}\\${outputKind}\\{pattern}");
            };

            return(task.WithOutputFile(
                       pattern,
                       containerUrl,
                       pathFunc,
                       uploadCondition));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Runs a series of tasks, using the OutputFiles feature in conjunction with the file conventions library to upload the tasks to a container.
        /// Then downloads the files from the container to the local machine.
        /// </summary>
        public static async Task <CloudBlobContainer> RunWithConventions(
            BatchClient batchClient,
            CloudStorageAccount linkedStorageAccount,
            string poolId,
            int nodeCount,
            string jobId)
        {
            await CreatePoolAsync(batchClient, poolId, nodeCount);

            CloudJob job = batchClient.JobOperations.CreateJob(jobId, new PoolInformation {
                PoolId = poolId
            });

            // Get the container URL to use
            string             containerName = job.OutputStorageContainerName();
            CloudBlobContainer container     = linkedStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync();

            string containerUrl = job.GetOutputStorageContainerUrl(linkedStorageAccount);

            // Commit the job to the Batch service
            await job.CommitAsync();

            Console.WriteLine($"Created job {jobId}");

            // Obtain the bound job from the Batch service
            await job.RefreshAsync();

            // Create a series of simple tasks which dump the task environment to a file and then write random values to a text file
            IEnumerable <CloudTask> tasksToAdd = Enumerable.Range(1, 20).Select(i =>
            {
                var taskId = i.ToString().PadLeft(3, '0');
                var task   = new CloudTask(taskId, "cmd /v:ON /c \"echo off && set && (FOR /L %i IN (1,1,100000) DO (ECHO !RANDOM!)) > output.txt\"");

                task.WithOutputFile(@"..\std*.txt", containerUrl, TaskOutputKind.TaskLog, OutputFileUploadCondition.TaskCompletion)
                .WithOutputFile(@"output.txt", containerUrl, TaskOutputKind.TaskOutput, OutputFileUploadCondition.TaskSuccess);

                return(task);
            }
                                                                                );

            // Add the tasks to the job; the tasks are automatically
            // scheduled for execution on the nodes by the Batch service.
            await job.AddTaskAsync(tasksToAdd);

            Console.WriteLine($"All tasks added to job {job.Id}");
            Console.WriteLine();

            Console.WriteLine($"Downloading outputs to {Directory.GetCurrentDirectory()}");

            foreach (CloudTask task in job.CompletedTasks())
            {
                if (task.ExecutionInformation.Result != TaskExecutionResult.Success)
                {
                    Console.WriteLine($"Task {task.Id} failed");
                    Console.WriteLine(SampleHelpers.GetFailureInfoDetails(task.ExecutionInformation.FailureInformation));
                }
                else
                {
                    Console.WriteLine($"Task {task.Id} completed successfully");
                }

                foreach (OutputFileReference output in task.OutputStorage(linkedStorageAccount).ListOutputs(TaskOutputKind.TaskOutput))
                {
                    Console.WriteLine($"output file: {output.FilePath}");
                    await output.DownloadToFileAsync($"{jobId}-{output.FilePath}", System.IO.FileMode.Create);
                }
            }

            return(container);
        }