示例#1
0
        /// <summary>
        /// cleanup transient filters over n minutes old
        /// </summary>
        public async Task <int> FilterCleanup()
        {
            Console.WriteLine("cleanup task is about to run");
            var startUtc           = DateTime.UtcNow;
            var deleteOlderThanUtc = startUtc.AddMinutes(-_ageInMinutesToDelete).ToString("yyyy-MM-dd HH:mm:ss"); // mySql requires this format

            _log.LogInformation($"FilterCleanup: ageInMinutesToDelete: {_ageInMinutesToDelete} deleteOlderThanUtc: {deleteOlderThanUtc}.");

            Dictionary <string, object> newRelicAttributes;
            var deletedCount = 0;

            try
            {
                deletedCount = await _filterRepository.DeleteTransientFilters(deleteOlderThanUtc);
            }
            catch (Exception ex)
            {
                newRelicAttributes = new Dictionary <string, object> {
                    { "message", string.Format($"execute delete on filter DB exeception: {ex.Message}") }
                };
                NewRelicUtils.NotifyNewRelic("FilterCleanup", "Fatal", startUtc, _log, newRelicAttributes);
                _serviceExceptionHandler.ThrowServiceException(HttpStatusCode.InternalServerError, 78, ex.Message);
            }

            newRelicAttributes = new Dictionary <string, object> {
                { "message", "Task completed." }, { "ageInMinutesToDelete", _ageInMinutesToDelete }, { "deleteOlderThanUtc", deleteOlderThanUtc }, { "deletedCount", deletedCount }
            };
            NewRelicUtils.NotifyNewRelic("FilterCleanup", "Information", startUtc, _log, newRelicAttributes);

            return(deletedCount);
        }