Skip to content

Commit 75e9804

Browse files
author
Joe LeVeque
committed
Add _check_and_adjust_for_system_clock_rollback() method to Subprocess class in order to streamline code a bit
1 parent d514b0b commit 75e9804

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

supervisor/process.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,25 @@ def _spawn_as_child(self, filename, argv):
355355
options.write(2, "supervisor: child process was not spawned\n")
356356
options._exit(127) # exit process with code for spawn failure
357357

358+
def _check_and_adjust_for_system_clock_rollback(self, test_time):
359+
"""
360+
Check if system clock has rolled backward beyond test_time. If so, set
361+
affected timestamps to test_time.
362+
"""
363+
if self.state == ProcessStates.STARTING:
364+
if test_time < self.laststart:
365+
self.laststart = test_time;
366+
if self.delay > 0 and test_time < self.delay - self.config.startsecs:
367+
self.delay = test_time + self.config.startsecs
368+
elif self.state == ProcessStates.STOPPING:
369+
if test_time < self.laststopreport:
370+
self.laststopreport = test_time;
371+
if self.delay > 0 and test_time < self.delay - self.config.stopwaitsecs:
372+
self.delay = test_time + self.config.stopwaitsecs
373+
elif self.state == ProcessStates.BACKOFF:
374+
if self.delay > 0 and test_time < self.delay - self.backoff:
375+
self.delay = test_time + self.backoff
376+
358377
def stop(self):
359378
""" Administrative stop """
360379
self.administrative_stop = True
@@ -366,10 +385,7 @@ def stop_report(self):
366385
if self.state == ProcessStates.STOPPING:
367386
now = time.time()
368387

369-
if now < self.laststopreport:
370-
# The system clock appears to have moved backward
371-
# Reset self.laststopreport to current system time
372-
self.laststopreport = now;
388+
self._check_and_adjust_for_system_clock_rollback(now)
373389

374390
if now > (self.laststopreport + 2): # every 2 seconds
375391
self.config.options.logger.info(
@@ -610,6 +626,8 @@ def transition(self):
610626
now = time.time()
611627
state = self.state
612628

629+
self._check_and_adjust_for_system_clock_rollback(now)
630+
613631
logger = self.config.options.logger
614632

615633
if self.config.options.mood > SupervisorStates.RESTARTING:
@@ -628,28 +646,13 @@ def transition(self):
628646
# STOPPED -> STARTING
629647
self.spawn()
630648
elif state == ProcessStates.BACKOFF:
631-
if self.delay > 0 and now < self.delay - self.backoff:
632-
# The system clock appears to have moved backward
633-
# Reset self.delay accordingly
634-
self.delay = now + self.backoff
635-
636649
if self.backoff <= self.config.startretries:
637650
if now > self.delay:
638651
# BACKOFF -> STARTING
639652
self.spawn()
640653

641654
processname = as_string(self.config.name)
642655
if state == ProcessStates.STARTING:
643-
if now < self.laststart:
644-
# The system clock appears to have moved backward
645-
# Reset self.laststart to current system time
646-
self.laststart = now;
647-
648-
if self.delay > 0 and now < self.delay - self.config.startsecs:
649-
# The system clock appears to have moved backward
650-
# Reset self.delay accordingly
651-
self.delay = now + self.config.startsecs
652-
653656
if now - self.laststart > self.config.startsecs:
654657
# STARTING -> RUNNING if the proc has started
655658
# successfully and it has stayed up for at least
@@ -673,11 +676,6 @@ def transition(self):
673676
logger.info('gave up: %s %s' % (processname, msg))
674677

675678
elif state == ProcessStates.STOPPING:
676-
if self.delay > 0 and now < self.delay - self.config.stopwaitsecs:
677-
# The system clock appears to have moved backward
678-
# Reset self.delay accordingly
679-
self.delay = now + self.config.stopwaitsecs
680-
681679
time_left = self.delay - now
682680
if time_left <= 0:
683681
# kill processes which are taking too long to stop with a final

0 commit comments

Comments
 (0)