Added mandatory restart delay.
[nssm.git] / service.cpp
index 93b7d4a..c3a5008 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
@@ -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