blob: fe979daa69330bb4262710f91ff238ad46b7894c [file] [log] [blame]
Matt Hartd4a09982015-07-17 16:13:11 +01001#! /usr/bin/python
2
3# Copyright 2013 Linaro Limited
4# Author Matt Hart <matthew.hart@linaro.org>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19# MA 02110-1301, USA.
20
21import lavapdu.pdurunner as pdurunner
22from multiprocessing import Process
23from setproctitle import setproctitle # pylint: disable=no-name-in-module
24from lavapdu.shared import pdus_from_config
25import signal
26import sys
27import os
28import logging
29processes = []
30log = logging.getLogger(__name__)
31
32
33def start_runner(config, pdu):
34 setproctitle("pdurunner for %s" % pdu)
35 p = pdurunner.PDURunner(config, pdu)
36 p.run_me()
37
38
39def start_em_up(config, pidfile):
40 pid = os.getpid()
41 if os.path.isfile(pidfile):
42 log.error("Pidfile already exists")
43 sys.exit(1)
44 f = open(pidfile, 'w')
45 f.write(str(pid))
46 f.close()
47 pdus = pdus_from_config(config)
48 for pdu in pdus:
49 p = Process(target=start_runner, args=(config, pdu))
50 p.start()
51 processes.append(p)
52 signal.signal(signal.SIGTERM, signal_term_handler)
53
54
55def signal_term_handler(a, b):
56 del a, b
57 print 'Sending sigterm to all children'
58 for proc in processes:
59 log.debug("Terminate %s", proc.pid)
60 proc.terminate()
61 sys.exit(0)