private async Task InvokeEvent(ScheduledEvent scheduledEvent) { try { await this.TryDispatchEvent(new ScheduledEventStarted(scheduledEvent)); async Task Invoke() { this._logger?.LogInformation("Scheduled task started..."); await scheduledEvent.InvokeScheduledEvent(); this._logger?.LogInformation("Scheduled task finished..."); }; if (scheduledEvent.ShouldPreventOverlapping()) { if (this._mutex.TryGetLock(scheduledEvent.OverlappingUniqueIdentifier(), EventLockTimeout_24Hours)) { try { await Invoke(); } finally { this._mutex.Release(scheduledEvent.OverlappingUniqueIdentifier()); } } } else { await Invoke(); } } catch (Exception e) { await this.TryDispatchEvent(new ScheduledEventFailed(scheduledEvent, e)); this._logger?.LogError("A scheduled task threw an Exception: " + e.Message); if (this._errorHandler != null) { this._errorHandler(e); } } finally { await this.TryDispatchEvent(new ScheduledEventEnded(scheduledEvent)); } }
private async Task InvokeEvent(ScheduledEvent scheduledEvent) { try { await this.TryDispatchEvent(new ScheduledEventStarted(scheduledEvent)); async Task Invoke() { this._logger?.LogDebug("Scheduled task started..."); await scheduledEvent.InvokeScheduledEvent(this._cancellationTokenSource.Token); this._logger?.LogDebug("Scheduled task finished..."); }; if (scheduledEvent.ShouldPreventOverlapping()) { if (this._mutex.TryGetLock(scheduledEvent.OverlappingUniqueIdentifier(), EventLockTimeout_24Hours)) { try { await Invoke(); } finally { this._mutex.Release(scheduledEvent.OverlappingUniqueIdentifier()); } } } else { await Invoke(); } await this.TryDispatchEvent(new ScheduledEventEnded(scheduledEvent)); } catch (Exception e) { await this.TryDispatchEvent(new ScheduledEventFailed(scheduledEvent, e)); this._logger?.LogError(e, "A scheduled task threw an Exception: "); Trace.WriteLine("Error Detected! \n NOTE: If using IInvocable, be sure to add service.AddTransient<ClassWithIInvocable> to your service. \n If using AutoSchedulerBase, be sure you don't!"); this._errorHandler?.Invoke(e); // throw (e); } }