Files
patroni/patroni/postgresql/callback_executor.py
T
Alexander KukushkinandGitHub a4bd6a9b4b Refactor postgresql class (#1060)
* Convert postgresql.py into a package
* Factor out cancellable process into a separate class
* Factor out connection handler into a separate class
* Move postmaster into postgresql package
* Factor out pg_rewind into a separate class
* Factor out bootstrap into a separate class
* Factor out slots handler into a separate class
* Factor out postgresql config handler into a separate class
* Move callback_executor into postgresql package

This is just a careful refactoring, without code changes.
2019-05-21 16:02:47 +02:00

41 lines
1.2 KiB
Python

import logging
import subprocess
from threading import Event, Lock, Thread
logger = logging.getLogger(__name__)
class CallbackExecutor(Thread):
def __init__(self):
super(CallbackExecutor, self).__init__()
self.daemon = True
self._lock = Lock()
self._cmd = None
self._process = None
self._callback_event = Event()
self.start()
def call(self, cmd):
with self._lock:
if self._process and self._process.poll() is None:
try:
self._process.kill()
logger.warning('Killed the old callback process because it was still running: %s', self._cmd)
except OSError:
logger.exception('Failed to kill the old callback')
self._cmd = cmd
self._callback_event.set()
def run(self):
while True:
self._callback_event.wait()
self._callback_event.clear()
with self._lock:
try:
self._process = subprocess.Popen(self._cmd, close_fds=True)
except Exception:
logger.exception('Failed to execute %s', self._cmd)
continue
self._process.wait()