aboutsummaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_killer.py
blob: 6b31816087d76670f38adccb09a35ba451390d31 (plain)
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
import signal
import os
import atexit

def setup_killer(server, method = None):
    def default_method():
        server.server_close()

    if method is None:
        method = default_method

    def alarm_handler(signum, frame):
        method()

    signal.signal(signal.SIGALRM, alarm_handler)
    signal.signal(signal.SIGTERM, alarm_handler)
    signal.alarm(120)


def write_pid(path):
    with open(path, 'w+') as f:
        f.write(str(os.getpid()))
        f.close()

    def cleanup():
        os.remove(path)

    atexit.register(cleanup)