Exemplo n.º 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var currentStatusRecordIds = _statusRepository
                                                 .GetLatestStatusRecordForEachLocation()
                                                 .Select(sr => sr.Id)
                                                 .ToList();

                    var oldRecords = await _statusRepository.GetStatusMonitorRepliesOlderThan(DateTime.Now.AddDays(-14));

                    var recordsToDelete = oldRecords.Where(sr => currentStatusRecordIds.All(id => sr.Id != id)).ToList();
                    await _statusRepository.DeleteStatusRecords(recordsToDelete);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                }
                finally
                {
                    await Task.Delay(1000 * 60 * 15, stoppingToken); // 15 minutes
                }
            }
        }