private ReportGenerationQueue GetQueueItem(GenerationDao dao, int reportGenerationQueueId)
 {
     return QueueHelper.GetQueueItem(reportGenerationQueueId);
 }
        /// <summary>
        /// Starts the generation process.
        /// </summary>
        /// <param name="generationDao">The generation DAO.</param>
        /// <param name="queuedReport">The queued report.</param>
        private void StartGenerationProcess(GenerationDao generationDao, ReportGenerationQueue queuedReport)
        {
            this.ReportQueueStatusService.UpdateReportGenerationQueue(new ReportGenerationQueueStatusDto()
            {
                ReportGenerationQueueId = queuedReport.ReportGenerationQueueId,
                ReportStatus = ReportStatus.Processing
            });

            IHostedProcess hostedApp = this.LaunchProcess(queuedReport.ReportGenerationQueueId);

            this._reportGeneratorEngineList.Add(hostedApp);
        }
        /// <summary>
        /// Requeues the orphaned processing items.
        /// If any ReportStatusId == 2 and the ReportGenerationQueueId is not in the list
        /// of threads that is executing the reports anymore, then 'RESET' the item, for regeneration
        /// increase the retry count
        /// </summary>
        /// <param name="generationDao">The generation DAO.</param>
        private void RequeueOrphanedProcessingItems(GenerationDao generationDao)
        {
            List<ReportGenerationQueue> processingReportList = generationDao.FindAll<ReportGenerationQueue>(queue =>
                queue.ReportGenerationStatus == ReportStatus.Processing).ToList();

            if (processingReportList != null && processingReportList.Count > 0)
            {
                foreach (ReportGenerationQueue item in processingReportList)
                {
                    IHostedProcess runningApplication = this._reportGeneratorEngineList.FirstOrDefault(
                        entity => entity.ReportGenerationQueueId == item.ReportGenerationQueueId);

                    if (runningApplication == null)
                    {
                        // this item in set to running in DB, but is not in current executing threads anymore
                        // hence this might be a report that generated a fatal exception, that can be recovered & rescheduled
                        this.ReportQueueStatusService.UpdateReportGenerationQueue(new ReportGenerationQueueStatusDto()
                        {
                            ReportGenerationQueueId = item.ReportGenerationQueueId,
                            NumberOfRetries = item.NumberOfRetries + 1,
                            ReportStatus = ReportStatus.Queued
                        });
                    }
                }
            }
        }
        /// <summary>
        /// Pulls the next report.
        /// </summary>
        /// <param name="generationDao">The generation DAO.</param>
        /// <returns></returns>
        private ReportGenerationQueue PullNextReport(GenerationDao generationDao)
        {
            List<ReportGenerationQueue> scheduledReportList = generationDao
                .FindAll<ReportGenerationQueue>(queue =>
                queue.ReportGenerationStatus == ReportStatus.Queued)
                .OrderBy(entity => entity.ReportGenerationQueueId)
                .ToList();

            scheduledReportList = this.FilterQueueForUser(scheduledReportList);

            ReportGenerationQueue queuedReport = scheduledReportList.FirstOrDefault();
            return queuedReport;
        }
        /// <summary>
        /// Checks the remove completed process.
        /// </summary>
        /// <param name="generationDao">The generation DAO.</param>
        /// <param name="process">The process.</param>
        private void CheckRemoveCompletedProcess(GenerationDao generationDao, IHostedProcess process)
        {
            ReportGenerationQueue processingReport = generationDao.Find<ReportGenerationQueue>(queue =>
                queue.ReportGenerationQueueId == process.ReportGenerationQueueId);

            if (processingReport != null)
            {
                // If the process is completed, cancelled or failed, remove it from the queue
                if (processingReport.ReportGenerationStatus.In(ReportStatus.Completed, ReportStatus.Cancelled, ReportStatus.Failed))
                {
                    if (processingReport.ReportGenerationStatus == ReportStatus.Cancelled)
                    {
                        this.TerminateProcess(process);
                    }

                    this._reportGeneratorEngineList.Remove(process);
                }
                else
                {
                    if (process.HasExited)
                    {
                        this._reportGeneratorEngineList.Remove(process);
                    }
                }
            }
        }
 public ReportGenerationQueueBuilder WithGenerationDao(GenerationDao dao)
 {
     this._generationDao = dao;
     return this;
 }