DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/BackgroundWorkController.cs

70 lines
1.9 KiB
C#
Raw Normal View History

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]
2023-10-10 13:45:48 +05:00
public class BackgroundWorkController : ControllerBase
{
2023-11-03 17:02:44 +05:00
private readonly BackgroundWorker worker;
2023-11-03 17:02:44 +05:00
public BackgroundWorkController(BackgroundWorker worker)
{
2023-11-03 17:02:44 +05:00
this.worker = worker;
}
[HttpGet]
public IActionResult GetAll()
{
var result = new {
2023-11-03 17:02:44 +05:00
CurrentWork = (BackgroundWorkDto?)worker.CurrentWork,
worker.MainLoopLastException,
RunOnceQueue = worker.Works.Select(work => (BackgroundWorkDto)work),
Done = worker.Done.Select(work => (BackgroundWorkDto)work),
Felled = worker.Felled.Select(work => (BackgroundWorkDto)work),
};
return Ok(result);
}
2023-10-10 13:45:48 +05:00
[HttpGet("current")]
2023-10-10 13:45:48 +05:00
public IActionResult GetCurrent()
{
2023-11-03 17:02:44 +05:00
var work = worker.CurrentWork;
2023-10-12 11:48:55 +05:00
if (work == null)
return NoContent();
return Ok(work);
2023-10-10 13:45:48 +05:00
}
[HttpGet("failed")]
2023-10-10 13:45:48 +05:00
public IActionResult GetFelled()
{
2023-11-03 17:02:44 +05:00
var result = worker.Felled.Select(work => (BackgroundWorkDto)work);
2023-10-10 13:45:48 +05:00
return Ok(result);
}
[HttpGet("done")]
public IActionResult GetDone()
{
2023-11-03 17:02:44 +05:00
var result = worker.Done.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
[HttpPost("restart"), Obsolete("temporary method")]
public async Task<IActionResult> RestartAsync(CancellationToken token)
{
2023-11-03 17:02:44 +05:00
await worker.StopAsync(token);
await worker.StartAsync(token);
return Ok();
}
}
}