Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks rework #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions daemonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class Daemonize(object):
:param action: your custom function which will be executed after daemonization.
:param keep_fds: optional list of fds which should not be closed.
:param auto_close_fds: optional parameter to not close opened fds.
:param privileged_action: action that will be executed before drop privileges if user or
:param privileged_action: optional action that will be executed before drop privileges if user or
group parameter is provided.
If you want to transfer anything from privileged_action to action, such as
opened privileged file descriptor, you should return it from
privileged_action function and catch it inside action function.
:param sigterm_action: optional action which will be executed once sigterm signal is received by the deamon.
:param user: drop privileges to this user if provided.
:param group: drop privileges to this group if provided.
:param verbose: send debug messages to logger if provided.
Expand All @@ -41,18 +42,19 @@ class Daemonize(object):
"""
def __init__(self, app, pid, action,
keep_fds=None, auto_close_fds=True, privileged_action=None,
user=None, group=None, verbose=False, logger=None,
sigterm_action=None, user=None, group=None, verbose=False, logger=None,
foreground=False, chdir="/"):
self.app = app
self.pid = os.path.abspath(pid)
self.action = action
self.keep_fds = keep_fds or []
self.auto_close_fds = auto_close_fds
self.privileged_action = privileged_action or (lambda: ())
self.sigterm_action = sigterm_action
self.user = user
self.group = group
self.logger = logger
self.verbose = verbose
self.auto_close_fds = auto_close_fds
self.foreground = foreground
self.chdir = chdir

Expand All @@ -61,6 +63,9 @@ def sigterm(self, signum, frame):
These actions will be done after SIGTERM.
"""
self.logger.warn("Caught signal %s. Stopping daemon." % signum)
if self.sigterm_action:
self.logger.debug("Executing daemon's sigterm_action() now")
self.sigterm_action()
sys.exit(0)

def exit(self):
Expand Down Expand Up @@ -243,6 +248,7 @@ def start(self):

try:
self.action(*privileged_action_result)

except Exception:
for line in traceback.format_exc().split("\n"):
self.logger.error(line)
5 changes: 4 additions & 1 deletion tests/daemon_sigterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ def main():
while True:
sleep(5)

daemon = Daemonize(app="test_app", pid=pid, action=main)
def end():
pass

daemon = Daemonize(app="test_app", pid=pid, action=main, sigterm_action=end)
daemon.start()