80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using System.Diagnostics;
|
|
using GerstITS.Data;
|
|
using GerstITS.Examples.JobDashboard.Configurations;
|
|
using GerstITS.Examples.JobDashboard.Data;
|
|
using GerstITS.Job;
|
|
using Quartz;
|
|
|
|
namespace GerstITS.Examples.JobDashboard.Jobs;
|
|
|
|
/// <summary>
|
|
/// A scheduled job that, on every run, persists a run record to the EF Core InMemory store through the
|
|
/// framework's <see cref="IRepository" /> (wired via AddEntityFrameworkSession). Every third run fails so
|
|
/// the Job Dashboard shows a mix of successful and failed executions.
|
|
/// </summary>
|
|
[DisallowConcurrentExecution]
|
|
public sealed class ReportGenerationJob : JobBase
|
|
{
|
|
#region Fields
|
|
|
|
private static int _runCounter;
|
|
|
|
private readonly ReportGenerationJobConfiguration _configuration;
|
|
private readonly IRepository _repository;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public ReportGenerationJob(ReportGenerationJobConfiguration configuration,
|
|
IRepository repository)
|
|
{
|
|
_configuration = configuration;
|
|
_repository = repository;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
protected override void Execute()
|
|
{
|
|
var runNumber = Interlocked.Increment(ref _runCounter);
|
|
var startedAt = DateTimeOffset.UtcNow;
|
|
var stopwatch = Stopwatch.StartNew();
|
|
|
|
// Simulate some work.
|
|
Thread.Sleep(250);
|
|
stopwatch.Stop();
|
|
|
|
var success = runNumber % 3 != 0;
|
|
var error = success
|
|
? null
|
|
: $"Report generation run #{runNumber} failed (simulated).";
|
|
|
|
PersistRun(startedAt, stopwatch.ElapsedMilliseconds, success, error);
|
|
|
|
if (!success)
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
private void PersistRun(DateTimeOffset startedAt, long durationMilliseconds, bool success, string error)
|
|
{
|
|
_repository.Save(new JobRunEntity
|
|
{
|
|
JobName = _configuration.Name,
|
|
StartedAt = startedAt,
|
|
DurationMilliseconds = durationMilliseconds,
|
|
Success = success,
|
|
Error = error
|
|
});
|
|
|
|
var storedRuns = _repository.Query<JobRunEntity>()
|
|
.Count();
|
|
|
|
Console.WriteLine($"---> [EF Core InMemory] persisted run of '{_configuration.Name}' (success: {success}); total runs stored: {storedRuns}");
|
|
}
|
|
|
|
#endregion
|
|
}
|