Ejemplo n.º 1
0
        public OneTimeJobRawResult ExecuteJob(
            JobDescription jobDescription,
            CredentialBase jobCredential = null)
        {
            if (this.listenerUri == string.Empty)
            {
                JobSchedulerEventServicev2.log.Error((object)"ListenerUri remains uninitialized");
                return(new OneTimeJobRawResult()
                {
                    Success = false,
                    Error = Resources.get_TestErrorJobFailed()
                });
            }
            if (jobCredential != null)
            {
                jobDescription.set_Credential(this.EncryptCredentials(jobCredential));
            }
            if (jobDescription.get_SupportedRoles() == null)
            {
                jobDescription.set_SupportedRoles((PackageType)7);
            }
            ScheduledJob scheduledJob = new ScheduledJob();

            scheduledJob.set_NotificationAddress(this.listenerUri);
            scheduledJob.set_State("CoreOneTimeJob");
            scheduledJob.set_RunOnce(true);
            scheduledJob.set_IsOneShot(true);
            scheduledJob.set_Job(jobDescription);
            ScheduledJob job = scheduledJob;
            Guid         scheduler;

            try
            {
                scheduler = this.SubmitScheduledJobToScheduler(job);
                OneTimeJobManager.Logger.DebugFormat("Job {0} scheduled", (object)scheduler);
            }
            catch (Exception ex)
            {
                OneTimeJobManager.Logger.ErrorFormat("Failed to submit job: {0}", (object)ex);
                OneTimeJobRawResult timeJobRawResult = new OneTimeJobRawResult();
                timeJobRawResult.Success          = false;
                timeJobRawResult.Error            = Resources.get_TestErrorJobFailed();
                timeJobRawResult.ExceptionFromJob = ex;
                timeJobRawResult = timeJobRawResult;
                return(timeJobRawResult);
            }
            TimeSpan timeout = jobDescription.get_Timeout().Add(this.jobTimeoutTolerance);

            OneTimeJobManager.PendingJobItem pendingJobItem = new OneTimeJobManager.PendingJobItem();
            this.pendingJobs[scheduler] = pendingJobItem;
            if (this.JobStarted != null)
            {
                this.JobStarted((object)this, (EventArgs) new OneTimeJobManager.JobStartedEventArgs(scheduler));
            }
            OneTimeJobRawResult timeJobRawResult1;

            if (pendingJobItem.WaitHandle.WaitOne(timeout))
            {
                timeJobRawResult1 = pendingJobItem.RawResult;
            }
            else
            {
                OneTimeJobManager.Logger.ErrorFormat("No result from job {0} received before timeout ({1})", (object)scheduler, (object)timeout);
                timeJobRawResult1 = new OneTimeJobRawResult()
                {
                    Success = false,
                    Error   = Resources.get_TestErrorTimeout()
                };
            }
            this.pendingJobs.TryRemove(scheduler, out pendingJobItem);
            return(timeJobRawResult1);
        }