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

69 lines
1.7 KiB
C#
Raw Permalink 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;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class BackgroundWorkController : ControllerBase
{
2024-08-19 10:01:07 +05:00
private readonly BackgroundWorker worker;
2024-08-19 10:01:07 +05:00
public BackgroundWorkController(BackgroundWorker worker)
{
this.worker = worker;
}
2024-08-19 10:01:07 +05:00
[HttpGet]
public IActionResult GetAll()
{
var result = new {
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
2024-08-19 10:01:07 +05:00
[HttpGet("current")]
public IActionResult GetCurrent()
{
var work = worker.CurrentWork;
if (work == null)
return NoContent();
2023-10-12 11:48:55 +05:00
2024-08-19 10:01:07 +05:00
return Ok(work);
}
2023-10-10 13:45:48 +05:00
2024-08-19 10:01:07 +05:00
[HttpGet("failed")]
public IActionResult GetFelled()
{
var result = worker.Felled.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
[HttpGet("done")]
public IActionResult GetDone()
{
var result = worker.Done.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
[HttpPost("restart"), Obsolete("temporary method")]
public async Task<IActionResult> RestartAsync(CancellationToken token)
{
await worker.StopAsync(token);
await worker.StartAsync(token);
return Ok();
}
}