using Hangfire; using Microsoft.AspNetCore.Mvc; using System; using System.Net.Http; namespace JobServer.Controllers { public class RecurringJobController : Controller { private readonly IHttpClientFactory _httpClientFactory; public RecurringJobController(IHttpClientFactory httpClientFactory) { this._httpClientFactory = httpClientFactory; } public string AddOrUpdate(string id, string url, string cron) { try { RecurringJob.AddOrUpdate(id, o => o.Handle(id, url), cron); return string.Empty; } catch (Exception ex) { return ex.ToString(); } } public string Remove(string id) { try { RecurringJob.RemoveIfExists(id); return string.Empty; } catch (Exception ex) { return ex.ToString(); } } private void Handle(string id, string url) { var wc = this._httpClientFactory.CreateClient(); try { using var content = new StringContent(id); wc.PostAsync(url, content); } catch (HttpRequestException ex) { Console.WriteLine(ex.ToString()); } } } }