DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/BackgroundWorkController.cs
2023-11-02 16:20:48 +05:00

71 lines
2.2 KiB
C#

using AsbCloudApp.Data;
using AsbCloudInfrastructure.Background;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class BackgroundWorkController : ControllerBase
{
private readonly BackgroundWorker backgroundWorker;
public BackgroundWorkController(BackgroundWorker backgroundWorker)
{
this.backgroundWorker = backgroundWorker;
}
[HttpGet]
public IActionResult GetAll()
{
var result = new {
CurrentWork = (BackgroundWorkDto?)backgroundWorker.CurrentWork,
backgroundWorker.MainLoopLastException,
RunOnceQueue = backgroundWorker.WorkStore.RunOnceQueue.Select(work => (BackgroundWorkDto)work),
Periodics = backgroundWorker.WorkStore.Periodics.Select(work => (BackgroundWorkDto)work.Work),
Done = backgroundWorker.WorkStore.Done.Select(work => (BackgroundWorkDto)work),
Felled = backgroundWorker.WorkStore.Felled.Select(work => (BackgroundWorkDto)work),
};
return Ok(result);
}
[HttpGet("current")]
public IActionResult GetCurrent()
{
var work = backgroundWorker.CurrentWork;
if (work == null)
return NoContent();
return Ok(work);
}
[HttpGet("failed")]
public IActionResult GetFelled()
{
var result = backgroundWorker.WorkStore.Felled.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
[HttpGet("done")]
public IActionResult GetDone()
{
var result = backgroundWorker.WorkStore.Done.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
[HttpPost("restart"), Obsolete("temporary method")]
public async Task<IActionResult> RestartAsync(CancellationToken token)
{
await backgroundWorker.StopAsync(token);
await backgroundWorker.StartAsync(token);
return Ok();
}
}
}