public async Task RemoveJobFromQueue(long jobId)
        {
            if (jobId == 0)
            {
                throw new ArgumentException("Job id can not be 0");
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                var jobEntity = await context.Job.SingleOrDefaultAsync(x => x.JobId == jobId);

                if (jobEntity == null)
                {
                    throw new ArgumentException($"Job id {jobId} does not exist");
                }

                if (jobEntity.Status != 1) // if already moved, then dont delete
                {
                    throw new ArgumentOutOfRangeException("Job is already moved from ready status, unable to delete");
                }

                context.Job.Remove(jobEntity);
                await context.SaveChangesAsync();
            }
        }
        public async Task <bool> UpdateCrossLoadingStatus(long jobId, JobStatusType status)
        {
            if (jobId == 0)
            {
                throw new ArgumentException("Job id can not be 0");
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                var entity = await context.Job.SingleOrDefaultAsync(x => x.JobId == jobId);

                if (entity == null)
                {
                    throw new ArgumentException($"Job id {jobId} does not exist");
                }

                entity.CrossLoadingStatus   = (short)status;
                entity.DateTimeUpdatedUtc   = _dateTimeProvider.GetNowUtc();
                context.Entry(entity).State = EntityState.Modified;

                await context.SaveChangesAsync();

                return(true);
            }
        }
        public async Task <string> GetTemplate(long jobId, JobStatusType status, JobType jobType, DateTime dateTimeJobSubmittedUtc)
        {
            using (IJobQueueDataContext context = _contextFactory())
            {
                var job = await context.FileUploadJobMetaData.SingleOrDefaultAsync(x => x.JobId == jobId);

                ReturnPeriod period = null;
                if (job != null)
                {
                    period = await GetReturnPeriod(job.CollectionName, dateTimeJobSubmittedUtc);
                }

                var emailTemplate = await
                                    context.JobEmailTemplate.SingleOrDefaultAsync(
                    x => x.JobType == (short)jobType && x.JobStatus == (short)status && x.Active.Value);

                if (emailTemplate == null)
                {
                    return(string.Empty);
                }

                if (period != null)
                {
                    return(emailTemplate.TemplateOpenPeriod);
                }

                return(emailTemplate.TemplateClosePeriod ?? emailTemplate.TemplateOpenPeriod);
            }
        }
Exemple #4
0
        public async Task <string> GetEmailTemplateAsync(CancellationToken cancellationToken, int collectionId)
        {
            using (IJobQueueDataContext context = _jobQueueDataContextFactory())
            {
                var emailTemplate = await
                                    context.JobEmailTemplate.SingleOrDefaultAsync(x => x.CollectionId == collectionId &&
                                                                                  x.Active.Value, cancellationToken);

                return(emailTemplate?.TemplateOpenPeriod ?? string.Empty);
            }
        }
        public async Task <IEnumerable <Jobs.Model.Job> > GetAllJobs()
        {
            var jobs = new List <Jobs.Model.Job>();

            using (IJobQueueDataContext context = _contextFactory())
            {
                var jobEntities = await context.Job.ToListAsync();

                jobEntities.ForEach(x => jobs.Add(JobConverter.Convert(x)));
            }

            return(jobs);
        }
        public async Task <bool> UpdateJob(Jobs.Model.Job job)
        {
            if (job == null)
            {
                throw new ArgumentNullException();
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                Job entity = await context.Job.SingleOrDefaultAsync(x => x.JobId == job.JobId);

                if (entity == null)
                {
                    throw new ArgumentException($"Job id {job.JobId} does not exist");
                }

                bool statusChanged = entity.Status != (short)job.Status;

                JobConverter.Convert(job, entity);
                entity.DateTimeUpdatedUtc = _dateTimeProvider.GetNowUtc();
                context.Entry(entity).Property("RowVersion").OriginalValue = job.RowVersion == null ? null : Convert.FromBase64String(job.RowVersion);
                context.Entry(entity).State = EntityState.Modified;

                if (job.Status == JobStatusType.Ready)
                {
                    context.JobSubmission.Add(new JobSubmission()
                    {
                        DateTimeUtc = _dateTimeProvider.GetNowUtc(),
                        JobId       = job.JobId
                    });
                }

                try
                {
                    await context.SaveChangesAsync();

                    if (statusChanged)
                    {
                        await SendEmailNotification(job);
                    }

                    return(true);
                }
                catch (DbUpdateConcurrencyException exception)
                {
                    throw new Exception(
                              "Save failed. Job details have been changed. Reload the job object and try save again");
                }
            }
        }
        public async Task <IEnumerable <Jobs.Model.Job> > GetJobsByPriorityAsync(int resultCount)
        {
            List <Jobs.Model.Job> jobs = new List <Jobs.Model.Job>();

            using (IJobQueueDataContext context = _contextFactory())
            {
                Job[] jobEntities = await context.Job.FromSql("dbo.GetJobByPriority @ResultCount={0}", resultCount).ToArrayAsync();

                foreach (Job jobEntity in jobEntities)
                {
                    jobs.Add(JobConverter.Convert(jobEntity));
                }
            }

            return(jobs);
        }
        public async Task <bool> UpdateJobStatus(long jobId, JobStatusType status)
        {
            if (jobId == 0)
            {
                throw new ArgumentException("Job id can not be 0");
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                var entity = await context.Job.SingleOrDefaultAsync(x => x.JobId == jobId);

                if (entity == null)
                {
                    throw new ArgumentException($"Job id {jobId} does not exist");
                }

                var statusChanged = entity.Status != (short)status;

                entity.Status               = (short)status;
                entity.DateTimeUpdatedUtc   = _dateTimeProvider.GetNowUtc();
                context.Entry(entity).State = EntityState.Modified;

                if (status == JobStatusType.Ready)
                {
                    context.JobSubmission.Add(new JobSubmission()
                    {
                        DateTimeUtc = _dateTimeProvider.GetNowUtc(),
                        JobId       = jobId
                    });
                }

                await context.SaveChangesAsync();

                if (statusChanged)
                {
                    await SendEmailNotification(jobId);
                }

                return(true);
            }
        }
        public async Task <Jobs.Model.Job> GetJobById(long jobId)
        {
            if (jobId == 0)
            {
                throw new ArgumentException("Job id can not be 0");
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                var entity = await context.Job.SingleOrDefaultAsync(x => x.JobId == jobId);

                if (entity == null)
                {
                    throw new ArgumentException($"Job id {jobId} does not exist");
                }

                var job = new Jobs.Model.Job();
                JobConverter.Convert(entity, job);
                return(job);
            }
        }
        public async Task <long> AddJob(Jobs.Model.Job job)
        {
            if (job == null)
            {
                throw new ArgumentNullException();
            }

            using (IJobQueueDataContext context = _contextFactory())
            {
                var entity = new Job
                {
                    DateTimeSubmittedUtc = _dateTimeProvider.GetNowUtc(),
                    JobType            = (short)job.JobType,
                    Priority           = job.Priority,
                    Status             = (short)job.Status,
                    SubmittedBy        = job.SubmittedBy,
                    NotifyEmail        = job.NotifyEmail,
                    CrossLoadingStatus = (await IsCrossLoadingEnabled(job.JobType)) ? (short)JobStatusType.Ready : (short?)null
                };
                context.Job.Add(entity);
                context.SaveChanges();
                return(entity.JobId);
            }
        }