internal async Task PullTaskAsync(CancellationToken token) { var alreadyLogout = false; TaskPullerStarted?.Invoke(this); while (true) { if (TaskManagerCancellationToken.IsCancellationRequested) { break; } if (!LinkedMetadata.IsExecutorEnabled) { break; } var cts = TaskManagerCancellationToken; CancellationTokenSource localCts = null; var timeout = ExecutionTimeout; if (timeout.HasValue) { localCts = new CancellationTokenSource(timeout.Value); cts = CancellationTokenSource.CreateLinkedTokenSource( TaskManagerCancellationToken.Token, localCts.Token); } try { ExecutionStarting?.Invoke(this); await Execution(cts).ConfigureAwait(false); ExecutionFinished?.Invoke(this); } catch (OperationCanceledException) { if (localCts?.IsCancellationRequested ?? false) { ExecutionTimeoutEvent?.Invoke(this); } else { ExecutionCancelled?.Invoke(this); break; } } catch (ObjectDisposedException) { break; } catch (Exception e) { ExecutionExceptionHandler?.Invoke(this, e); } if (TaskManagerCancellationToken.IsCancellationRequested) { break; } if (!LinkedMetadata.IsExecutorEnabled) { break; } if (LinkedMetadata.GlobalApproveNewExecutorCreationCriteriaInContext() && ShouldTryToCreateNewPuller()) { CreatingNewPuller?.Invoke(this); #pragma warning disable 4014 LinkedMetadata.CreateNewTaskExecutor(token); #pragma warning restore 4014 NewPullerCreated?.Invoke(this); } if (!ShouldTryTerminateCurrentPuller()) { continue; } //Try kill itself if (await LinkedMetadata.TryPerformLogoutAsync().ConfigureAwait(false)) { alreadyLogout = true; break; } } if (!alreadyLogout) { await LinkedMetadata.ForceLogoutAsync().ConfigureAwait(false); } TaskPullerFinalized?.Invoke(this); }
protected override void OnCancelled() { base.OnCancelled(); ExecutionCancelled?.Invoke(this, EventArgs.Empty); }