Files
seiscomp-training/etc/init/caps.py

70 lines
1.9 KiB
Python

# SeisComP init script for CAPS
# Code is required to be Python 2 and 3 compatible
# pylint: disable=consider-using-f-string, unspecified-encoding
import os
import subprocess
import time
import seiscomp.kernel
class Module(seiscomp.kernel.Module):
def __init__(self, env):
seiscomp.kernel.Module.__init__(self, env, env.moduleName(__file__))
self.killTimeout = 60
self.reloadTimeout = 60
def updateConfigProxy(self):
return "trunk"
def updateConfig(self):
# By default the "trunk" module must be configured to write the
# bindings into the database
return 0
def supportsAliases(self):
# The default handler does not support aliases
return True
def reload(self):
if not self.isRunning():
self.env.log("{} is not running".format(self.name))
return 1
self.env.log("reloading {}".format(self.name))
lockfile = self.env.lockFile(self.name)
reloadfile = os.path.join(
os.path.dirname(lockfile), "{}.reload".format(self.name)
)
# Open pid file
with open(lockfile, "r") as f:
# Try to read the pid
pid = int(f.readline())
# Create empty reload file to be removed by CAPS server after
# reload is complete.
try:
with open(reloadfile, "a"):
pass
except OSError as e:
self.env.log(
"could not create reload file {}: {}".format(reloadfile, e)
)
return 1
# Send SIGHUP
subprocess.call("kill -s USR1 %d" % pid, shell=True)
# wait for reload file to disappear
for _ in range(0, int(self.reloadTimeout * 5)):
time.sleep(0.2)
if not os.path.isfile(reloadfile):
return 0
self.env.log("timeout exceeded")
return 1