public async Task StartProcessesAsync() { var newProcess = _mapper.Map <ProcessModel, Process>( new ProcessModel() { Created = DateTime.Now, ProcessId = Guid.NewGuid(), Status = Common.Models.ProcessStatus.Queued }); await _context.Processes.AddAsync(newProcess); await _context.SaveChangesAsync(); _messageBusService.Send("processes", newProcess); }
public Task StartAsync(CancellationToken cancellationToken) { //TODO add logging _messageBusService.GetChannel <ProcessModel>(Constants.ProcessQueueName, async(processModel) => { using (var scope = _serviceProvider.CreateScope()) { using (var dbContext = scope.ServiceProvider.GetRequiredService <IWebDbContext>()) { var process = dbContext.Processes .Where(p => p.ProcessId == processModel.ProcessId) .SingleOrDefault(); if (processModel?.Status == Common.Models.ProcessStatus.Queued && process?.Status == DAL.Entities.ProcessStatus.Queued) { process.Status = DAL.Entities.ProcessStatus.InProgress; await dbContext.SaveChangesAsync(); _messageBusService.Send(Constants.ProcessQueueName, _mapper.Map <Process, ProcessModel>(process)); } else if (processModel?.Status == Common.Models.ProcessStatus.InProgress && process?.Status == DAL.Entities.ProcessStatus.InProgress) { // imitation of running task await Task.Delay(10000); process.Status = DAL.Entities.ProcessStatus.Completed; await dbContext.SaveChangesAsync(); } } } }); return(Task.CompletedTask); }