Exemplo n.º 1
0
        private void Working()
        {
            //received a downloaded user back from the github worker
            Receive <GithubWorkerActor.StarredReposForUser>(user =>
            {
                _githubProgressStats = _githubProgressStats.UserQueriesFinished();
                foreach (var repo in user.Repos)
                {
                    if (!_similarRepos.ContainsKey(repo.HtmlUrl))
                    {
                        _similarRepos[repo.HtmlUrl] = new SimilarRepo(repo);
                    }

                    //increment the number of people who starred this repo
                    _similarRepos[repo.HtmlUrl].SharedStarrers++;
                }
            });

            Receive <PublishUpdate>(update =>
            {
                //check to see if the job is done
                if (_receivedInitialUsers && _githubProgressStats.IsFinished)
                {
                    _githubProgressStats = _githubProgressStats.Finish();

                    //all repos minus forks of the current one
                    var sortedSimilarRepos = _similarRepos.Values
                                             .Where(x => x.Repo.Name != _currentRepo.Repo).OrderByDescending(x => x.SharedStarrers).ToList();
                    foreach (var subscriber in _subscribers)
                    {
                        subscriber.Tell(sortedSimilarRepos);
                    }
                    BecomeWaiting();
                }

                foreach (var subscriber in _subscribers)
                {
                    subscriber.Tell(_githubProgressStats);
                }
            });

            //completed our initial job - we now know how many users we need to query
            Receive <User[]>(users =>
            {
                _receivedInitialUsers = true;
                _githubProgressStats  = _githubProgressStats.SetExpectedUserCount(users.Length);

                //queue up all of the jobs
                foreach (var user in users)
                {
                    _githubWorker.Tell(new RetryableQuery(new GithubWorkerActor.QueryStarrer(user.Login), 3));
                }
            });

            Receive <GithubCommanderActor.CanAcceptJob>(job => Sender.Tell(new GithubCommanderActor.UnableToAcceptJob(job.Repo)));

            Receive <SubscribeToProgressUpdates>(updates =>
            {
                //this is our first subscriber, which means we need to turn publishing on
                if (_subscribers.Count == 0)
                {
                    Context.System.Scheduler.ScheduleTellRepeatedly(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100),
                                                                    Self, PublishUpdate.Instance, Self, _publishTimer);
                }

                _subscribers.Add(updates.Subscriber);
            });

            //query failed, but can be retried
            Receive <RetryableQuery>(query => query.CanRetry, query => _githubWorker.Tell(query));

            //query failed, can't be retried, and it's a QueryStarrers operation - means the entire job failed
            Receive <RetryableQuery>(query => !query.CanRetry && query.Query is GithubWorkerActor.QueryStarrers, query =>
            {
                _receivedInitialUsers = true;
                foreach (var subscriber in _subscribers)
                {
                    subscriber.Tell(new JobFailed(_currentRepo));
                }
                BecomeWaiting();
            });

            //query failed, can't be retried, and it's a QueryStarrers operation - means individual operation failed
            Receive <RetryableQuery>(query => !query.CanRetry && query.Query is GithubWorkerActor.QueryStarrer, query => _githubProgressStats.IncrementFailures());
        }
        private void Working()
        {
            //received a downloaded user back from the github worker
            Receive <StarredReposForUser>(user =>
            {
                _githubProgressStats = _githubProgressStats.UserQueriesFinished();
                foreach (var repo in user.Repos)
                {
                    if (!_similarRepos.ContainsKey(repo.HtmlUrl))
                    {
                        _similarRepos[repo.HtmlUrl] = new SimilarRepo(repo);
                    }

                    //increment the number of people who starred this repo
                    _similarRepos[repo.HtmlUrl].SharedStarrers++;
                }
            });

            Receive <PublishUpdate>(_ =>
            {
                //check to see if the job is done
                if (_receivedInitialUsers && _githubProgressStats.IsFinished)
                {
                    _githubProgressStats = _githubProgressStats.Finish();

                    //all repos minus forks of the current one
                    var sortedSimilarRepos = _similarRepos
                                             .Values
                                             .Where(x => x.Repo.Name != _currentRepo.Repo)
                                             .OrderByDescending(x => x.SharedStarrers)
                                             .ToArray();
                    foreach (var subscriber in _subscribers)
                    {
                        subscriber.Tell(sortedSimilarRepos);
                    }
                    BecomeWaiting();
                }

                foreach (var subscriber in _subscribers)
                {
                    subscriber.Tell(_githubProgressStats);
                }
            });

            //completed our initial job - we now know how many users we need to query
            Receive <IEnumerable <User> >(
                users => users?.Any() ?? false,
                users =>
            {
                _receivedInitialUsers = true;
                _githubProgressStats  = _githubProgressStats.SetExpectedUserCount(users.Count());

                //queue up all of the jobs
                foreach (var user in users)
                {
                    githubWorker.Tell(new RetryableQuery(new QueryStarrer(user.Login), 3));
                }
            });

            Receive <CanAcceptJob>(job => Sender.Tell(new UnableToAcceptJob(job)));

            Receive <SubscribeToProgressUpdates>(updates =>
            {
                //this is our first subscriber, which means we need to turn publishing on
                if (_subscribers.Count == 0)
                {
                    Context.System.Scheduler.ScheduleTellRepeatedly(
                        TimeSpan.FromMilliseconds(1000),
                        TimeSpan.FromMilliseconds(1000),
                        Self,
                        PublishUpdate.Instance,
                        Self,
                        _publishTimer);
                }

                _subscribers.Add(updates.Subscriber);
            });

            ReceiveQuery();
        }