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.
72 lines
2.2 KiB
72 lines
2.2 KiB
using Hangfire;
|
|
using JobServer.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Net.Http;
|
|
|
|
namespace JobServer.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]/[action]")]
|
|
public class RecurringJobController : Controller
|
|
{
|
|
private readonly ILogger<RecurringJobController> _logger;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
public RecurringJobController(ILogger<RecurringJobController> logger, IHttpClientFactory httpClientFactory)
|
|
{
|
|
this._logger = logger;
|
|
this._httpClientFactory = httpClientFactory;
|
|
}
|
|
|
|
[HttpPost]
|
|
public string AddOrUpdate(RecurringJobModel model)
|
|
{
|
|
try
|
|
{
|
|
RecurringJob.AddOrUpdate(model.Id, () => Handle(model.Id, model.Url), model.Cron, TimeZoneInfo.Local);
|
|
this._logger.LogDebug($"job {model.Id} has updated");
|
|
return string.Empty;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex.ToString());
|
|
return ex.ToString();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("{id}")]
|
|
public string Remove(string id)
|
|
{
|
|
try
|
|
{
|
|
RecurringJob.RemoveIfExists(id);
|
|
this._logger.LogDebug($"job {id} has removed");
|
|
return string.Empty;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex.ToString());
|
|
return ex.ToString();
|
|
}
|
|
}
|
|
|
|
public void Handle(string id, string url)
|
|
{
|
|
var wc = this._httpClientFactory.CreateClient();
|
|
try
|
|
{
|
|
using var content = new StringContent(id);
|
|
wc.PostAsync(url, content);
|
|
var result = wc.PostAsync(url, content).Result;
|
|
this._logger.LogDebug($"{result.StatusCode}:{result.Content.ReadAsStringAsync().Result}");
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
this._logger.LogError(ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
} |