Compare commits
10 Commits
f0f92c5400
...
6a9792bb57
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a9792bb57 | |||
| 6954a86358 | |||
| d6c5b63c49 | |||
| 8ca360d47e | |||
| 2dadefecc5 | |||
| 162f066b08 | |||
| 6592642945 | |||
| 855f22cf87 | |||
| 726673e277 | |||
| 65d615f43e |
@@ -9,8 +9,13 @@ namespace EnvelopeGenerator.ServiceHost.Controllers;
|
|||||||
public class DocResultController(IMediator mediator) : ControllerBase
|
public class DocResultController(IMediator mediator) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> GetAsync([FromQuery] ReadSingleEnvelopeDocResultQuery query, CancellationToken cancel = default)
|
public async Task<IActionResult> GetAsync([FromQuery] ReadSingleEnvelopeDocResultQuery query, [FromQuery] bool download = false, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
return File(await mediator.Send(query, cancel), "application/pdf", $"envelope_{query.Envelope.Uuid}.pdf");
|
var bytes = await mediator.Send(query, cancel);
|
||||||
|
|
||||||
|
if (download)
|
||||||
|
return File(bytes, "application/pdf", $"envelope_{query.Envelope.Uuid}.pdf");
|
||||||
|
|
||||||
|
return File(bytes, "application/pdf");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,21 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.ServiceHost.Controllers;
|
namespace EnvelopeGenerator.ServiceHost.Controllers;
|
||||||
|
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class WorkerController(IEnumerable<IHostedService> hostedServices, ILogger<WorkerController> logger) : ControllerBase
|
public class WorkerController(JobStateManager jobStateManager) : ControllerBase
|
||||||
{
|
{
|
||||||
private Worker? Worker => hostedServices.OfType<Worker>().FirstOrDefault();
|
private readonly JobStateManager _jobStateManager = jobStateManager;
|
||||||
|
|
||||||
[HttpPost("stop")]
|
[HttpGet(nameof(FinalizeDocumentJob))]
|
||||||
public async Task<IActionResult> Stop(CancellationToken cancel)
|
public IActionResult GetStateOfFinalizeDocumentJob() => Ok(_jobStateManager.GetState<FinalizeDocumentJob>());
|
||||||
|
|
||||||
|
[HttpPost(nameof(FinalizeDocumentJob))]
|
||||||
|
public IActionResult SetStateOfFinalizeDocumentJob([FromQuery] State state)
|
||||||
{
|
{
|
||||||
if (Worker is null)
|
_jobStateManager.SetState<FinalizeDocumentJob>(state);
|
||||||
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();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0-windows</TargetFramework>
|
<TargetFramework>net8.0-windows</TargetFramework>
|
||||||
@@ -20,7 +20,8 @@
|
|||||||
<PackageReference Include="System.Drawing.Common" Version="8.0.16" />
|
<PackageReference Include="System.Drawing.Common" Version="8.0.16" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||||
<PackageReference Include="DevExpress.Reporting.Core" Version="24.2.*" />
|
<PackageReference Include="DevExpress.Reporting.Core" Version="24.2.*" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="8.0.17" />
|
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="8.0.17" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -10,7 +10,13 @@ public static class DependencyInjection
|
|||||||
[Obsolete("Check obsoleted services")]
|
[Obsolete("Check obsoleted services")]
|
||||||
public static IServiceCollection AddFinalizeDocumentJob(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddFinalizeDocumentJob(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.Configure<WorkerOptions>(configuration.GetSection(nameof(WorkerOptions)));
|
services.Configure<WorkerOptions>(configuration.GetSection("Worker"));
|
||||||
|
services.AddSingleton(provider =>
|
||||||
|
{
|
||||||
|
var options = provider.GetRequiredService<IOptions<WorkerOptions>>().Value;
|
||||||
|
var manager = new JobStateManager(options.InitialJobState);
|
||||||
|
return manager;
|
||||||
|
});
|
||||||
services.AddScoped<FinalizeDocumentJob>();
|
services.AddScoped<FinalizeDocumentJob>();
|
||||||
services.AddScoped<ActionService>();
|
services.AddScoped<ActionService>();
|
||||||
services.AddSingleton<TempFiles>();
|
services.AddSingleton<TempFiles>();
|
||||||
|
|||||||
19
EnvelopeGenerator.ServiceHost/Jobs/JobStateManager.cs
Normal file
19
EnvelopeGenerator.ServiceHost/Jobs/JobStateManager.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using AngleSharp.Common;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
|
|
||||||
|
public class JobStateManager(Dictionary<string, State>? initialState = null)
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<Type, State> _states = new();
|
||||||
|
|
||||||
|
public State GetState<TJob>() => _states.GetOrAdd(typeof(TJob), type => initialState?.GetOrDefault(type.Name, State.Stopped) ?? State.Stopped);
|
||||||
|
|
||||||
|
public State SetState<TJob>(State state) => _states[typeof(TJob)] = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum State
|
||||||
|
{
|
||||||
|
Running,
|
||||||
|
Stopped
|
||||||
|
}
|
||||||
@@ -1,10 +1,23 @@
|
|||||||
using EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
|
|
||||||
public class WorkerOptions
|
public class WorkerOptions
|
||||||
{
|
{
|
||||||
|
private int _delayMilliseconds = 1000;
|
||||||
|
|
||||||
|
public int DelayMilliseconds
|
||||||
|
{
|
||||||
|
get => _delayMilliseconds;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value < 1)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(value), "Delay must be at least 1 millisecond.");
|
||||||
|
|
||||||
|
_delayMilliseconds = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, State> InitialJobState { get; set; } = [];
|
||||||
|
|
||||||
public string GdPictureLicenseKey { get; set; } = null!;
|
public string GdPictureLicenseKey { get; set; } = null!;
|
||||||
|
|
||||||
public PDFBurnerOptions PdfBurner { get; set; } = new();
|
public PDFBurnerOptions PdfBurner { get; set; } = new();
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
using EnvelopeGenerator.ServiceHost.Jobs;
|
using EnvelopeGenerator.ServiceHost.Jobs;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.ServiceHost;
|
namespace EnvelopeGenerator.ServiceHost;
|
||||||
|
|
||||||
public class Worker : BackgroundService
|
public class Worker : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly ILogger<Worker> _logger;
|
private readonly ILogger<Worker> _logger;
|
||||||
private readonly int _delayMilliseconds;
|
private readonly WorkerOptions _options;
|
||||||
|
private readonly JobStateManager _jobStateManager;
|
||||||
private readonly IServiceScopeFactory _scopeFactory;
|
private readonly IServiceScopeFactory _scopeFactory;
|
||||||
|
|
||||||
public Worker(ILogger<Worker> logger, IConfiguration configuration, IServiceScopeFactory scopeFactory)
|
public Worker(ILogger<Worker> logger, IOptions<WorkerOptions> options, JobStateManager jobStateManager, IServiceScopeFactory scopeFactory)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_delayMilliseconds = Math.Max(1, configuration.GetValue("Worker:DelayMilliseconds", 1000));
|
_options = options.Value;
|
||||||
|
_jobStateManager = jobStateManager;
|
||||||
_scopeFactory = scopeFactory;
|
_scopeFactory = scopeFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,11 +27,14 @@ public class Worker : BackgroundService
|
|||||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
}
|
}
|
||||||
|
|
||||||
using var scope = _scopeFactory.CreateScope();
|
if (_jobStateManager.GetState<FinalizeDocumentJob>() == State.Running)
|
||||||
var finalizeDocumentJob = scope.ServiceProvider.GetRequiredService<FinalizeDocumentJob>();
|
{
|
||||||
await finalizeDocumentJob.ExecuteAsync(stoppingToken);
|
using var scope = _scopeFactory.CreateScope();
|
||||||
|
var finalizeDocumentJob = scope.ServiceProvider.GetRequiredService<FinalizeDocumentJob>();
|
||||||
|
await finalizeDocumentJob.ExecuteAsync(stoppingToken);
|
||||||
|
}
|
||||||
|
|
||||||
await Task.Delay(_delayMilliseconds, stoppingToken);
|
await Task.Delay(_options.DelayMilliseconds, stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user