예제 #1
0
        /// <summary>
        /// Creates a job in the specified pool.
        /// </summary>
        /// <param name="batchClient">A <see cref="BatchClient"/>.</param>
        /// <param name="jobId">The id of the job to be created.</param>
        /// <param name="poolId">The id of the <see cref="CloudPool"/> in which to create the job.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns>
        private static async Task CreateJobAsync(BatchClient batchClient, string jobId, string poolId, IList <ResourceFile> resourceFiles)
        {
            Console.WriteLine("Creating job [{0}]...", jobId);

            CloudPool pool = batchClient.PoolOperations.GetPool(PoolId);

            //pool.StartTask = new StartTask
            //{
            //    CommandLine = "cmd /c (robocopy %AZ_BATCH_TASK_WORKING_DIR% %AZ_BATCH_NODE_SHARED_DIR%) ^& IF %ERRORLEVEL% LEQ 1 exit 0",
            //    ResourceFiles = resourceFiles,
            //    WaitForSuccess = true
            //};

            //if (pool.CurrentDedicatedComputeNodes == 0 && pool.CurrentLowPriorityComputeNodes == 0)
            //{
            //    Console.WriteLine("We are creating nodes");
            //    pool.Resize(0, 2, TimeSpan.FromMinutes(30), ComputeNodeDeallocationOption.Requeue);
            //}

            CloudJob job = batchClient.JobOperations.CreateJob();

            job.Id = jobId;
            job.PoolInformation = new PoolInformation {
                PoolId = poolId
            };
            //job.UsesTaskDependencies = true;
            JobConstraints nConst = new JobConstraints(TimeSpan.FromMinutes(30), 0);

            job.Constraints = nConst;

            await job.CommitAsync();
        }
        private static void AssertJobCorrectness(
            JobOperations jobOperations,
            string jobId,
            ref CloudJob boundJob,
            string expectedPoolId,
            int expectedPriority,
            JobConstraints expectedJobConstraints)
        {
            //boundJob.Refresh();
            boundJob = jobOperations.GetJob(jobId); //TODO: Have to do this due to parent prop item loss on commit
            Assert.Equal(expectedPriority, boundJob.Priority);
            Assert.Equal(expectedPoolId, boundJob.ExecutionInformation.PoolId);

            if (expectedJobConstraints != null)
            {
                Assert.NotNull(boundJob.Constraints);
                Assert.Equal(expectedJobConstraints.MaxTaskRetryCount, boundJob.Constraints.MaxTaskRetryCount);
                Assert.Equal(expectedJobConstraints.MaxWallClockTime, boundJob.Constraints.MaxWallClockTime);
            }
        }
        public void Bug1433069TestBoundJobCommit()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-TestBoundJobCommit";

                try
                {
                    //
                    // Create the job
                    //
                    CloudJob cloudJob = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                    cloudJob.PoolInformation = new PoolInformation()
                    {
                        PoolId = poolFixture.PoolId
                    };

                    testOutputHelper.WriteLine("Initial job schedule commit()");
                    cloudJob.Commit();

                    //Get the job
                    CloudJob refreshableJob = batchCli.JobOperations.GetJob(jobId);

                    //Update the bound job priority
                    const int          newJobPriority        = 5;
                    OnAllTasksComplete newOnAllTasksComplete = OnAllTasksComplete.NoAction;

                    testOutputHelper.WriteLine("Job priority is: {0}", refreshableJob.Priority);
                    refreshableJob.Priority           = newJobPriority;
                    refreshableJob.OnAllTasksComplete = newOnAllTasksComplete;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, poolFixture.PoolId, newJobPriority, null);

                    //Update the bound job pool name
                    //Must disable the job first before updating its pool
                    refreshableJob.Disable(DisableJobOption.Terminate);

                    //Wait for job to reach disabled state (could go to Disabling for a bit)
                    //TODO: Use a uBtilities wait helper here
                    DateTime jobDisabledStateWaitStartTime = DateTime.UtcNow;
                    TimeSpan jobDisabledTimeout            = TimeSpan.FromSeconds(120);
                    while (refreshableJob.State != JobState.Disabled)
                    {
                        testOutputHelper.WriteLine("Bug1433069TestBoundJobCommit: sleeping for (refreshableJob.State != JobState.Disabled)");
                        Thread.Sleep(TimeSpan.FromSeconds(10));
                        refreshableJob = batchCli.JobOperations.GetJob(jobId);

                        if (DateTime.UtcNow > jobDisabledStateWaitStartTime.Add(jobDisabledTimeout))
                        {
                            Assert.False(true, "Timed out waiting for job to go to disabled state");
                        }
                    }

                    const string newPoolId = "testPool";
                    refreshableJob.PoolInformation.PoolId = newPoolId;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, newPoolId, newJobPriority, null);

                    //Enable the job again
                    refreshableJob.Enable();

                    //Update the bound job constraints
                    JobConstraints newJobConstraints = new JobConstraints(TimeSpan.FromSeconds(200), 19);
                    refreshableJob.Constraints = newJobConstraints;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, newPoolId, newJobPriority, newJobConstraints);
                }
                finally
                {
                    batchCli.JobOperations.DeleteJob(jobId);
                }
            }

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
        public static void DisplayJobScheduleLong(ITestOutputHelper testOutputHelper, CloudJobSchedule curWI)
        {
            // job schedule top level simple properties
            testOutputHelper.WriteLine("Id:  " + curWI.Id);
            testOutputHelper.WriteLine("       State: " + curWI.State.ToString());
            testOutputHelper.WriteLine("       " + "URL: " + curWI.Url);
            testOutputHelper.WriteLine("       " + "LastModified: " + (curWI.LastModified.HasValue ? curWI.LastModified.Value.ToLongDateString() : "<null>"));

            // execution INFO
            {
                JobScheduleExecutionInformation wiExInfo = curWI.ExecutionInformation;

                testOutputHelper.WriteLine("       ExeInfo:");
                testOutputHelper.WriteLine("               LastUpdateTime: " + (wiExInfo.EndTime.HasValue ? wiExInfo.EndTime.Value.ToLongDateString() : "<null>"));
                testOutputHelper.WriteLine("               NextRuntime: " + (wiExInfo.NextRunTime.HasValue ? wiExInfo.NextRunTime.Value.ToLongDateString() : "<null>"));
                testOutputHelper.WriteLine("               RecentJob:");

                // RecentJob
                RecentJob rj = wiExInfo.RecentJob;

                if (null == rj)
                {
                    testOutputHelper.WriteLine(" <null>");
                }
                else
                {
                    testOutputHelper.WriteLine("                         Id: " + rj.Id);
                    testOutputHelper.WriteLine("                         Url: " + rj.Url);
                }
            }

            // JobSpecification
            JobSpecification jobSpec = curWI.JobSpecification;

            testOutputHelper.WriteLine("       JobSpecification:");

            if (null == jobSpec)
            {
                testOutputHelper.WriteLine(" <null>");
            }
            else
            {
                testOutputHelper.WriteLine("");
                testOutputHelper.WriteLine("           Priority: " + (jobSpec.Priority.HasValue ? jobSpec.Priority.ToString() : "<null>"));

                JobConstraints jobCon = jobSpec.Constraints;

                testOutputHelper.WriteLine("           Constraints: ");

                if (null == jobCon)
                {
                    testOutputHelper.WriteLine("null");
                }
                else
                {
                    testOutputHelper.WriteLine("");
                    testOutputHelper.WriteLine("             MaxTaskRetryCount: " + (jobCon.MaxTaskRetryCount.HasValue ? jobSpec.Constraints.MaxTaskRetryCount.Value.ToString() : "<null>"));
                    testOutputHelper.WriteLine("             MaxWallClockTime: " + (jobCon.MaxWallClockTime.HasValue ? jobSpec.Constraints.MaxWallClockTime.Value.TotalMilliseconds.ToString() : "<null>"));
                }

                JobManagerTask ijm = jobSpec.JobManagerTask;

                if (null == ijm)
                {
                    testOutputHelper.WriteLine("<null>");
                }
                else
                {
                    testOutputHelper.WriteLine("           JobManagerTask:");
                    testOutputHelper.WriteLine("               CommandLine        : " + ijm.CommandLine);
                    testOutputHelper.WriteLine("               KillJobOnCompletion: " + (ijm.KillJobOnCompletion.HasValue ? ijm.KillJobOnCompletion.Value.ToString() : "<null>"));
                    testOutputHelper.WriteLine("               Id                 : " + ijm.Id);
                    testOutputHelper.WriteLine("               RunExclusive       : " + (ijm.RunExclusive.HasValue ? ijm.RunExclusive.Value.ToString() : "<null>"));

                    IEnumerable <EnvironmentSetting> envSettings = ijm.EnvironmentSettings;

                    if (null != envSettings)
                    {
                        List <EnvironmentSetting> envSettingsList = new List <EnvironmentSetting>(ijm.EnvironmentSettings);
                        testOutputHelper.WriteLine("               EnvironmentSettings.count:" + envSettingsList.Count);
                    }
                    else
                    {
                        testOutputHelper.WriteLine("               EnvironmentSettings: <null>");
                    }

                    IEnumerable <ResourceFile> resFilesProp = ijm.ResourceFiles;

                    if (null != resFilesProp)
                    {
                        List <ResourceFile> resFiles = new List <ResourceFile>();
                        testOutputHelper.WriteLine("               ResourceFiles.count:" + resFiles.Count);
                    }
                    else
                    {
                        testOutputHelper.WriteLine("               ResourceFiles: <null>");
                    }

                    TaskConstraints tc = ijm.Constraints;

                    if (null == tc)
                    {
                        testOutputHelper.WriteLine("               TaskConstraints: <null>");
                    }
                    else
                    {
                        testOutputHelper.WriteLine("               TaskConstraints: ");
                        testOutputHelper.WriteLine("                   MaxTaskRetryCount: " + (tc.MaxTaskRetryCount.HasValue ? tc.MaxTaskRetryCount.Value.ToString() : "<null>"));
                        testOutputHelper.WriteLine("                   MaxWallClockTime: " + (tc.MaxWallClockTime.HasValue ? tc.MaxWallClockTime.Value.TotalMilliseconds.ToString() : "<null>"));
                        testOutputHelper.WriteLine("                   RetentionTime: " + (tc.RetentionTime.HasValue ? tc.RetentionTime.Value.TotalMilliseconds.ToString() : "<null>"));
                    }

                    if (ijm.UserIdentity != null)
                    {
                        testOutputHelper.WriteLine("               UserIdentity: ");
                        testOutputHelper.WriteLine("                   UserName: "******"                   ElevationLevel: ", ijm.UserIdentity.AutoUser?.ElevationLevel);
                        testOutputHelper.WriteLine("                   Scope: ", ijm.UserIdentity.AutoUser?.Scope);
                    }
                }
            }


            // metadata
            {
                IEnumerable <MetadataItem> mdis = curWI.Metadata;

                testOutputHelper.WriteLine("       Metadata: ");

                if (null == mdis)
                {
                    testOutputHelper.WriteLine("<null>");
                }
                else
                {
                    List <MetadataItem> meta = new List <MetadataItem>(curWI.Metadata);

                    testOutputHelper.WriteLine(" count:" + meta.Count);
                }
            }

            // schedule
            Schedule sched = curWI.Schedule;

            if (null == sched)
            {
                testOutputHelper.WriteLine("       Schedule: <null>");
            }
            else
            {
                testOutputHelper.WriteLine("       Schedule:");
                testOutputHelper.WriteLine("           DoNotRunAfter:" + (sched.DoNotRunAfter.HasValue ? sched.DoNotRunAfter.Value.ToLongDateString() : "<null>"));
                testOutputHelper.WriteLine("           DoNotRunUntil: " + (sched.DoNotRunUntil.HasValue ? sched.DoNotRunUntil.Value.ToLongDateString() : "<null>"));
                testOutputHelper.WriteLine("           RecurrenceInterval: " + (sched.RecurrenceInterval.HasValue ? sched.RecurrenceInterval.Value.TotalMilliseconds.ToString() : "<null>"));
                testOutputHelper.WriteLine("           StartWindow       :" + (sched.StartWindow.HasValue ? sched.StartWindow.Value.TotalMilliseconds.ToString() : "<null>"));
            }

            // stats
            JobScheduleStatistics stats = curWI.Statistics;

            if (null == stats)
            {
                testOutputHelper.WriteLine("       Stats: <null>");
            }
            else
            {
                testOutputHelper.WriteLine("       Stats:");
                testOutputHelper.WriteLine("           LastUpdateTime: " + stats.LastUpdateTime.ToLongDateString());
                testOutputHelper.WriteLine("           KernelCPUTime: " + stats.KernelCpuTime.TotalMilliseconds.ToString());
                testOutputHelper.WriteLine("           NumFailedTasks: " + stats.FailedTaskCount.ToString());
                testOutputHelper.WriteLine("           NumTimesCalled    : " + stats.TaskRetryCount);
                testOutputHelper.WriteLine("           NumSucceededTasks: " + stats.SucceededTaskCount);
                testOutputHelper.WriteLine("           ReadIOGiB      : " + stats.ReadIOGiB);
                testOutputHelper.WriteLine("           ReadIOps         : " + stats.ReadIOps);
                testOutputHelper.WriteLine("           StartTime        : " + stats.StartTime.ToLongDateString());
                testOutputHelper.WriteLine("           Url              : " + stats.Url);
                testOutputHelper.WriteLine("           UserCpuTime      : " + stats.UserCpuTime.TotalMilliseconds.ToString());
                testOutputHelper.WriteLine("           WaitTime         : " + stats.WaitTime.TotalMilliseconds.ToString());
                testOutputHelper.WriteLine("           WallClockTime    : " + stats.WallClockTime.TotalMilliseconds.ToString());
                testOutputHelper.WriteLine("           WriteIOGiB     : " + stats.WriteIOGiB);
                testOutputHelper.WriteLine("           WriteIOps        : " + stats.WriteIOps);
            }
        }