示例#1
0
        protected override async Task ExecuteAsync(CancellationToken token)
        {
            _logger.LogInformation($"Heartbeat service started");

            while (!token.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(1), token);

                if (_tasks.Count == 0)
                {
                    continue;
                }

                DateTime now = DateTime.Now;

                try
                {
                    List <HeartbeatTask> tasks = new List <HeartbeatTask>();
                    lock (_tasks)
                    {
                        while (_tasks.Count > 0 && _tasks[0].NextHeartBeat < now)
                        {
                            tasks.Add(_tasks[0]);
                            _tasks.RemoveAt(0);
                        }
                    }

                    foreach (var t in tasks)
                    {
                        _logger.LogDebug($"Sending Heartbeat for token '{t.TaskToken}'");
                        var r = await _client.SendTaskHeartbeatAsync(new SendTaskHeartbeatRequest
                        {
                            TaskToken = t.TaskToken
                        }, token);

                        if (r.HttpStatusCode != HttpStatusCode.OK)
                        {
                            _logger.LogDebug($"Heartbeat received an error '{t.TaskToken}', cancelling worker");
                            t.CancellationTokenSource.Cancel();
                        }
                        else
                        {
                            RegisterHeartbeat(t.Delay, t.TaskToken, t.CancellationTokenSource);
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.LogWarning(e, "Error while sending heartbeat");
                }
            }

            _logger.LogDebug($"Heartbeat service stopped");
        }