public void ProcessJobs()
        {
            IJobsRepository jobsRepository = new JobsRepository(_ctx);
            var             allJobs        = jobsRepository.GetAllJobs();
            var             jobsToProcess  = allJobs.Where(x => x.Status == JobStatus.New).ToList();

            jobsToProcess.ForEach(job => job.ChangeStatus(JobStatus.InProgress));

            _ctx.SaveChanges();

            Parallel.ForEach(jobsToProcess, (currentjob) =>
            {
                new Task(async() =>
                {
                    bool result = await this.ProcessJob(currentjob).ConfigureAwait(false);
                    if (result)
                    {
                        currentjob.ChangeStatus(JobStatus.Done);
                    }
                    else
                    {
                        _ctx.SaveChanges();
                        currentjob.ChangeStatus(JobStatus.Failed);
                    }
                }).Start();
            });

            _ctx.SaveChanges();
        }
Ejemplo n.º 2
0
        public async Task ProcessJobs()
        {
            var jobsToProcess = _dbContext.Jobs.Where(x => JobStatusesToProcess.Contains(x.Status) && (x.DoAfter == null || x.DoAfter < DateTime.Today)).ToList();

            foreach (var job in jobsToProcess)
            {
                var _ = Task.Run(async() =>
                {
                    var result = await ProcessJob(job);

                    job.Status = result;
                    _dbContext.SaveChanges();
                });
            }
        }
Ejemplo n.º 3
0
        public void CreateJob(CreateJobRequest jobRequest)
        {
            var newJob = new Job()
            {
                DoAfter = jobRequest.DoAfter,
                Name    = jobRequest.Name,
                Status  = JobStatus.New,
                Logs    = new List <Log>()
                {
                    new Log
                    {
                        CreatedAt   = DateTime.Now,
                        Description = "Job created."
                    }
                }
            };

            _dbContext.Jobs.Add(newJob);
            _dbContext.SaveChanges();
        }
Ejemplo n.º 4
0
        public ActionResult Create(string name, DateTime doAfter)
        {
            try
            {
                using (ZavenDotNetInterviewContext _ctx = new ZavenDotNetInterviewContext())
                {
                    Job newJob = new Job()
                    {
                        Id = Guid.NewGuid(), DoAfter = doAfter, Name = name, Status = JobStatus.New
                    };
                    newJob = _ctx.Jobs.Add(newJob);
                    _ctx.SaveChanges();
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Ejemplo n.º 5
0
 public void SaveChanges()
 {
     _ctx.SaveChanges();
 }