public async Task Execute(IJobExecutionContext context) { try { var schedule = typeof(T).FullName; var lockHandle = await _lockManager .TryLockAsync(schedule, TimeSpan.FromSeconds(60), CancellationToken.None).ConfigureAwait(false); if (lockHandle == null) { //someone else has locked this instance, do nothing return; } _logger.Information("Executing schedule {Schedule} {LockHandle}", schedule, lockHandle); using (new SingletonTimerScope(_logger, lockHandle, false)) using (_dependencyInjection.GetScope()) { var processor = _dependencyInjection.GetInstance <IProcessSchedule <T> >(); await processor.ProcessAsync(context.CancellationToken).ConfigureAwait(false); } } catch (Exception e) { ConsoleWriter.WriteLine($"Error processing schedule {typeof(T)} {e.Message} {e.StackTrace}"); _logger.Error(e, "Error processing schedule {Schedule}", typeof(T)); } }
public async Task ProcessAsync <T>(IMessageStateHandler <T> messageStateHandler, CancellationToken cancellationToken) where T : class, IMessage { var typedMessage = await messageStateHandler.GetMessageAsync().ConfigureAwait(false); var messageHandler = _processorProvider.GetInstance <IProcessMessage <T> >(typeof(TMessageProcessor)); await messageHandler.ProcessAsync(typedMessage, cancellationToken).ConfigureAwait(false); await messageStateHandler.CompleteAsync().ConfigureAwait(false); }