Branch: Tag:

2014-12-02

2014-12-02 13:30:22 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Logging: Remove log notices after 7 days. Fixes [bug 6950 (#6950)].

Adds a background process that runns once every hour that removes
stale entries from the server event log.

The current configuration is to keep notices for 7 days, and all
other entries for as long as the server stays up.

520:    // Zap some of the remaining caches.    destruct(argcache);    destruct(cache); +  stop_error_log_cleaner();   #ifdef THREADS   #if constant(Filesystem.Monitor.basic)    stop_fsgarb();
3223:    report_debug( s );   }    + protected BackgroundProcess error_log_cleaner_process; +  + protected void clean_error_log(mapping(string:array(int)) log, +  mapping(string:int) cutoffs) + { +  if (!log || !sizeof(log)) return; +  foreach(cutoffs; string prefix; int cutoff) { +  foreach(log; string key; array(int) times) { +  if (!has_prefix(key, prefix)) continue; +  int sz = sizeof(times); +  times = filter(times, `>=, cutoff); +  if (sizeof(times) == sz) continue; +  // NB: There's a race here, where newly triggered errors may be lost. +  // It's very unlikely to be a problem in practice though. +  if (!sizeof(times)) { +  m_delete(log, key); +  } else { +  log[key] = times; +  } +  } +  } + } +  + protected void error_log_cleaner() + { +  mapping(string:int) cutoffs = ([ +  "1,": time(1) - 3600*24*7, // Keep notices for 7 days. +  ]); +  +  // First the global error_log. +  clean_error_log(error_log, cutoffs); +  +  // Then all configurations and modules. +  foreach(configurations, Configuration conf) { +  clean_error_log(conf->error_log, cutoffs); +  +  foreach(indices(conf->otomod), RoxenModule mod) { +  clean_error_log(mod->error_log, cutoffs); +  } +  } + } +  + protected void start_error_log_cleaner() + { +  if (error_log_cleaner_process) return; +  +  // Clean the error log once every hour. +  error_log_cleaner_process = BackgroundProcess(3600, error_log_cleaner); + } +  + protected void stop_error_log_cleaner() + { +  if (error_log_cleaner_process) { +  error_log_cleaner_process->stop(); +  error_log_cleaner_process = UNDEFINED; +  } + } +    // When was Roxen started?   int boot_time =time();   int start_time =time();
6026:   #endif   #endif /* THREADS */    +  start_error_log_cleaner(); +    #ifdef TEST_EUID_CHANGE    if (test_euid_change) {    Stdio.File f = Stdio.File();