blob: 827bbf2d13662dc68f6707f25887fc6d237e58a9 [file] [log] [blame]
Joshua Hesketh0ddd6382013-07-26 10:33:36 +10001#!/usr/bin/python2
2#
3# Copyright 2013 ...
4
Joshua Hesketh1377f6f2013-07-26 12:02:15 +10005
6""" worker_server.py is an executable worker server that loads and runs
7task_plugins. """
8
Joshua Hesketh0ddd6382013-07-26 10:33:36 +10009import argparse
10import daemon
11import extras
12import imp
13import json
14import logging
15import os
16import signal
17import sys
18
19import worker_manager
20
21# as of python-daemon 1.6 it doesn't bundle pidlockfile anymore
22# instead it depends on lockfile-0.9.1 which uses pidfile.
Joshua Hesketh1377f6f2013-07-26 12:02:15 +100023PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile'])
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100024
25
26class Server(object):
27
28 """ This is the worker server object to be daemonized """
Joshua Hesketh363d0042013-07-26 11:44:07 +100029 log = logging.getLogger("worker_server.Server")
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100030
31 def __init__(self, config):
32 # Config init
33 self.config = config
34 self.manager = None
35 self.plugins = []
36 self.load_plugins()
37
38 # Python logging output file.
39 self.debug_log = self.config['debug_log']
40
41 self.tasks = {}
42
43 def setup_logging(self):
44 if self.debug_log:
45 logging.basicConfig(format='%(asctime)s %(message)s',
46 filename=self.debug_log, level=logging.DEBUG)
47 else:
48 logging.basicConfig(format='%(asctime)s %(message)s',
49 level=logging.WARN)
50 self.log.debug('Log pusher starting.')
51
52 def load_plugins(self):
53 """ Load the available plugins from task_plugins """
54 # Load plugins
55 for ent in os.listdir('task_plugins'):
56 if (os.path.isdir('task_plugins/' + ent)
57 and os.path.isfile('task_plugins/' + ent + '/task.py')):
58 plugin_info = imp.find_module('task', ['task_plugins/' + ent])
59 self.plugins.append(imp.load_module('task', *plugin_info))
60
61 def run_tasks(self):
62 """ Run the tasks """
63 for plugin in self.plugins:
64 self.tasks[plugin.__worker_name__] = plugin.Runner(self.config)
65 self.tasks[plugin.__worker_name__].daemon = True
66 self.tasks[plugin.__worker_name__].start()
67
68 self.manager = worker_manager.GearmanManager(self.config, self.tasks)
69 self.manager.daemon = True
70 self.manager.start()
71
72 def exit_handler(self, signum):
73 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
74 for task_name, task in self.tasks.items():
75 task.stop()
76 self.manager.stop()
77 sys.exit(0)
78
79 def main(self):
80 self.setup_logging()
81 self.run_tasks()
82
83 while True:
84 try:
85 signal.pause()
86 except KeyboardInterrupt:
87 print "Ctrl + C: asking tasks to exit nicely...\n"
88 self.exit_handler(signal.SIGINT)
89
90
91def main():
92 parser = argparse.ArgumentParser()
93 parser.add_argument('-c', '--config',
94 default=
Joshua Hesketh363d0042013-07-26 11:44:07 +100095 '/etc/turbo-hipster/config.json',
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100096 help='Path to json config file.')
Joshua Hesketh478f1512013-07-26 11:47:27 +100097 parser.add_argument('--background', action='store_true',
98 help='Run in the background.')
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100099 parser.add_argument('-p', '--pidfile',
Joshua Hesketh363d0042013-07-26 11:44:07 +1000100 default='/var/run/turbo-hipster/'
Joshua Hesketh0ddd6382013-07-26 10:33:36 +1000101 'sql-migrate-gearman-worker.pid',
102 help='PID file to lock during daemonization.')
103 args = parser.parse_args()
104
105 with open(args.config, 'r') as config_stream:
106 config = json.load(config_stream)
107
108 server = Server(config)
109
Joshua Hesketh478f1512013-07-26 11:47:27 +1000110 if args.background:
Joshua Hesketh1377f6f2013-07-26 12:02:15 +1000111 pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10)
Joshua Hesketh0ddd6382013-07-26 10:33:36 +1000112 with daemon.DaemonContext(pidfile=pidfile):
113 server.main()
Joshua Hesketh478f1512013-07-26 11:47:27 +1000114 else:
115 server.main()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +1000116
117
118if __name__ == '__main__':
119 main()