You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/JobServer/Controllers/RecurringJobController.cs

57 lines
1.4 KiB

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<RecurringJobController>(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());
}
}
}
}