Rotate files while the service is running.
[nssm.git] / service.cpp
index 93b7d4a..d0141a1 100644 (file)
@@ -162,9 +162,9 @@ unsigned long priority_index_to_constant(int index) {
   return NORMAL_PRIORITY_CLASS;\r
 }\r
 \r
-static inline int throttle_milliseconds(unsigned long throttle) {\r
+static inline unsigned long throttle_milliseconds(unsigned long throttle) {\r
   /* pow() operates on doubles. */\r
-  int ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;\r
+  unsigned long ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;\r
   return ret * 1000;\r
 }\r
 \r
@@ -1431,7 +1431,7 @@ int start_service(nssm_service_t *service) {
 \r
   if (get_process_creation_time(service->process_handle, &service->creation_time)) ZeroMemory(&service->creation_time, sizeof(service->creation_time));\r
 \r
-  close_output_handles(&si);\r
+  close_output_handles(&si, ! service->rotate_stdout_online, ! service->rotate_stderr_online);\r
 \r
   if (service->affinity) {\r
     /*\r
@@ -1490,6 +1490,9 @@ int start_service(nssm_service_t *service) {
     else service->throttle = 0;\r
   }\r
 \r
+  /* Ensure the restart delay is always applied. */\r
+  if (service->restart_delay && ! service->throttle) service->throttle++;\r
+\r
   return 0;\r
 }\r
 \r
@@ -1632,14 +1635,22 @@ void throttle_restart(nssm_service_t *service) {
   /* This can't be a restart if the service is already running. */\r
   if (! service->throttle++) return;\r
 \r
-  int ms = throttle_milliseconds(service->throttle);\r
+  unsigned long ms;\r
+  unsigned long throttle_ms = throttle_milliseconds(service->throttle);\r
+  TCHAR threshold[8], milliseconds[8];\r
+\r
+  if (service->restart_delay > throttle_ms) ms = service->restart_delay;\r
+  else ms = throttle_ms;\r
 \r
   if (service->throttle > 7) service->throttle = 8;\r
 \r
-  TCHAR threshold[8], milliseconds[8];\r
-  _sntprintf_s(threshold, _countof(threshold), _TRUNCATE, _T("%lu"), service->throttle_delay);\r
   _sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), ms);\r
-  log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service->name, threshold, milliseconds, 0);\r
+\r
+  if (service->throttle == 1 && service->restart_delay > throttle_ms) log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESTART_DELAY, service->name, milliseconds, 0);\r
+  else {\r
+    _sntprintf_s(threshold, _countof(threshold), _TRUNCATE, _T("%lu"), service->throttle_delay);\r
+    log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service->name, threshold, milliseconds, 0);\r
+  }\r
 \r
   if (use_critical_section) EnterCriticalSection(&service->throttle_section);\r
   else if (service->throttle_timer) {\r