mirror of
https://github.com/fergalmoran/Quartzmin.git
synced 2025-12-31 05:47:37 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using Quartz;
|
||||
using Quartz.Impl.Matchers;
|
||||
using Quartz.Spi;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Quartz.Plugins.RecentHistory
|
||||
{
|
||||
public class ExecutionHistoryPlugin : ISchedulerPlugin, IJobListener
|
||||
{
|
||||
IScheduler _scheduler;
|
||||
IExecutionHistoryStore _store;
|
||||
|
||||
public string Name { get; set; }
|
||||
public Type ExecutionHistoryStoreType { get; set; }
|
||||
|
||||
public Task Initialize(string pluginName, IScheduler scheduler, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
Name = pluginName;
|
||||
_scheduler = scheduler;
|
||||
_scheduler.ListenerManager.AddJobListener(this, EverythingMatcher<JobKey>.AllJobs());
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task Start(CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_store = _scheduler.Context.GetExecutionHistoryStore();
|
||||
|
||||
if (_store == null)
|
||||
{
|
||||
if (ExecutionHistoryStoreType != null)
|
||||
_store = (IExecutionHistoryStore)Activator.CreateInstance(ExecutionHistoryStoreType);
|
||||
|
||||
if (_store == null)
|
||||
throw new Exception(nameof(ExecutionHistoryStoreType) + " is not set.");
|
||||
|
||||
_scheduler.Context.SetExecutionHistoryStore(_store);
|
||||
}
|
||||
|
||||
_store.SchedulerName = _scheduler.SchedulerName;
|
||||
|
||||
await _store.Purge();
|
||||
}
|
||||
|
||||
public Task Shutdown(CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var entry = new ExecutionHistoryEntry()
|
||||
{
|
||||
FireInstanceId = context.FireInstanceId,
|
||||
SchedulerInstanceId = context.Scheduler.SchedulerInstanceId,
|
||||
SchedulerName = context.Scheduler.SchedulerName,
|
||||
ActualFireTimeUtc = context.FireTimeUtc.UtcDateTime,
|
||||
ScheduledFireTimeUtc = context.ScheduledFireTimeUtc?.UtcDateTime,
|
||||
Recovering = context.Recovering,
|
||||
Job = context.JobDetail.Key.ToString(),
|
||||
Trigger = context.Trigger.Key.ToString(),
|
||||
};
|
||||
await _store.Save(entry);
|
||||
}
|
||||
|
||||
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var entry = await _store.Get(context.FireInstanceId);
|
||||
if (entry != null)
|
||||
{
|
||||
entry.FinishedTimeUtc = DateTime.UtcNow;
|
||||
entry.ExceptionMessage = jobException?.GetBaseException()?.Message;
|
||||
await _store.Save(entry);
|
||||
}
|
||||
if (jobException == null)
|
||||
await _store.IncrementTotalJobsExecuted();
|
||||
else
|
||||
await _store.IncrementTotalJobsFailed();
|
||||
}
|
||||
|
||||
public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
var entry = await _store.Get(context.FireInstanceId);
|
||||
if (entry != null)
|
||||
{
|
||||
entry.Vetoed = true;
|
||||
await _store.Save(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user