import logging
import os
import psutil
+import signal
import subprocess
import sys
import yaml
from teuthology import safepath
log = logging.getLogger(__name__)
-start_time = datetime.datetime.now(datetime.timezone.utc)
-restart_file_path = '/tmp/teuthology-restart-dispatcher'
-stop_file_path = '/tmp/teuthology-stop-dispatcher'
-
-def sentinel(path):
- if not os.path.exists(path):
- return False
- file_mtime = datetime.datetime.fromtimestamp(
- os.path.getmtime(path),
- datetime.timezone.utc,
- )
- return file_mtime > start_time
-
-
-def restart(log=log):
- log.info('Restarting...')
- args = sys.argv[:]
- args.insert(0, sys.executable)
- os.execv(sys.executable, args)
-
-
-def stop():
+def stop(_sig, _frame) -> None:
log.info('Stopping...')
sys.exit(0)
def main(args):
+ signal.signal(signal.SIGTERM, stop)
archive_dir = args.archive_dir or teuth_config.archive_base
# Refuse to start more than one dispatcher per machine type
while keep_running:
try:
- if sentinel(restart_file_path):
- restart()
- elif sentinel(stop_file_path):
- stop()
-
load_config()
for proc in list(job_procs):
rc = proc.poll()
self.ctx.log_dir = str(tmp_path / "log/dir")
self.ctx.tube = 'tube'
- @patch("os.path.exists")
- def test_restart_file_path_doesnt_exist(self, m_exists):
- m_exists.return_value = False
- result = dispatcher.sentinel(dispatcher.restart_file_path)
- assert not result
-
- @patch("os.path.getmtime")
- @patch("os.path.exists")
- def test_needs_restart(self, m_exists, m_getmtime):
- m_exists.return_value = True
- now = datetime.datetime.now(datetime.timezone.utc)
- m_getmtime.return_value = (now + datetime.timedelta(days=1)).timestamp()
- assert dispatcher.sentinel(dispatcher.restart_file_path)
-
- @patch("os.path.getmtime")
- @patch("os.path.exists")
- def test_does_not_need_restart(self, m_exists, m_getmtime):
- m_exists.return_value = True
- now = datetime.datetime.now(datetime.timezone.utc)
- m_getmtime.return_value = (now - datetime.timedelta(days=1)).timestamp()
- assert not dispatcher.sentinel(dispatcher.restart_file_path)
-
@patch("teuthology.repo_utils.ls_remote")
@patch("os.path.isdir")
@patch("teuthology.repo_utils.fetch_teuthology")