public SmartConfigContext CreateContext() { var ctx = new SmartConfigContext(this); foreach (var e in this.GetDefaultOptions()) { ctx.Options[e.key] = e.value; } return(ctx); }
public async Task ExecuteAsync(SmartConfigContext context, SmartConfigArguments args, CancellationToken externalCancelToken) { if (_isStarted) { throw new InvalidOperationException("Already started"); } this.ExecutedTime = TimeSpan.Zero; _isStarted = true; _timerCts = new CancellationTokenSource(); var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(externalCancelToken, _timerCts.Token); try { _timer.Start(); this.Elapsed(this, new SmartConfigTimerEventArgs(this.Timeout, this.ExecutedTime)); var broadcastingTask = _broadcaster.BroadcastAsync(context, args, linkedCts.Token).CancelOnFaulted(linkedCts); var receivingTask = _receiver.ListenAsync(context, linkedCts.Token).CancelOnFaulted(linkedCts); await Task.WhenAll(broadcastingTask, receivingTask); } catch (OperationCanceledException ocex) { if (externalCancelToken.IsCancellationRequested) { throw ocex; } } catch { linkedCts.Cancel(); throw; } finally { if (_timer.Enabled) { _timer.Stop(); } linkedCts.Dispose(); _timerCts.Dispose(); _timerCts = null; ExecutedTime = TimeSpan.Zero; _isStarted = false; } }
public static async Task ExecuteAsync( this ISmartConfigJob self, SmartConfigArguments args, SmartConfigContext context) { await self.ExecuteAsync(context, args, CancellationToken.None); }