Example #1
0
        public async Task Cleanup(
            [Summary("mode", "which data you want to delete")] CleanupMode cleanupMode,
            [Summary("channel", "where to delete, defaults to current.")] ITextChannel channel = null,
            [Summary("count", "how many messages to scan for your mode.")] long count          = 100,
            [Summary("user", "additional filter on this user")] IUser filterUser = null)
        {
            if (channel == null && Context.Channel is ITextChannel txtChnl)
            {
                channel = txtChnl;
            }

            if (channel == null)
            {
                await Context.Interaction.RespondAsync(Translator.T().CmdOnlyTextChannel());

                return;
            }

            await Context.Interaction.RespondAsync("Cleaning channels...", ephemeral : true);

            if (count > 1000)
            {
                count = 1000;
            }

            var func = new Func <IMessage, bool>(m => true);

            switch (cleanupMode)
            {
            case CleanupMode.Bots:
                func = IsFromBot;
                break;

            case CleanupMode.Attachments:
                func = HasAttachment;
                break;
            }

            int deleted = 0;

            try
            {
                deleted = await IterateAndDeleteChannels(channel, (int)count, func, Context.User, filterUser);
            }
            catch (HttpException ex)
            {
                if (ex.HttpCode == HttpStatusCode.Forbidden)
                {
                    await Context.Interaction.ModifyOriginalResponseAsync(msg => msg.Content = Translator.T().CmdCannotViewOrDeleteInChannel());
                }
                else if (ex.HttpCode == HttpStatusCode.Forbidden)
                {
                    await Context.Interaction.ModifyOriginalResponseAsync(msg => msg.Content = Translator.T().CmdCannotFindChannel());
                }

                return;
            }
            await Context.Interaction.ModifyOriginalResponseAsync(msg => msg.Content = Translator.T().CmdCleanup(deleted, channel));
        }
        //========================================================================================= Constructors

        public PreviewCleaner(string path  = null, CleanupMode cleanupMode = CleanupMode.AllVersions,
                              int maxIndex = 0, int maxDegreeOfParallelism = 10, int blockSize = 500)
        {
            Path     = path;
            MaxIndex = Math.Max(0, maxIndex);
            Mode     = cleanupMode;

            MaxDegreeOfParallelism = Math.Max(1, maxDegreeOfParallelism);
            BlockSize = Math.Max(1, blockSize);
        }
Example #3
0
        public void GetCleanupReturnsCorrectCleanupType(CleanupMode cleanupMode, Type expected)
        {
            // Arrange

            // Act
            var cleanup = Program.GetCleanup(cleanupMode);

            // Assert
            Assert.IsType(expected, cleanup);
        }
Example #4
0
        public void GetCleanupReturnsCorrectCleanupType(CleanupMode cleanupMode, Type expected)
        {
            // Arrange

            // Act
            var cleanup = Program.GetCleanup(cleanupMode);

            // Assert
            Assert.IsType(expected, cleanup);
        }
Example #5
0
        private static void Cleanup(CleanupMode cleanupMode)
        {
            Logger.Info("Cleanup mode: {0}", cleanupMode);
            Logger.Info("Cleaning up...");

            var cleanup = GetCleanup(cleanupMode);

            cleanup.Cleanup();

            Logger.Info("Finished cleaning up.");
        }
Example #6
0
        public void GetCleanupModeReturnsCorrectCleanupMode(string argument, CleanupMode expected)
        {
            // Arrange
            var args = new List<string> { argument };

            // Act
            var cleanupMode = Program.GetCleanupMode(args);

            // Assert
            Assert.Equal(expected, cleanupMode);
        }
Example #7
0
        public void GetCleanupModeReturnsCorrectCleanupMode(string argument, CleanupMode expected)
        {
            // Arrange
            var args = new List <string> {
                argument
            };

            // Act
            var cleanupMode = Program.GetCleanupMode(args);

            // Assert
            Assert.Equal(expected, cleanupMode);
        }
Example #8
0
 public static ICleanup GetCleanup(CleanupMode cleanupMode)
 {
     switch (cleanupMode)
     {
         case CleanupMode.UnpublishedScoreCards:
             return Container.Resolve<UnpublishedScoreCardsCleanup>();
         case CleanupMode.DuplicateScoreCards:
             return Container.Resolve<DuplicateScoreCardsCleanup>();
         case CleanupMode.InactiveProfiles:
             return Container.Resolve<InactiveProfilesCleanup>();
         default:
             throw new ArgumentOutOfRangeException(nameof(cleanupMode));
     }
 }
