Inheritance: IFixtureController
        private void TimerEvent(IService theService)
        {
            if (FixtureController.IsAddingItems)
            {
                return;
            }

            try
            {
                FixtureController.BeginAddingItems();
                var features = theService.GetFeatures();
                Parallel.ForEach(_sportsList, new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                                 sport =>
                {
                    _logger.Info($"Get feature = {sport}");

                    var theFeature = features.Find(f => f.Name.Equals(sport));
                    if (theFeature != null)
                    {
                        _logger.InfoFormat("Get the list of available fixtures for {0} from GTP", sport);
                        var fixtures = theFeature.GetResources();

                        if (fixtures != null && fixtures.Count > 0)
                        {
                            var tmpSport = sport;
                            Parallel.ForEach(fixtures, new ParallelOptions {
                                MaxDegreeOfParallelism = 10
                            },
                                             fixture => ProcessFixture(fixture, tmpSport));
                        }
                        else
                        {
                            _logger.InfoFormat("There are currently no {0} fixtures in UDAPI", sport);
                        }
                    }
                    else
                    {
                        _logger.InfoFormat("Cannot find {0} in UDAPI....", sport);
                    }
                });
            }
            catch (AggregateException aggex)
            {
                foreach (var innerException in aggex.InnerExceptions)
                {
                    _logger.Error(innerException);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            finally
            {
                FixtureController.FinishAddingItems();
            }
        }
        private void ProcessFixture(IResource fixture, string sport)
        {
            if (!FixtureController.Contains(fixture.Id))
            {
                try
                {
                    var matchStatus = 0;

                    if (fixture.Content != null)
                    {
                        matchStatus = fixture.Content.MatchStatus;
                    }

                    //if not match over
                    if (matchStatus != (int)SSMatchStatus.MatchOver)
                    {
                        _logger.InfoFormat("Get UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);
                        var snapshotString = fixture.GetSnapshot();
                        _logger.InfoFormat("Successfully retrieved UDAPI Snapshot for {0} id {1}", fixture.Name, fixture.Id);

                        var fixtureSnapshot = snapshotString.FromJson <Fixture>();
                        var epoch           = fixtureSnapshot.Epoch;

                        //process the snapshot here and push it into the client system

                        try
                        {
                            FixtureController.AddListener(fixture.Id, () => new StreamListener(fixture, epoch));
                        }
                        catch (Exception)
                        {
                            _logger.ErrorFormat("sport=\"{0}\" : fixtureName=\"{1}\" - Unable to stream fixture", sport, fixture.Name);
                            throw;
                        }
                    }
                    else
                    {
                        _logger.InfoFormat("Fixture {0} id {1} has finished. Will not process", fixture.Name, fixture.Id);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Fixture {0} id {1} There is a problem processing this fixture", fixture.Name, fixture.Id), ex);
                }
            }
            else
            {
                bool isMatchOver = FixtureController.Contains(fixture.Id) && FixtureController.GetItem(fixture.Id).FixtureEnded;

                if (isMatchOver)
                {
                    if (FixtureController.Contains(fixture.Id))
                    {
                        FixtureController.RemoveFixture(fixture.Id);
                        _logger.InfoFormat("fixtureName=\"{0}\" fixtureId={1} is over.", fixture.Name, fixture.Id);
                    }
                }
                else
                {
                    _logger.InfoFormat("fixtureName=\"{0}\" fixtureId={1} is currently being processed", fixture.Name, fixture.Id);
                }

                _logger.InfoFormat("Fixture {0} id {1} is currently being processed", fixture.Name, fixture.Id);

                if (_listeners.ContainsKey(fixture.Id))
                {
                    if (_listeners[fixture.Id].FixtureEnded)
                    {
                        StreamListener theListener;
                        if (_listeners.TryRemove(fixture.Id, out theListener))
                        {
                            _logger.InfoFormat("Fixture {0} id {1} is over.", fixture.Name, fixture.Id);
                        }

                        bool activeFixture;
                        _activeFixtures.TryRemove(fixture.Id, out activeFixture);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void ConfigurationUpdated(object sender, FileSystemEventArgs fileSystemEventArgs)
        {
            if (fileSystemEventArgs.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }

            string command = null;

            var getCommand = new Func <string>(() =>
            {
                lock (_watcher)
                {
                    using (var sr = new StreamReader(_filePath))
                    {
                        command = sr.ReadLine();
                    }
                    using (var sw = new StreamWriter(_filePath, false))
                    {
                        sw.Write(String.Empty);
                    }

                    return(command != null ? command.Trim() : command);
                }
            });

            int maxTries = 0;

            while (maxTries < 3)
            {
                try
                {
                    getCommand();
                    maxTries = 3;
                }
                catch (Exception)
                {
                    maxTries++;
                    Thread.Sleep(1000);
                }
            }


            if (string.IsNullOrEmpty(command))
            {
                return;
            }

            if (command.StartsWith("STOP:"))
            {
                var fixtureId = command.Substring(5);
                FixtureController.StopFixture(fixtureId);
            }

            if (command.StartsWith("BLOCK:"))
            {
                var fixtureId = command.Substring(6);
                FixtureController.StopFixture(fixtureId, true);
            }

            if (command.StartsWith("RESTART:"))
            {
                var fixtureId = command.Substring("RESTART:".Length);
                FixtureController.RestartFixture(fixtureId);
            }
        }