-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMosquittoRunner.py
61 lines (49 loc) · 1.43 KB
/
MosquittoRunner.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
#!/bin/python3
import os
import signal
import sys
import threading
import logging
from threading import Timer
class MosquittoRunner:
def __init__(self, binaryPath, port, stderrFile, stdoutFile, startupTimeout = 5):
self.binaryPath = binaryPath
self.port = port
self.pid = None
self.stderrFile = stderrFile
self.stdoutFile = stdoutFile
self.startupTimeout = startupTimeout
self.event = threading.Event()
def startupHandler(self, signum, frame):
self.event.set()
def start(self):
if self.pid != None:
raise Exception("Already running")
signal.signal(signal.SIGTERM, self.startupHandler)
parentPid = os.getpid()
pid = os.fork()
if (pid == 0): # child
fdout = os.open(self.stdoutFile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
fderr = os.open(self.stderrFile, os.O_WRONLY|os.O_CREAT|os.O_TRUNC)
os.dup2(fdout, sys.stdout.fileno())
os.dup2(fderr, sys.stderr.fileno())
args = [self.binaryPath, "-p", str(self.port)]
os.execv(self.binaryPath, args)
else:
self.pid = pid
res = self.event.wait(self.startupTimeout)
self.event.clear()
return res
def stop(self, timeout = 15):
kill = lambda pid: (os.kill(pid, signal.SIGKILL))
if self.pid is None:
raise Exception("Nothing to stop")
killTimer = Timer(timeout, kill, [self.pid])
try:
killTimer.start()
os.kill(self.pid, signal.SIGINT)
res = os.waitpid(self.pid, 0)
return res[1]
finally:
killTimer.cancel()
self.pid = None