예제 #1
0
파일: Node.cs 프로젝트: misupov/turbina
        private async Task OperateLoop()
        {
            while (true)
            {
                await Inlets.AnyMessageAvailableAsync();

                try
                {
                    NodeDisposedToken.ThrowIfCancellationRequested();
                    var inletsBefore = Inlets.GetSnapshot();
                    await Operate();

                    _operationsCount++;
                    var inletsAfter = Inlets.GetSnapshot();
                    if (!Equals(inletsBefore, inletsAfter))
                    {
                    }
                }
                catch (OperationCanceledException ex) when(ex.CancellationToken == NodeDisposedToken)
                {
                    break;
                }
                catch (Exception ex)
                {
                    _exceptionsCount++;
                    Exception.Send(ex);
                }
            }
        }
예제 #2
0
        protected override async Task Operate()
        {
            var currentIsEnabled = false;
            var currentInterval  = default(TimeSpan);
            var stopwatch        = Stopwatch.StartNew();
            var counter          = 0;
            var ticksElapsed     = 0;

            while (true)
            {
                var newIsEnabled     = await IsEnabled;
                var newInterval      = await Interval;
                var useLocalTimeZone = await UseLocalTimeZone;

                var shouldPost = true;
                if (currentIsEnabled != newIsEnabled || currentInterval != newInterval)
                {
                    shouldPost       = false;
                    stopwatch        = Stopwatch.StartNew();
                    ticksElapsed     = 0;
                    currentIsEnabled = newIsEnabled;
                    currentInterval  = newInterval;
                    if (!currentIsEnabled || currentInterval.TotalMilliseconds <= 0)
                    {
                        break;
                    }
                }

                if (shouldPost)
                {
                    ticksElapsed++;
                    counter++;
                    Counter.Send(counter);
                    Timestamp.Send(useLocalTimeZone ? DateTimeOffset.Now : DateTimeOffset.UtcNow);
                }

                using (var cts1 = new CancellationTokenSource())
                    using (var cts2 = CancellationTokenSource.CreateLinkedTokenSource(cts1.Token, NodeDisposedToken))
                    {
                        var delayTask = GetDelayTask(stopwatch.Elapsed, ticksElapsed, currentInterval, cts2.Token);
                        if (delayTask.Status != TaskStatus.RanToCompletion)
                        {
                            await Task.WhenAny(
                                delayTask.IgnoreCancellation(),
                                Inlets.AnyMessageAvailableAsync());

                            cts1.Cancel();
                        }
                    }

                NodeDisposedToken.ThrowIfCancellationRequested();
            }
        }