Exemple #1
0
        public static void StartCleanup()
        {
            CleanupLog.WriteMethod();

            _directoryToCleanupQueue = new BlockingQueue <string>(new Queue <string>());

            _continueRunning = true;

            // Initialize Configuration
            ReadConfigFile();

            // Connect to Remote Shares
            ConnectToRemoteShares();

            // Setup Global Cleanup Timer
            if (ConfigurationSettings.EnableGlobalTimerCleanup)
            {
                _cleanupTimer.Interval = TimeHelper.ConvertMinutesToMilliseconds(ConfigurationSettings.HowOftenInMinutesToCleanup);
                _cleanupTimer.Elapsed += (s, e) => Cleanup();
                _cleanupTimer.Start();
            }

            // Setup Individual Cleanup Directory Watchers
            if (ConfigurationSettings.EnableFileWatcherCleanup)
            {
                ConfigurationSettings.DirectoriesToCleanup.ForEach(d => AddDirectoryToCleanup(d));
            }

            // Perform Initial Cleanup
            if (ConfigurationSettings.EnableGlobalTimerCleanup || ConfigurationSettings.EnableFileWatcherCleanup)
            {
                _continueRunning = true;

                Cleanup();

                CleanupLog.WriteLine($"Initial Cleanup Totals:  Found {_numberOfFilesProcessed} files, Deleted {_numberOfFilesDeleted} files.");
            }
            else
            {
                CleanupLog.WriteLine("No Cleanup is enabled. Exiting");

                StopCleanup();

                return;
            }

            // Monitor Directories for Change, Checking Delete Queue Until Aborted
            while (_continueRunning)
            {
                Thread.Sleep(3000); // Wait 3 Seconds between Checks

                if (_isCleaning)
                {
                    return;
                }

                var path = _directoryToCleanupQueue.Dequeue();
                if (path != null)
                {
                    CleanupFolder(path);
                }

                CleanupLog.WriteLine($"Cleanup Totals:  Found {_numberOfFilesProcessed} files, Deleted {_numberOfFilesDeleted} files.");
            }
        }