2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data;
|
2023-10-09 13:12:45 +05:00
|
|
|
using AsbCloudInfrastructure.Background;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-11-02 16:20:48 +05:00
|
|
|
using System;
|
2023-10-09 13:12:45 +05:00
|
|
|
using System.Linq;
|
2023-11-02 16:20:48 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2023-10-09 13:12:45 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
{
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
[Authorize]
|
|
|
|
[ApiController]
|
2023-10-10 13:45:48 +05:00
|
|
|
public class BackgroundWorkController : ControllerBase
|
2023-10-09 13:12:45 +05:00
|
|
|
{
|
2023-11-03 17:02:44 +05:00
|
|
|
private readonly BackgroundWorker worker;
|
2023-10-09 13:12:45 +05:00
|
|
|
|
2023-11-03 17:02:44 +05:00
|
|
|
public BackgroundWorkController(BackgroundWorker worker)
|
2023-10-09 13:12:45 +05:00
|
|
|
{
|
2023-11-03 17:02:44 +05:00
|
|
|
this.worker = worker;
|
2023-10-09 13:12:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[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),
|
2023-10-09 13:12:45 +05:00
|
|
|
};
|
|
|
|
return Ok(result);
|
|
|
|
}
|
2023-10-10 13:45:48 +05:00
|
|
|
|
2023-11-02 16:20: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
|
|
|
}
|
|
|
|
|
2023-11-02 16:20: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);
|
|
|
|
}
|
2023-11-02 16:20:48 +05:00
|
|
|
|
|
|
|
[HttpGet("done")]
|
|
|
|
public IActionResult GetDone()
|
|
|
|
{
|
2023-11-03 17:02:44 +05:00
|
|
|
var result = worker.Done.Select(work => (BackgroundWorkDto)work);
|
2023-11-02 16:20:48 +05:00
|
|
|
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);
|
2023-11-02 16:20:48 +05:00
|
|
|
return Ok();
|
|
|
|
}
|
2023-10-09 13:12:45 +05:00
|
|
|
}
|
|
|
|
}
|