- Timestamp:
- Jan 26, 2020, 1:36:36 PM (12 months ago)
- Branches:
- asyncio, master, proxy-ticket
- Children:
- 422eade
- Parents:
- e2200db
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/mgstest/services.py
re2200db r264ab17 28 28 """A generic service used in the mod_gnutls test environment.""" 29 29 30 def __init__(self, start=None, stop=None, forking=False,31 env=None,condition=None, check=None, pidfile=None):30 def __init__(self, start=None, stop=None, env=None, 31 condition=None, check=None, pidfile=None): 32 32 # command to start the service 33 33 self.start_command = start 34 34 # command to stop the service (otherwise use SIGTERM) 35 35 self.stop_command = stop 36 # forking: expect the start command to terminate during startup37 self.forking = forking38 36 # condition: start service if the function returns true 39 37 self.condition = condition or (lambda: True) 40 38 41 # child process for non-forking services39 # child process 42 40 self.process = None 43 # Forking processes like apache2 require a PID file for44 # tracking. The process must delete its PID file whenexiting.41 # PID file, if any. The process must delete its PID file when 42 # exiting. 45 43 self.pidfile = Path(pidfile) if pidfile else None 46 if not self.pidfile and self.forking:47 raise ValueError('Forking service must provide PID file!')48 self.pid = None49 44 50 45 # add environment variables for a subprocess only … … 68 63 return 69 64 print(f'Starting: {self.start_command}') 70 if self.forking: 71 subprocess.run(self.start_command, check=True, 72 env=self.process_env) 73 else: 74 self.process = subprocess.Popen(self.start_command, 75 env=self.process_env, 76 close_fds=True) 65 self.process = subprocess.Popen(self.start_command, 66 env=self.process_env, 67 close_fds=True) 77 68 78 69 def stop(self): … … 81 72 # skip 82 73 return 83 # Read PID file before actually sending the stop signal 84 if self.pidfile: 85 if not self.pidfile.exists(): 86 print(f'Skipping stop of {self.stop_command}, no PID file!') 87 # Presumably the process isn't running, ignore. 88 return 89 self.pid = int(self.pidfile.read_text()) 74 if not self.process or self.process.poll(): 75 # process either never started or already stopped 76 return 77 90 78 if self.stop_command: 91 79 print(f'Stopping: {self.stop_command}') … … 93 81 else: 94 82 print(f'Stopping (SIGTERM): {self.start_command}') 95 if self.process: 96 # non-forking process: direct SIGTERM to child process 97 self.process.terminate() 98 else: 99 # forking process: SIGTERM based on PID file 100 os.kill(self.pid, signal.SIGTERM) 83 self.process.terminate() 101 84 102 85 def wait(self): … … 109 92 self.process.wait() 110 93 self.process = None 111 elif self.pid and self.pidfile:112 print(f'Waiting for PID {self.pid} to delete its PID file '113 f'({self.pidfile})...', file=sys.stderr)114 sys.stderr.flush()115 while True:116 if self.pidfile.exists():117 sleep(self._step)118 else:119 break120 self.pid = None121 94 122 95 def wait_ready(self, timeout=None): … … 188 161 super(ApacheService, self).__init__(start=start_cmd, 189 162 stop=base_cmd + ['stop'], 190 forking=False,191 163 env=env, 192 164 pidfile=pidfile,
Note: See TracChangeset
for help on using the changeset viewer.