Example #9
0
        public static ICleanup GetCleanup(CleanupMode cleanupMode)
        {
            switch (cleanupMode)
            {
            case CleanupMode.UnpublishedScoreCards:
                return(Container.Resolve <UnpublishedScoreCardsCleanup>());

            case CleanupMode.DuplicateScoreCards:
                return(Container.Resolve <DuplicateScoreCardsCleanup>());

            case CleanupMode.InactiveProfiles:
                return(Container.Resolve <InactiveProfilesCleanup>());

            default:
                throw new ArgumentOutOfRangeException(nameof(cleanupMode));
            }
        }
 public CleanupAttribute(CleanupMode cleanupMode)
 {
     this.cleanupMode = cleanupMode;
 }
        /// <summary>
        /// Performs cleanup on the dictionaries in either MakeRoom or Maintenance mode. Returns true if the goal was achieved, false if the cleanup was canceled because another cleanup was executing conurrently.
        /// </summary>
        /// <param name="mode"></param>
        private bool Cleanup(CleanupMode mode)
        {
            if (mode == CleanupMode.MakeRoom && byteCeiling < 1)
            {
                return(true);                                                 //We don't perform minimal cleanups unless a ceiling is specified.
            }
            //We have to track removed items so we can fire events later.
            List <KeyValuePair <T, int> > removed = CounterRemoved != null ? new List <KeyValuePair <T, int> >() : null;

            //We have to weak lock method-level, because otherwise a background thread could be cleaning when GetOrAdd is called, and we could have a deadlock
            //With syncLock locked in GetOrAdd, waiting on cleanupLock, and Cleanup locked on cleanupLock, waiting on GetOrAdd.
            //If we didn't have any method-level lock, we'd waste resources with simultaneous cleanup runs.
            //sortLock is redundant with cleanupLock, but remains in case I decide to pullt the method level lock
            if (!Monitor.TryEnter(cleanupLock))
            {
                return(false);                                //Failed to lock, another thread is cleaning.
            }
            try {
                //In high precision, we lock the entire long-running process
                if (precision == EventCountingStrategy.ThreadingPrecision.Accurate)
                {
                    Monitor.Enter(syncLock);
                }
                try {
                    EventCounter[] counters;
                    //In fast mode, only lock for the copy and delete. We can sort outside after taking a snapshot.
                    //We wont remove newly added ones, but thats ok.
                    lock (syncLock) {
                        if (mode == CleanupMode.MakeRoom && bytesUsed < byteCeiling)
                        {
                            return(true);                                                         //Nothing to do, there is stil room
                        }
                        counters = new EventCounter[countersToKeys.Count];
                        countersToKeys.Keys.CopyTo(counters, 0); //Clone
                    }
                    //Lock for sorting so we have synchronized access to a.sortValue
                    lock (sortLock) {
                        //Pause values
                        for (int i = 0; i < counters.Length; i++)
                        {
                            counters[i].sortValue = counters[i].GetValue();
                        }
                        //Sort lowest counters to the top using quicksort
                        Array.Sort <EventCounter>(counters, delegate(EventCounter a, EventCounter b) {
                            return(a.sortValue - b.sortValue);
                        });
                    }
                    //Go back into lock
                    lock (syncLock) {
                        long removedBytes = 0;
                        long goal         = byteCeiling / 10; //10% is our goal if we are removing minimally.
                        //Remove items
                        for (int i = 0; i < counters.Length; i++)
                        {
                            EventCounter c = counters[i];
                            if (mode == CleanupMode.MakeRoom && removedBytes >= goal)
                            {
                                return(true);                                                      //Done, we hit our goal!
                            }
                            if (mode == CleanupMode.Maintenance && c.sortValue > 0)
                            {
                                return(true);                                                    //Done, We hit the end of the zeros
                            }
                            if (mode == CleanupMode.Maintenance && c.GetValue() > 0)
                            {
                                continue;                                                      //Skip counters that incremeted while we were working.
                            }
                            //Look up key
                            T key;
                            countersToKeys.TryGetValue(c, out key);
                            if (key.Equals(default(T)))
                            {
                                continue;                         //Skip counters that have already been removed
                            }
                            //Remove counter
                            countersToKeys.Remove(c);
                            keysToCounters.Remove(key);
                            //Increment our local byte removal counter
                            removedBytes += c.CustomSize + itemSize;
                            //Decrement global counter
                            bytesUsed = bytesUsed - c.CustomSize - itemSize;
                            //store removed keys
                            if (removed != null)
                            {
                                removed.Add(new KeyValuePair <T, int>(key, c.GetValue()));
                            }
                        }
                    }
                } finally {
                    if (precision == EventCountingStrategy.ThreadingPrecision.Accurate)
                    {
                        Monitor.Exit(syncLock);
                    }
                }
            } finally {
                Monitor.Exit(cleanupLock);

                //Fire CounterRemoved event (may still be in syncLock)!
                if (removed != null && this.CounterRemoved != null)
                {
                    foreach (KeyValuePair <T, int> p in removed)
                    {
                        CounterRemoved(this, p.Key, p.Value);
                    }
                }
            }


            return(true);
        }
Example #12
0
 public CleanupAttribute(CleanupMode mode)
 {
     Mode = mode;
 }