Example #1
0
        public static async Task Main(string[] args)
        {
            DateTime startTime;

            try
            {
                _settings  = LoadSettings(args);
                _container = LoadDiContainer();
                startTime  = DateTime.UtcNow;
                while (true)
                {
                    await StartPurge();

                    startTime = GetNextStartTime(startTime);
                    using (ILifetimeScope scope = _container.BeginLifetimeScope())
                    {
                        SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
                        ITraceService   traceService    = scope.Resolve <ITraceService>();
                        await WriteTrace(traceService, settingsFactory, $"Next start time {startTime:O}");
                    }
                    await Task.Delay(startTime.Subtract(DateTime.UtcNow));
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }
            finally
            {
                if (_container != null)
                {
                    _container.Dispose();
                }
            }
        }
Example #2
0
 public LogSettings(Settings settings,
                    AccountInterface.ITokenService tokenService,
                    SettingsFactory settingsFactory)
 {
     _settings        = settings;
     _tokenService    = tokenService;
     _settingsFactory = settingsFactory;
 }
Example #3
0
        private static async Task Purge(IPurgeWorker purgeWorker)
        {
            using ILifetimeScope scope = _container.BeginLifetimeScope();
            SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();

            if (purgeWorker.Status == PurgeWorkerStatus.InProgress)
            {
                await Task.WhenAll(
                    Task.Run(() => UpdateExceptionMetaData(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             ),
                    Task.Run(() => UpdateMetricMetaData(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             ),
                    Task.Run(() => UpdateTraceMetaData(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             )
                    );

                await Task.WhenAll(
                    Task.Run(() => PurgeException(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             ),
                    Task.Run(() => PurgeMetric(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             ),
                    Task.Run(() => PurgeTrace(
                                 settingsFactory,
                                 scope.Resolve <ITraceService>(),
                                 scope.Resolve <IPurgeSaver>(),
                                 purgeWorker.DomainId
                                 )
                             )
                    );

                purgeWorker.Status = PurgeWorkerStatus.Complete;
            }
        }
Example #4
0
        private static async Task StartPurge()
        {
            using ILifetimeScope scope = _container.BeginLifetimeScope();
            SettingsFactory settingsFactory = scope.Resolve <SettingsFactory>();
            ITraceService   traceService    = scope.Resolve <ITraceService>();

            await WriteTrace(traceService, settingsFactory, "Start Purge");
            await InitializeWorkers();

            CoreSettings        settings      = settingsFactory.CreateCore(_settings);
            IPurgeWorkerFactory workerFactory = scope.Resolve <IPurgeWorkerFactory>();
            IPurgeWorkerSaver   workerSaver   = scope.Resolve <IPurgeWorkerSaver>();
            Guid?workerId = await workerFactory.Claim(settings);

            while (workerId.HasValue && !workerId.Value.Equals(Guid.Empty))
            {
                await WriteTrace(traceService, settingsFactory, $"Claimed worker {workerId.Value}");

                IPurgeWorker purgeWorker = await workerFactory.Get(settings, workerId.Value);

                try
                {
                    await Purge(purgeWorker);
                }
                catch (System.Exception ex)
                {
                    purgeWorker.Status = PurgeWorkerStatus.Error;
                    try
                    {
                        await scope.Resolve <IExceptionService>().Create(settingsFactory.CreateLog(_settings), _settings.ExceptionLoggingDomainId, ex);
                    }
                    catch { }
                    throw;
                }
                finally
                {
                    await workerSaver.Update(settings, purgeWorker);
                    await WriteTrace(traceService, settingsFactory, $"Updated Worker {workerId.Value} Status {purgeWorker.Status}");
                }
                workerId = await workerFactory.Claim(settings);
            }
            try
            {
                await WriteTrace(traceService, settingsFactory, "Purging meta data");
                await PurgMetaData(settingsFactory, scope.Resolve <IPurgeSaver>());
            }
            catch (System.Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
                try
                {
                    await scope.Resolve <IExceptionService>().Create(settingsFactory.CreateLog(_settings), _settings.ExceptionLoggingDomainId, ex);
                }
                catch { }
            }
        }
Example #5
0
 /// <summary>
 /// Calling stored procs to delete old purge meta data records
 /// </summary>
 private static async Task PurgMetaData(SettingsFactory settingsFactory, IPurgeSaver saver)
 {
     DateTime     minTimestamp = SubtractSettingsTimespan(DateTime.UtcNow.Date, _settings.PurgeMetaDataTimespan).ToUniversalTime();
     CoreSettings settings     = settingsFactory.CreateCore(_settings);
     await Task.WhenAll(
         saver.DeleteExceptionByMinTimestamp(settings, minTimestamp),
         saver.DeleteMetricByMinTimestamp(settings, minTimestamp),
         saver.DeleteTraceByMinTimestamp(settings, minTimestamp)
         );
 }
Example #6
0
 /// <summary>
 /// Calling stored procedure to
 /// 1) delete old completed workers
 /// 2) reset hung workers
 /// 3) add domain workers
 /// </summary>
 private static async Task InitializeWorkers()
 {
     using (ILifetimeScope scope = _container.BeginLifetimeScope())
     {
         SettingsFactory   settingsFactory = scope.Resolve <SettingsFactory>();
         IPurgeWorkerSaver saver           = scope.Resolve <IPurgeWorkerSaver>();
         await saver.InitializePurgeWorker(settingsFactory.CreateCore(_settings));
         await WriteTrace(scope.Resolve <ITraceService>(), settingsFactory, "Workers Initialized");
     }
 }
Example #7
0
        private static async Task PurgeTrace(
            SettingsFactory settingsFactory,
            ITraceService traceService,
            IPurgeSaver purgeSaver,
            Guid domainId)
        {
            DateTime     maxTimestamp = DateTime.UtcNow;
            CoreSettings settings     = settingsFactory.CreateCore(_settings);

            await WriteTrace(traceService, settingsFactory, $"Purging traces");

            await purgeSaver.PurgeTrace(settings, domainId, maxTimestamp);
        }
Example #8
0
        private static async Task UpdateTraceMetaData(
            SettingsFactory settingsFactory,
            ITraceService traceService,
            IPurgeSaver purgeSaver,
            Guid domainId)
        {
            DateTime     minTimestamp = SubtractSettingsTimespan(DateTime.UtcNow.Date, _settings.RetensionPeriod).ToUniversalTime();
            CoreSettings settings     = settingsFactory.CreateCore(_settings);
            DateTime     expiration   = DateTime.UtcNow;

            expiration = new DateTime(expiration.Year, expiration.Month, 1).AddMonths(1);
            await WriteTrace(traceService, settingsFactory, $"Updating meta data for traces created up to {minTimestamp}");

            await purgeSaver.InitializeTrace(settings, domainId, expiration, minTimestamp);
        }
Example #9
0
 public static async Task WriteTrace(ITraceService traceService, SettingsFactory settingsFactory, string value)
 {
     Console.WriteLine(value);
     await traceService.Create(settingsFactory.CreateLog(_settings), _settings.TraceLoggingDomainId, "log-purger", value);
 }