private static void Updater() { Console.WriteLine("Sleeping for 30 seconds..."); Thread.Sleep(30000); Console.WriteLine("Sleeping finished"); UpdateJob(Settings.Default.JobId, 2, "Job is in progress", "Worker started processing this job"); var points = GetPointsInBetween(new LatLngTime { AddedAt = new DateTime(2016, 4, 3, 3, 14, 15), Point = new LatLng { Lat = 44.4415775m, Lng = 26.0179001m } }, new LatLngTime { AddedAt = new DateTime(2016, 4, 3, 3, 30, 15), Point = new LatLng { Lat = 44.483285m, Lng = 26.0979763m } }); foreach (var point in points) { using (var dbContext = new ServerPushDbContext()) { var job = dbContext.Jobs.FirstOrDefault(x => x.Id == Settings.Default.JobId); if (job == null) { return; } job.Positions.Add(new JobPosition { AddedAt = point.AddedAt, Latitude = point.Point.Lat, Longitude = point.Point.Lng }); dbContext.Entry(job).State = EntityState.Modified; dbContext.SaveChanges(); } Thread.Sleep(60000); } Console.WriteLine("Sleeping for 30 seconds..."); Thread.Sleep(30000); Console.WriteLine("Sleeping finished"); UpdateJob(Settings.Default.JobId, 3, "Job is finished", "Worker finished processing this job"); }
internal JobUpdater(IHubContext hubContext, ServerPushDbContext dbContext) { if (hubContext == null) { throw new ArgumentNullException(nameof(hubContext)); } if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } this._hubContext = hubContext; this._dbContext = dbContext; }
public async Task Subscribe(int jobId) { await this.Groups.Add(this.Context.ConnectionId, GetGroup(jobId)); using (var dbContext = new ServerPushDbContext()) { var job = await dbContext .Jobs .Include(x => x.Status) .Include(x => x.Actions) .Include(x => x.Positions) .FirstOrDefaultAsync(x => x.Id == jobId); if (job == null) { return; } if (job.StatusId == 1 || string.IsNullOrEmpty(job.Resolution) || job.Actions.Count == 1) { return; } var actions = job.Actions.OrderByDescending(x => x.AddedAt).Select(x => new ActionListViewModel { Id = x.Id, Name = x.Name, AddedAt = x.AddedAt, }).ToArray().AsJson(); var positions = job.Positions.OrderBy(x => x.AddedAt).Select(x => new LatLng { Lat = x.Latitude, Lng = x.Longitude }).ToArray().AsJson(); this.Clients.Client(this.Context.ConnectionId).update(job.Id, job.Status.Name, job.Resolution, actions, positions); } }
private static void UpdateJob(int jobId, int statusId, string resolution, string action) { using (var dbContext = new ServerPushDbContext()) { var job = dbContext.Jobs.FirstOrDefault(x => x.Id == jobId); if (job == null) { return; } job.StatusId = statusId; job.Resolution = resolution; job.Actions.Add(new JobAction { AddedAt = DateTime.Now, Name = action, }); dbContext.Entry(job).State = EntityState.Modified; dbContext.SaveChanges(); } }