Example #1
0
        private static async Task <JobResult> ExecuteJob(CloudStampyParameters queueItem, StampyJobType requestedJobType, TimeSpan timeout)
        {
            JobResult jobResult    = null;
            Exception jobException = null;

            var sw  = new Stopwatch();
            var job = JobFactory.GetJob(eventsLogger, queueItem, requestedJobType);

            if (job != null)
            {
                try
                {
                    sw.Start();
                    eventsLogger.WriteInfo(queueItem, "start job");
                    var jobResultTask = Task.Run(() => job.Execute());
                    var timeoutTask   = Task.Run(async() => await Task.Delay(timeout));

                    Task finishedTask;

                    while (true)
                    {
                        var timerTask = Task.Run(async() => await Task.Delay(TimeSpan.FromMinutes(1)));
                        finishedTask = await Task.WhenAny(new Task[] { jobResultTask, timerTask, timeoutTask });

                        //log the progress of the job
                        resultsLogger.WriteJobProgress(queueItem.RequestId, queueItem.JobId, requestedJobType, job.JobStatus.ToString(), job.ReportUri);

                        if (finishedTask.Id == jobResultTask.Id)
                        {
                            //job is done
                            jobResult = await jobResultTask;
                            break;
                        }
                        else if (finishedTask.Id == timeoutTask.Id)
                        {
                            jobResult = new JobResult {
                                JobStatus = Status.Cancelled
                            };
                            eventsLogger.WriteError(queueItem, $"Cancel job. {job.GetType().ToString()} took longer than timeout of {timeout.TotalMinutes.ToString()}mins");
                            var isCancelled = await job.Cancel();

                            if (!isCancelled)
                            {
                                eventsLogger.WriteError(queueItem, "Error while cancelling job");
                            }
                            else
                            {
                                eventsLogger.WriteInfo(queueItem, "Cancelled job successfully");
                            }

                            break;
                        }
                    }
                    sw.Stop();
                }
                catch (Exception ex)
                {
                    jobException = ex;
                    jobResult    = new JobResult {
                        JobStatus = Status.Failed
                    };
                    eventsLogger.WriteError(queueItem, "Error while running job", ex);
                    throw new JobExecutionException(job.GetType().Name, "Error while running job", ex);
                }
                finally
                {
                    resultsLogger.WriteResult(queueItem.RequestId, queueItem.JobId, requestedJobType, jobResult.JobStatus.ToString(), (int)sw.Elapsed.TotalMinutes, job.ReportUri, jobException);
                }
            }
            else
            {
                eventsLogger.WriteError(queueItem, "Cannot run this job");
            }

            return(jobResult);
        }
Example #2
0
 public void WriteResult(string requestId, string jobId, StampyJobType jobType, string status, int jobDurationMinutes, string jobUri, Exception ex)
 {
     WriteEvent(DateTime.UtcNow, requestId, jobId, jobType, status, jobDurationMinutes, jobUri, ex != null ? ex.GetType().ToString() : "", ex != null ? ex.ToString() : "");
 }
Example #3
0
        public static IJob GetJob(ICloudStampyLogger logger, CloudStampyParameters args, StampyJobType requestedJobType)
        {
            IJob job = null;

            switch (requestedJobType)
            {
            case StampyJobType.None:
                break;

            case StampyJobType.CreateService:
                job = new ServiceCreationJob(logger, args);
                break;

            case StampyJobType.Build:
                job = new BuildJob(logger, args);
                break;

            case StampyJobType.Deploy:
                job = new DeploymentJob(logger, args);
                break;

            case StampyJobType.Test:
                job = new TestJob(logger, args);
                break;

            case StampyJobType.RemoveResources:
                job = new ScavengerJob(logger, args);
                break;

            default:
                break;
            }

            return(job);
        }
Example #4
0
 public void WriteJobProgress(string requestId, string jobId, StampyJobType jobType, string status, string jobUri)
 {
     WriteEvent(DateTime.UtcNow, requestId, jobId, jobType, status, null, jobUri, null, null);
 }