Esempio n. 1
0
        private async void StartBackups()
        {
            while (true)
            {
                try
                {
                    await Task.Delay(TimeSpan.FromMinutes(1));

                    if (_initialized)
                    {
                        var tempFile = new FileInfo(_settings.BackupFile + ".temp");
                        await tempFile.DeleteAsync();

                        var files = _dataDir.EnumerateFiles()
                                    .Where(f => f.Name.StartsWith("db."))
                                    .Where(f => f.Name.EndsWith(".json.zip"))
                                    .ToList();
                        using (var zip = ZipFile.Open(tempFile.FullName, ZipArchiveMode.Update))
                            foreach (var file in files.Watch(files.Count))
                            {
                                using (await _fileLocks.GetOrAdd(file.FullName).EnterAsync())
                                {
                                    var entry = zip.CreateEntry(file.Name, CompressionLevel.Optimal);
                                    using var stream = entry.Open();
                                    using var reader = file.OpenRead();
                                    await reader.CopyToAsync(stream);

                                    await stream.FlushAsync();
                                }
                            }
                        await tempFile.MoveAsync(_settings.BackupFile);
                    }
                }
                catch (Exception e)
                {
                    Loggers.Error(e);
                }
            }
        }
Esempio n. 2
0
        public static async void StartLogging()
        {
            using (var logger = FileLogger.AppendDefault(typeof(ExecutionTimings)))
                while (true)
                {
                    try
                    {
                        await Task.Delay(TimeSpan.FromSeconds(15));

                        if (_timings.Count == 0)
                        {
                            continue;
                        }

                        var b        = new StringBuilder("***Execution Timings***").AppendLine();
                        var timings  = _timings.OrderByDescending(t => t.Value.ElapsedMilliseconds.Value).ToList();
                        var maxName  = timings.Max(t => t.Key.Length);
                        var maxCount = timings.Max(t => t.Value.Count.ToString().Length);
                        foreach (var t in timings)
                        {
                            var totalElapsed = TimeSpan.FromMilliseconds(t.Value.ElapsedMilliseconds);
                            var avgElapsed   = TimeSpan.FromMilliseconds(t.Value.ElapsedMilliseconds / t.Value.Count);
                            b.Append($"Name: {t.Key.PadRight(maxName)} ")
                            .Append($"Count: {t.Value.Count.ToString().PadLeft(maxCount)} ")
                            .Append($"Total Elapsed: {totalElapsed} ")
                            .Append($"Avg Elapsed: {avgElapsed} ")
                            .AppendLine();
                        }
                        await logger.ClearLog();

                        logger.Info(b.ToString());
                    }
                    catch (Exception e)
                    {
                        Loggers.Error(e);
                    }
                }
        }
Esempio n. 3
0
        private async Task StartLogging()
        {
            while (_running || _messages.Count > 0)
            {
                try
                {
                    var result = await _messages.ConsumeAsync(1000);

                    if (result.HasValue)
                    {
                        await Console.Out.WriteLineAsync(result.Value.Message);

                        await Console.Out.FlushAsync();
                    }
                }
                catch (Exception e)
                {
                    await Task.Delay(10000);

                    Loggers.Error(e);
                }
            }
        }