예제 #1
0
        private void WebSourcesManager(GlobalSettings settings)
        {
            var rarePokemonRepositories = RarePokemonRepositoryFactory.CreateRepositories(settings);

            var repoTasks = rarePokemonRepositories.Select(rarePokemonRepository => StartPollRarePokemonRepository(settings, rarePokemonRepository)).Cast <Task>().ToList();

            var discordTask = TryStartDiscordReader();

            while (true)
            {
                if (!_clientWriter.Listener.Server.IsBound)
                {
                    Log.Info("Server has lost connection. Restarting...");
                    _clientWriter.StartNet(GlobalSettings.Port);
                }
                try
                {
                    // Manage repo tasks
                    for (var i = 0; i < repoTasks.Count; ++i)
                    {
                        var t = repoTasks[i];
                        if (t.Status != TaskStatus.Running && t.Status != TaskStatus.WaitingToRun && t.Status != TaskStatus.WaitingForActivation)
                        {
                            // Replace broken tasks with a new one
                            repoTasks[i].Dispose();
                            repoTasks[i] = StartPollRarePokemonRepository(settings, rarePokemonRepositories[i]);
                        }
                    }

                    // Manage Discord task
                    if (discordTask.Status != TaskStatus.Running && discordTask.Status != TaskStatus.WaitingToRun && discordTask.Status != TaskStatus.WaitingForActivation)
                    {
                        // Replace broken task with a new one
                        discordTask.Dispose();
                        discordTask = TryStartDiscordReader();
                    }
                }
                catch (Exception e)
                {
                    Log.Error($"Exception in thread manager: {e}");
                    throw;
                }
                Thread.Sleep(20 * 1000);
            }
        }
예제 #2
0
        private void PollRarePokemonRepositories(GlobalSettings globalSettings)
        {
            List <RarePokemonRepository> rarePokemonRepositories = RarePokemonRepositoryFactory.createRepositories(globalSettings);

            int delay = 30 * 1000;

            foreach (RarePokemonRepository rarePokemonRepository in rarePokemonRepositories)
            {
                var cancellationTokenSource = new CancellationTokenSource();
                var token    = cancellationTokenSource.Token;
                var listener = Task.Factory.StartNew(async() =>
                {
                    Thread.Sleep(5 * 1000);
                    while (true)
                    {
                        Thread.Sleep(delay);
                        for (int retrys = 0; retrys <= 3; retrys++)
                        {
                            var pokeSniperList = rarePokemonRepository.FindAll();
                            if (pokeSniperList != null)
                            {
                                if (pokeSniperList.Any())
                                {
                                    await feedToClients(pokeSniperList, rarePokemonRepository.GetChannel());
                                }
                                else
                                {
                                    Log.Debug("No new pokemon on {0}", rarePokemonRepository.GetChannel());
                                }
                                break;
                            }
                            if (token.IsCancellationRequested)
                            {
                                break;
                            }
                            Thread.Sleep(1000);
                        }
                    }
                }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
        }