예제 #1
0
        public static async Task MainAsync(string[] args, CancellationToken token)
        {
            {
                Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");

                var websiteTask = Task.Run(() => Website.Program.Main(new string[0]));

                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "OldyaXRl");
                httpClient.Timeout = Timeout.InfiniteTimeSpan;

                for (int i = 0; i < 12; i++)
                {
                    try
                    {
                        var checksResponseString = await httpClient.GetStringAsync("http://localhost:5000/api/checks");

                        Console.WriteLine(Serialize(JsonConvert.DeserializeObject(checksResponseString)));
                        break;
                    }
                    catch
                    {
                        await Task.Delay(5000);
                    }
                }


                var heartGroupClient = new HeartGroupClient("http://localhost:5000", "Write");
                var heartbeat        = await heartGroupClient.CreateHeartbeatAsync("PoGoNotifications.PokemonEncounter", Environment.MachineName, token);

                Console.WriteLine(Serialize(heartbeat));

                var checkResponse = await httpClient.PostAsync("http://localhost:5000/api/checkrunner", new ByteArrayContent(new byte[0]));

                var checkResponseString = await checkResponse.Content.ReadAsStringAsync();

                Console.WriteLine(Serialize(JsonConvert.DeserializeObject(checkResponseString)));

                var notificationResponse = await httpClient.PostAsync("http://localhost:5000/api/notificationrunner", new ByteArrayContent(new byte[0]));

                var notificationResponseString = await notificationResponse.Content.ReadAsStringAsync();

                Console.WriteLine(Serialize(JsonConvert.DeserializeObject(notificationResponseString)));

                await websiteTask;
            }
        }
        public async Task SendHeartbeatAsync(CancellationToken token)
        {
            var now = DateTimeOffset.UtcNow;

            if (now - _lastHeartbeat < MaximumFrequency)
            {
                return;
            }

            bool acquired = false;

            try
            {
                acquired = await _heartbeatLock.WaitAsync(TimeSpan.Zero);

                if (!acquired)
                {
                    return;
                }

                await _client.CreateHeartbeatAsync(_heartGroupName, Environment.MachineName, token);

                _lastHeartbeat = now;
            }
            catch (Exception exception)
            {
                _logger.LogWarning("Sending the heartbeat failed. Exception: {exception}", exception);
            }
            finally
            {
                if (acquired)
                {
                    _heartbeatLock.Release();
                }
            }
        }