Merge pull request 'Метод для запуска задачи по Id' (#283) from feature/31429430-add-method-to-start-work-by-id into dev

Reviewed-on: http://test.digitaldrilling.ru:8080/DDrilling/AsbCloudServer/pulls/283
This commit is contained in:
Никита Фролов 2024-06-10 08:57:56 +05:00
commit 32fd358dfb

View File

@ -2,6 +2,7 @@
using AsbCloudInfrastructure.Background;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Threading;
@ -15,10 +16,14 @@ namespace AsbCloudWebApi.Controllers
public class PeriodicBackgroundWorkerController : ControllerBase
{
private readonly PeriodicBackgroundWorker worker;
private readonly IServiceProvider serviceProvider;
public PeriodicBackgroundWorkerController(PeriodicBackgroundWorker worker)
public PeriodicBackgroundWorkerController(
PeriodicBackgroundWorker worker,
IServiceProvider serviceProvider)
{
this.worker = worker;
this.serviceProvider = serviceProvider;
}
[HttpGet]
@ -33,6 +38,27 @@ namespace AsbCloudWebApi.Controllers
return Ok(result);
}
/// <summary>
/// Запуск задачи по Id
/// </summary>
/// <param name="workId">Id задачи</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("start/{workId}")]
public async Task<IActionResult> Start(string workId, CancellationToken token)
{
var targetWork = worker.Works.FirstOrDefault(w => w.Work.Id == workId);
if(targetWork is not null)
{
using (var scope = serviceProvider.CreateScope()) {
var result = await targetWork.Work.Start(scope.ServiceProvider, token);
return Ok(result);
}
}
return BadRequest("Work not found by workId");
}
[HttpPost("restart"), Obsolete("temporary method")]
public async Task<IActionResult> RestartAsync(CancellationToken token)
{