forked from danielroseman/django-initd
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdaemon_command.py
More file actions
77 lines (67 loc) · 3.55 KB
/
Copy pathdaemon_command.py
File metadata and controls
77 lines (67 loc) · 3.55 KB
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
71
72
73
74
75
76
77
import signal
from django.core.management.base import BaseCommand
from initd import Initd
class DaemonCommand(BaseCommand):
"""
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
def add_arguments(self, parser):
parser.add_argument('--start', action='store_const', const='start',
dest='action', help='Start the daemon')
parser.add_argument('--stop', action='store_const', const='stop',
dest='action', help='Stop the daemon')
parser.add_argument('--restart', action='store_const', const='restart',
dest='action', help='Stop and restart the daemon')
parser.add_argument('--status', action='store_const', const='status',
dest='action', help='Report whether the daemon is'
' currently running or stopped')
parser.add_argument('--workdir', action='store', dest='workdir',
default=self.WORKDIR, help='Full path of the working'
' directory to which the process should change on'
' daemon start.')
parser.add_argument('--umask', action='store', dest='umask',
default=self.UMASK, type=int,
help='File access creation mask ("umask") to set'
' for the process on daemon start.')
parser.add_argument('--pidfile', action='store', dest='pid_file',
default=self.PID_FILE, help='PID filename.')
parser.add_argument('--logfile', action='store', dest='log_file',
default=self.LOGFILE, help='Path to log file')
parser.add_argument('--stdout', action='store', dest='stdout',
default=self.STDOUT, help='Destination to redirect'
' standard out')
parser.add_argument('--stderr', action='store', dest='stderr',
default=self.STDERR, help='Destination to redirect'
' standard error')
parser.add_argument('--force', action='store_true', dest='force',
default=False, help='SIGKILL process if it won\'t'
' terminate on SIGTERM')
def loop_callback(self):
raise NotImplementedError
def exit_callback(self):
pass
def handle(self, **options):
self.options = 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()