public async Task Process(CancellationToken token) { var stopwatch = new Stopwatch(); Job currentJob = null; while (!token.IsCancellationRequested) { stopwatch.Start(); try { var activeJobs = await jobsDataService.GetActiveJobs(); foreach (var job in activeJobs) { currentJob = job; if (CircuitBreakers.IsBroken(job.GetJobIdentifier())) { Logger.Error($"{job.GetJobIdentifier()} has a broken circuit breaker"); continue; } var schedule = jsonSerializer.Deserialize <Schedule>(job.schedule); if (schedule.start >= DateTime.UtcNow) { continue; } var nextEvent = schedulerService.GetNextEvent(schedule, job.last_executed_at); Logger.Trace($"Next execution for {job.command_name}:{job.id} at {nextEvent}"); if (nextEvent != null && nextEvent < clock.UtcNow) { Logger.Trace($"Executing job {job.command_name}:{job.id}"); WrapFireEvent(job); } if (nextEvent == null) { Logger.Trace($"Marking job {job.command_name}:{job.id} as expired"); await jobsDataService.MarkJobExpired(job.id); } } } catch (Exception e) { Logger.Error($"Error Job {currentJob?.command_name}:{currentJob?.id} Processing: {e.Message}"); if (currentJob != null) { CircuitBreakers.RecordBreak(currentJob.GetJobIdentifier()); } } while (stopwatch.ElapsedMilliseconds < configuration.JobProcessThrottleMs()) { await Task.Delay(100, token); } stopwatch.Reset(); } }
public override async Task <CommandResult> Execute() { var activeJobs = await jobsDataService.GetActiveJobs(); if (Expired) { var expiredJobs = await jobsDataService.GetExpiredJobs(); return(CommandResult.CreateSuccessResult(activeJobs.Concat(expiredJobs))); } return(CommandResult.CreateSuccessResult(activeJobs)); }
public override async Task <CommandResult> Execute() { var values = new Dictionary <string, Func <Task <string> > >(); values.Add("login sessions", async() => (await loginSessionDataService.GetAllActiveSessions()).Count.ToString()); values.Add("bookmarks", async() => (await bookmarksDataService.GetTotal()).ToString()); values.Add("new bookmarks", async() => (await bookmarksDataService.GetRecentCount()).ToString()); values.Add("weight change (week)", async() => (await personalHealthService.CalculateRecentlyLostWeight()).ToString(CultureInfo.InvariantCulture)); values.Add("weight change", async() => (await personalHealthService.TotalWeightChange()).ToString(CultureInfo.InvariantCulture)); values.Add("log entries", async() => (await journalDataService.GetCount()).ToString()); values.Add("new log entries", async() => (await journalDataService.GetRecentEntryCount()).ToString()); values.Add("active jobs", async() => (await jobsDataService.GetActiveJobs()).Count.ToString()); values.Add("feeds", async() => (await feedDataService.GetFeeds()).Count.ToString()); values.Add("unread items", async() => (await feedDataService.GetFeeds()).Sum(i => i.unread_count).ToString()); values.Add("notes", async() => (await notebookDataService.GetNoteCount()).ToString()); values.Add("boards", async() => (await boardDataService.GetBoardCount()).ToString()); values.Add("incomplete board items", async() => (await boardDataService.GetIncompleteItemCount()).ToString()); values.Add("recently added board items", async() => (await boardDataService.GetRecentlyAddedItemCount()).ToString()); values.Add("recently completed board items", async() => (await boardDataService.GetRecentlyCompletedItemCount()).ToString()); values.Add("checklists", async() => (await checklistsDataService.GetChecklists()).Count.ToString()); values.Add("completed checklists", async() => (await checklistsDataService.GetChecklistCompletionLog()).Count.ToString()); var counter = 0; try { var summaries = await Task.WhenAll(values.Select(async v => { try { return(new { label = v.Key, amount = await v.Value(), id = ++counter }); } catch (Exception) { return(null); } })); return(CommandResult.CreateSuccessResult(summaries.Where(s => s != null))); } catch (Exception e) { Logger.Error("Could not build summaries"); Logger.Error(e.Message); var summary = new { label = "Errors Building Summaries", amount = "1" }; return(CommandResult.CreateSuccessResult(new List <object> { summary })); } }