Esempio n. 1
0
        //Some exceptions may go unobserved in this method, as not all created Tasks are awaited if problems occur
        /// <summary>
        /// <para>Preconditions: The associated <see cref="DiscordInterfacer"/> of the indirectly-associated <see cref="ProcessTagCommandActivity"/> is in a Connected state</para>
        /// </summary>
        public async Task Execute()
        {
            Log.Application_.LogVerbose("Starting processing of latest Comments");
            Settings        CurrentSettings;
            Task <Settings> CurrentSettingsTask = RepositorySettings.LoadSettings();

            //Robustness — clear the cache in case it hasn't been cleared for some reason
            RepositoryTaglists.ClearCache();

            /*
             * Initialize the Taglist Repository cache, so that subsequent calls to Load
             * by the ProcessCommentActivity SubActivity tasks
             * will efficiently retrieve the cached result
             */
            //TODO Implement proper lazy-loading in CacheingTaglistRepository, so that results are cached upon calls to Load and not just LoadAll
            Task InitializeCacheTask = RepositoryTaglists.LoadAll();

            try{
                CurrentSettings = await CurrentSettingsTask;
            }catch (DataAccessException Error) {
                Log.Application_.LogError("Error loading Settings while pulling latest Comments from Imgur; pull aborted: " + Error.Message);
                return;
            }
            Task <IDictionary <string, IList <IComment> > > NewCommentsTask = Imgur.ReadCommentsSince(
                CurrentSettings.CommentsProcessedUpToInclusive,
                CurrentSettings.CommenterUsernames,
                CurrentSettings.RequestThreshholdPullCommentsPerUser
                );
            IDictionary <string, IList <IComment> > NewComments;

            try{
                NewComments = await NewCommentsTask;
            }catch (ImgurException Error) {
                Log.Application_.LogError("Error pulling latest Comments from Imgur: " + Error.Message);
                return;
            }
            try{
                await InitializeCacheTask;
            }catch (DataAccessException Error) {
                Log.Application_.LogError("Error loading Taglists while pulling Imgur Comments; Comment processing aborted: " + Error.Message);
                return;
            }
            //Execute the sub-activity for each new Comment, keeping track of the latest date–time from all the Comments
            Log.Application_.LogVerbose("Processing latest Comments from {0} total Commenters", NewComments.Count);
            bool           AnyCommentsProcessed = false;
            DateTimeOffset LatestCommentAt      = DateTimeOffset.MinValue;

            foreach (KeyValuePair <string, IList <IComment> > _NewUserComments in NewComments)
            {
                IList <IComment> NewUserComments = _NewUserComments.Value;
                //Process Comments from a particular user, oldest Comment first
                Log.Application_.LogVerbose("Processing latest Comments from Commenter '{0}', total — {1}", _NewUserComments.Key, NewUserComments.Count);
                foreach (IComment NewUserComment in NewUserComments.Reverse())
                {
                    bool CommentsProcessed = await SubActivity.ExecuteIfNew(NewUserComment);

                    if (CommentsProcessed)
                    {
                        AnyCommentsProcessed = true;
                    }
                    if (NewUserComment.DateTime > LatestCommentAt)
                    {
                        LatestCommentAt = NewUserComment.DateTime;
                    }
                }
            }
            RepositoryTaglists.ClearCache();
            //Update and save the latest Comment date–time if it has progressed
            Log.Application_.LogVerbose("Latest date–time of all Comments read — {0:u}; current saved value is {1:u}", LatestCommentAt, CurrentSettings.CommentsProcessedUpToInclusive);
            if (LatestCommentAt > CurrentSettings.CommentsProcessedUpToInclusive)
            {
                Log.Application_.LogVerbose("Updating and saving latest processed Comment date–time");
                CurrentSettings.CommentsProcessedUpToInclusive = LatestCommentAt;
                try{
                    await RepositorySettings.SaveWritableSettings(CurrentSettings);
                }catch (DataAccessException Error) {
                    Log.Application_.LogError(
                        "Unable to save updated Settings with new 'Last Processed Comment date–time' of {0:u}; it would be advisable, though not essential, to update the settings file manually with the updated value. Details: {1}",
                        CurrentSettings.CommentsProcessedUpToInclusive, Error.Message
                        );
                }
            }
            if (AnyCommentsProcessed)
            {
                await Imgur.LogRemainingBandwidth(TraceEventType.Information);
            }
            else
            {
                await Imgur.LogRemainingBandwidth(TraceEventType.Verbose);
            }
        }