public override IOjsSubmission RetrieveSubmission()
        {
            lock (this.SubmissionsForProcessing)
            {
                if (this.SubmissionsForProcessing.IsEmpty)
                {
                    var sl = this.submissionsForProcessingData
                             .GetAllUnprocessed()
                             .OrderBy(x => x.Id)
                             .Select(x => x.SubmissionId)
                             .ToList();

                    if (sl.Count > 0)
                    {
                        this.Logger.Information(
                            "Retrieved {SubmissionsCount} submissions from DB: {RetrievedSubmissions}",
                            sl.Count,
                            sl);
                    }

                    sl.ForEach(this.SubmissionsForProcessing.Enqueue);
                }

                var isSubmissionRetrieved = this.SubmissionsForProcessing.TryDequeue(out var submissionId);

                if (!isSubmissionRetrieved)
                {
                    return(null);
                }

                this.Logger.Information("Submission {Submission} retrieved from data store successfully", submissionId);

                this.submission = this.submissionsData.GetById(submissionId);

                this.submissionForProcessing = this.submissionsForProcessingData.GetBySubmission(submissionId);

                if (this.submission == null || this.submissionForProcessing == null)
                {
                    this.Logger.Error("Cannot retrieve submission {Submission} from database", submissionId);
                    return(null);
                }

                this.SetSubmissionToProcessing();
            }

            return(this.GetSubmissionModel());
        }
        private void AddOrUpdateWithNoSaveChanges(int submissionId)
        {
            var submissionForProcessing = this.GetBySubmissionId(submissionId);

            if (submissionForProcessing != null)
            {
                submissionForProcessing.Processing = false;
                submissionForProcessing.Processed  = false;
            }
            else
            {
                submissionForProcessing = new SubmissionForProcessing
                {
                    SubmissionId = submissionId
                };

                this.submissionsForProcessing.Add(submissionForProcessing);
            }
        }
        public override IOjsSubmission RetrieveSubmission()
        {
            lock (this.SubmissionsForProcessing)
            {
                if (this.SubmissionsForProcessing.IsEmpty)
                {
                    this.submissionsForProcessingData
                    .GetAllUnprocessed()
                    .OrderBy(x => x.Id)
                    .Select(x => x.SubmissionId)
                    .ToList()
                    .ForEach(this.SubmissionsForProcessing.Enqueue);
                }

                var isSubmissionRetrieved = this.SubmissionsForProcessing.TryDequeue(out var submissionId);

                if (!isSubmissionRetrieved)
                {
                    return(null);
                }

                this.Logger.InfoFormat($"Submission #{submissionId} retrieved from data store successfully");

                this.submission = this.submissionsData.GetById(submissionId);

                this.submissionForProcessing = this.submissionsForProcessingData.GetBySubmission(submissionId);

                if (this.submission == null || this.submissionForProcessing == null)
                {
                    this.Logger.Error($"Cannot retrieve submission #{submissionId} from database");
                    return(null);
                }

                this.SetSubmissionToProcessing();
            }

            return(this.GetSubmissionModel());
        }
Beispiel #4
0
        public void Start()
        {
            this.logger.Info("SubmissionJob starting...");
            var container = Bootstrap.Container;

            while (!this.stopping)
            {
                using (ThreadScopedLifestyle.BeginScope(container))
                {
                    var data = new OjsData();
                    var submissionsForProccessingData = container.GetInstance <ISubmissionsForProcessingDataService>();
                    var participantScoresData         = container.GetInstance <IParticipantScoresDataService>();

                    Submission submission = null;
                    SubmissionForProcessing submissionForProcessing = null;
                    bool retrievedSubmissionSuccessfully;
                    try
                    {
                        lock (this.submissionsForProcessing)
                        {
                            if (this.submissionsForProcessing.IsEmpty)
                            {
                                var submissions = submissionsForProccessingData
                                                  .GetUnprocessedSubmissions()
                                                  .OrderBy(x => x.Id)
                                                  .Select(x => x.SubmissionId)
                                                  .ToList();

                                submissions.ForEach(this.submissionsForProcessing.Enqueue);
                            }

                            retrievedSubmissionSuccessfully = this.submissionsForProcessing
                                                              .TryDequeue(out var submissionId);

                            if (retrievedSubmissionSuccessfully)
                            {
                                this.logger
                                .InfoFormat($"Submission №{submissionId} retrieved from data store successfully");

                                submission = data.Submissions.GetById(submissionId);

                                submissionForProcessing = submissionsForProccessingData
                                                          .GetBySubmissionId(submissionId);

                                if (submission != null && submissionForProcessing != null && !submission.Processing)
                                {
                                    submissionsForProccessingData.SetToProcessing(submissionForProcessing.Id);
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        this.logger.FatalFormat("Unable to get submission for processing. Exception: {0}", exception);
                        throw;
                    }

                    if (retrievedSubmissionSuccessfully && submission != null && submissionForProcessing != null)
                    {
                        this.BeginProcessingSubmission(
                            submission,
                            submissionForProcessing,
                            data,
                            submissionsForProccessingData,
                            participantScoresData);
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }

            this.logger.Info("SubmissionJob stopped.");
        }
Beispiel #5
0
        private void BeginProcessingSubmission(
            Submission submission,
            SubmissionForProcessing submissionForProcessing,
            IOjsData data,
            ISubmissionsForProcessingDataService submissionsForProccessingData,
            IParticipantScoresDataService participantScoresData)
        {
            submission.ProcessingComment = null;
            try
            {
                data.TestRuns.DeleteBySubmissionId(submission.Id);
                this.ProcessSubmission(submission);
                data.SaveChanges();
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("ProcessSubmission on submission №{0} has thrown an exception: {1}", submission.Id, exception);
                submission.ProcessingComment = $"Exception in ProcessSubmission: {exception.Message}";
            }

            try
            {
                this.CalculatePointsForSubmission(submission);
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("CalculatePointsForSubmission on submission №{0} has thrown an exception: {1}", submission.Id, exception);
                submission.ProcessingComment = $"Exception in CalculatePointsForSubmission: {exception.Message}";
            }

            submission.Processed = true;
            submissionsForProccessingData.SetToProcessed(submissionForProcessing.Id);

            try
            {
                participantScoresData.SaveBySubmission(submission);
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("SaveParticipantScore on submission №{0} has thrown an exception: {1}", submission.Id, exception);
                submission.ProcessingComment = $"Exception in SaveParticipantScore: {exception.Message}";
            }

            try
            {
                submission.CacheTestRuns();
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("CacheTestRuns on submission №{0} has thrown an exception: {1}", submission.Id, exception);
                submission.ProcessingComment = $"Exception in CacheTestRuns: {exception.Message}";
            }

            try
            {
                data.SaveChanges();
            }
            catch (Exception exception)
            {
                this.logger.ErrorFormat("Unable to save changes to the submission №{0}! Exception: {1}", submission.Id, exception);
            }

            this.logger.InfoFormat("Submission №{0} successfully processed", submission.Id);
        }