Add WorkerController API to control background Worker service
Introduced a new WorkerController with endpoints to start, stop, and restart the background Worker service via HTTP API. The controller uses dependency injection to locate the Worker instance and logs each operation. Also removed an unused Controllers folder reference from the project file.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class WorkerController(IEnumerable<IHostedService> hostedServices, ILogger<WorkerController> logger) : ControllerBase
|
||||
{
|
||||
private Worker? Worker => hostedServices.OfType<Worker>().FirstOrDefault();
|
||||
|
||||
[HttpPost("stop")]
|
||||
public async Task<IActionResult> Stop(CancellationToken cancel)
|
||||
{
|
||||
if (Worker is null)
|
||||
return NotFound();
|
||||
|
||||
logger.LogInformation("Stopping Worker via API request.");
|
||||
await Worker.StopAsync(cancel);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("start")]
|
||||
public async Task<IActionResult> Start(CancellationToken cancel)
|
||||
{
|
||||
if (Worker is null)
|
||||
return NotFound();
|
||||
|
||||
logger.LogInformation("Starting Worker via API request.");
|
||||
await Worker.StartAsync(cancel);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("restart")]
|
||||
public async Task<IActionResult> Restart(CancellationToken cancel)
|
||||
{
|
||||
if (Worker is null)
|
||||
return NotFound();
|
||||
|
||||
logger.LogInformation("Restarting Worker via API request.");
|
||||
await Worker.StopAsync(cancel);
|
||||
await Worker.StartAsync(cancel);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
@@ -26,10 +26,6 @@
|
||||
<ProjectReference Include="..\EnvelopeGenerator.PdfEditor\EnvelopeGenerator.PdfEditor.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Jobs\FinalizeDocument\rptEnvelopeHistory.cs">
|
||||
<SubType>Component</SubType>
|
||||
|
||||
Reference in New Issue
Block a user