// Lists the node files under a task.
        private IEnumerable <PSNodeFile> ListNodeFilesByTask(ListNodeFileOptions options)
        {
            // Get the single node file matching the specified name
            if (!string.IsNullOrEmpty(options.NodeFileName))
            {
                WriteVerbose(string.Format(Resources.GetNodeFileByTaskByName, options.NodeFileName, options.TaskId));
                JobOperations jobOperations = options.Context.BatchOMClient.JobOperations;
                NodeFile      nodeFile      = jobOperations.GetNodeFile(options.JobId, options.TaskId, options.NodeFileName, options.AdditionalBehaviors);
                PSNodeFile    psNodeFile    = new PSNodeFile(nodeFile);
                return(new PSNodeFile[] { psNodeFile });
            }
            // List node files using the specified filter
            else
            {
                string           taskId           = options.Task == null ? options.TaskId : options.Task.Id;
                ODATADetailLevel odata            = null;
                string           verboseLogString = null;
                if (!string.IsNullOrEmpty(options.Filter))
                {
                    verboseLogString = string.Format(Resources.GetNodeFileByTaskByOData, taskId);
                    odata            = new ODATADetailLevel(filterClause: options.Filter);
                }
                else
                {
                    verboseLogString = string.Format(Resources.GetNodeFileByTaskNoFilter, taskId);
                }
                WriteVerbose(verboseLogString);

                IPagedEnumerable <NodeFile> nodeFiles = null;
                if (options.Task != null)
                {
                    nodeFiles = options.Task.omObject.ListNodeFiles(options.Recursive, odata, options.AdditionalBehaviors);
                }
                else
                {
                    JobOperations jobOperations = options.Context.BatchOMClient.JobOperations;
                    nodeFiles = jobOperations.ListNodeFiles(options.JobId, options.TaskId, options.Recursive, odata, options.AdditionalBehaviors);
                }
                Func <NodeFile, PSNodeFile> mappingFunction = f => { return(new PSNodeFile(f)); };
                return(PSPagedEnumerable <PSNodeFile, NodeFile> .CreateWithMaxCount(
                           nodeFiles, mappingFunction, options.MaxCount, () => WriteVerbose(string.Format(Resources.MaxCount, options.MaxCount))));
            }
        }
        /// <summary>
        /// Downloads a node file using the specified options.
        /// </summary>
        /// <param name="options">The download options.</param>
        public void DownloadNodeFile(DownloadNodeFileOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            NodeFile nodeFile = null;

            switch (options.NodeFileType)
            {
            case PSNodeFileType.Task:
            {
                JobOperations jobOperations = options.Context.BatchOMClient.JobOperations;
                nodeFile = jobOperations.GetNodeFile(options.JobId, options.TaskId, options.NodeFileName, options.AdditionalBehaviors);
                break;
            }

            case PSNodeFileType.ComputeNode:
            {
                PoolOperations poolOperations = options.Context.BatchOMClient.PoolOperations;
                nodeFile = poolOperations.GetNodeFile(options.PoolId, options.ComputeNodeId, options.NodeFileName, options.AdditionalBehaviors);
                break;
            }

            case PSNodeFileType.PSNodeFileInstance:
            {
                nodeFile = options.NodeFile.omObject;
                break;
            }

            default:
            {
                throw new ArgumentException(Resources.NoNodeFile);
            }
            }

            DownloadNodeFileByInstance(nodeFile, options.DestinationPath, options.Stream, options.AdditionalBehaviors);
        }
예제 #3
0
        public void TestGetNodeFileByTask()
        {
            Action test = () =>
            {
                using (BatchClient batchCli = TestUtilities.OpenBatchClientAsync(TestUtilities.GetCredentialsFromEnvironment()).Result)
                {
                    JobOperations jobOperations = batchCli.JobOperations;

                    string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-" + nameof(TestGetNodeFileByTask);
                    try
                    {
                        //
                        // Create the job
                        //
                        CloudJob job = jobOperations.CreateJob(jobId, new PoolInformation());
                        job.PoolInformation = new PoolInformation()
                        {
                            PoolId = this.poolFixture.PoolId
                        };

                        this.testOutputHelper.WriteLine("Initial job schedule commit()");

                        job.Commit();

                        //
                        // Wait for the job
                        //
                        this.testOutputHelper.WriteLine("Waiting for job");
                        CloudJob boundJob = jobOperations.GetJob(jobId);

                        //
                        // Add task to the job
                        //
                        const string taskId      = "T1";
                        const string taskMessage = "This is a test";

                        this.testOutputHelper.WriteLine("Adding task: {0}", taskId);
                        CloudTask task = new CloudTask(taskId, string.Format("cmd /c echo {0}", taskMessage));
                        boundJob.AddTask(task);

                        //
                        // Wait for the task to complete
                        //
                        this.testOutputHelper.WriteLine("Waiting for the task to complete");
                        Utilities        utilities        = batchCli.Utilities;
                        TaskStateMonitor taskStateMonitor = utilities.CreateTaskStateMonitor();

                        //Wait for the task state to be running
                        taskStateMonitor.WaitAll(
                            jobOperations.ListTasks(jobId),
                            TaskState.Completed,
                            TimeSpan.FromSeconds(30));

                        //Download the data
                        this.testOutputHelper.WriteLine("Downloading the stdout for the file");
                        NodeFile file = jobOperations.GetNodeFile(jobId, taskId, Constants.StandardOutFileName);
                        string   data = file.ReadAsString();
                        this.testOutputHelper.WriteLine("Data: {0}", data);
                        Assert.Contains(taskMessage, data);

                        // Download the data again using the JobOperations read file content helper
                        data = batchCli.JobOperations.CopyNodeFileContentToString(jobId, taskId, Constants.StandardOutFileName);
                        this.testOutputHelper.WriteLine("Data: {0}", data);
                        Assert.Contains(taskMessage, data);
                    }
                    finally
                    {
                        jobOperations.DeleteJob(jobId);
                    }
                }
            };

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }