Esempio n. 1
0
        static Utils()
        {
            LogFolder = Consts.LogsFolder;
            LogFile   = Consts.LogFile;
            Consts.LocalAppDataPath.CreateDirectory();
            Consts.LogsFolder.CreateDirectory();

            var options = new DbOptions().SetCreateIfMissing(true);

            _hashCache = RocksDb.Open(options, (string)Consts.LocalAppDataPath.Combine("GlobalHashCache.rocksDb"));

            _startTime = DateTime.Now;

            if (LogFile.Exists)
            {
                var newPath = Consts.LogsFolder.Combine(Consts.EntryPoint.FileNameWithoutExtension + LogFile.LastModified.ToString(" yyyy-MM-dd HH_mm_ss") + ".log");
                LogFile.MoveToAsync(newPath, true).Wait();
            }

            var logFiles = LogFolder.EnumerateFiles(false).ToList();

            if (logFiles.Count >= Consts.MaxOldLogs)
            {
                Log($"Maximum amount of old logs reached ({logFiles.Count} >= {Consts.MaxOldLogs})");
                var filesToDelete = logFiles
                                    .Where(f => f.IsFile)
                                    .OrderBy(f => f.LastModified)
                                    .Take(logFiles.Count - Consts.MaxOldLogs)
                                    .ToList();

                Log($"Found {filesToDelete.Count} old log files to delete");

                var success = 0;
                var failed  = 0;
                filesToDelete.Do(f =>
                {
                    try
                    {
                        f.Delete();
                        success++;
                    }
                    catch (Exception e)
                    {
                        failed++;
                        Log($"Could not delete log at {f}!\n{e}");
                    }
                });

                Log($"Deleted {success} log files, failed to delete {failed} logs");
            }

            var watcher = new FileSystemWatcher((string)Consts.LocalAppDataPath);

            AppLocalEvents = Observable.Merge(Observable.FromEventPattern <FileSystemEventHandler, FileSystemEventArgs>(h => watcher.Changed += h, h => watcher.Changed -= h).Select(e => (FileEventType.Changed, e.EventArgs)),
                                              Observable.FromEventPattern <FileSystemEventHandler, FileSystemEventArgs>(h => watcher.Created += h, h => watcher.Created -= h).Select(e => (FileEventType.Created, e.EventArgs)),
                                              Observable.FromEventPattern <FileSystemEventHandler, FileSystemEventArgs>(h => watcher.Deleted += h, h => watcher.Deleted -= h).Select(e => (FileEventType.Deleted, e.EventArgs)))
                             .ObserveOn(RxApp.TaskpoolScheduler);
            watcher.EnableRaisingEvents = true;
            InitPatches();
        }
Esempio n. 2
0
        public static async Task CompactFolder(this AbsolutePath folder, WorkQueue queue, Algorithm algorithm)
        {
            var driveInfo   = folder.DriveInfo().DiskSpaceInfo;
            var clusterSize = driveInfo.SectorsPerCluster * driveInfo.BytesPerSector;

            await folder
            .EnumerateFiles(true)
            .Where(f => f.Size > clusterSize)
            .PMap(queue, async path =>
            {
                Utils.Status($"Compacting {path.FileName}");
                await path.Compact(algorithm);
            });
        }
Esempio n. 3
0
        public static async Task InitializeLogging()
        {
            _startTime = DateTime.Now;

            if (LoggingSettings.LogToFile)
            {
                LogFolder = Consts.LogsFolder;
                LogFile   = Consts.LogFile;
                Consts.LocalAppDataPath.CreateDirectory();
                Consts.LogsFolder.CreateDirectory();

                if (LogFile.Exists)
                {
                    var newPath = Consts.LogsFolder.Combine(Consts.EntryPoint.FileNameWithoutExtension + LogFile.LastModified.ToString(" yyyy-MM-dd HH_mm_ss") + ".log");
                    await LogFile.MoveToAsync(newPath, true);
                }

                var logFiles = LogFolder.EnumerateFiles(false).ToList();
                if (logFiles.Count >= Consts.MaxOldLogs)
                {
                    Log($"Maximum amount of old logs reached ({logFiles.Count} >= {Consts.MaxOldLogs})");
                    var filesToDelete = logFiles
                                        .Where(f => f.IsFile)
                                        .OrderBy(f => f.LastModified)
                                        .Take(logFiles.Count - Consts.MaxOldLogs)
                                        .ToList();

                    Log($"Found {filesToDelete.Count} old log files to delete");

                    var success = 0;
                    var failed  = 0;
                    filesToDelete.Do(f =>
                    {
                        try
                        {
                            f.Delete();
                            success++;
                        }
                        catch (Exception e)
                        {
                            failed++;
                            Log($"Could not delete log at {f}!\n{e}");
                        }
                    });

                    Log($"Deleted {success} log files, failed to delete {failed} logs");
                }
            }
        }