private async Task RunAsync(int userId, Schedule schedule) { var log = new Log { ExecutionDateTimeUtc = DateTime.UtcNow, ScheduleId = schedule.Id, UserId = userId, MachineName = Environment.MachineName }; try { var url = schedule.Url; if (!url.StartsWith("http")) url = $"http://localhost{url}"; using (var client = new HttpClient()) { var req = new HttpRequestMessage(new HttpMethod(schedule.HttpVerb), url); var contentType = "application/json"; schedule.Headers?.ForEach(h => { if (h.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase)) contentType = h.Value; else req.Content.Headers.Add(h.Key, h.Value); }); if (!string.IsNullOrWhiteSpace(schedule.Data)) { req.Content = new StringContent(schedule.Data, Encoding.UTF8, contentType); } var resp = await client.SendAsync(req).ConfigureAwait(false); log.Result = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); log.Success = resp.IsSuccessStatusCode; } } catch (Exception ex) { _log.Error($"Failed to run schedule {schedule.Name}", ex); log.Result = ex.Message; } _database.Save(log); }
private async Task RunScheduledAsync(Schedule schedule) { var currentTime = DateTime.UtcNow; if (schedule.NextRunUtc > currentTime) return; var frequency = (Frequency) Enum.Parse(typeof(Frequency), schedule.Frequency); var nextRun = GetNextRun(frequency, schedule.NextRunUtc); if (frequency == Frequency.Single) { schedule.Disabled = true; } else { //Handle scenarios where past executions may have been skipped. while (nextRun < currentTime) { nextRun = GetNextRun(frequency, nextRun); } } schedule.NextRunUtc = nextRun; _database.Update(schedule); await RunAsync(0, schedule); }