/
daemon_command.py
70 lines (60 loc) · 3.03 KB
/
daemon_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from optparse import make_option
import signal
from django.core.management.base import NoArgsCommand
from initd import Initd
class DaemonCommand(NoArgsCommand):
"""
Run a management command as a daemon.
Subclass this and override the `loop_callback` method with the code the
daemon process should run. Optionally, override `exit_callback` with
code to run when the process is stopped.
Alternatively, if your code has more complex setup/shutdown requirements,
override `handle_noargs` along the lines of the basic version here.
Pass one of --start, --stop, --restart or --status to work as a daemon.
Otherwise, the command will run as a standard application.
"""
requires_model_validation = True
WORKDIR = '.'
UMASK = 0
PID_FILE = 'daemon_command.pid'
LOGFILE = 'daemon_command.log'
STDOUT = '/dev/null'
STDERR = STDOUT
option_list = NoArgsCommand.option_list + (
make_option('--start', action='store_const', const='start', dest='action',
help='Start the daemon'),
make_option('--stop', action='store_const', const='stop', dest='action',
help='Stop the daemon'),
make_option('--restart', action='store_const', const='restart', dest='action',
help='Stop and restart the daemon'),
make_option('--status', action='store_const', const='status', dest='action',
help='Report whether the daemon is currently running or stopped'),
make_option('--workdir', action='store', dest='workdir', default=WORKDIR,
help='Full path of the working directory to which the process should '
'change on daemon start.'),
make_option('--umask', action='store', dest='umask', default=UMASK, type="int",
help='File access creation mask ("umask") to set for the process on '
'daemon start.'),
make_option('--pidfile', action='store', dest='pid_file',
default=PID_FILE, help='PID filename.'),
make_option('--logfile', action='store', dest='log_file',
default=LOGFILE, help='Path to log file'),
make_option('--stdout', action='store', dest='stdout', default=STDOUT,
help='Destination to redirect standard out'),
make_option('--stderr', action='store', dest='stderr', default=STDERR,
help='Destination to redirect standard error'),
)
def loop_callback(self):
raise NotImplementedError
def exit_callback(self):
pass
def handle_noargs(self, **options):
action = options.pop('action', None)
if action:
# daemonizing so set up functions to call while running and at close
daemon = Initd(**options)
daemon.execute(action, run=self.loop_callback, exit=self.exit_callback)
else:
# running in console, so set up signal to call on ctrl-c
signal.signal(signal.SIGINT, lambda sig, frame: self.exit_callback())
self.loop_callback()