Skip to content

Commit

Permalink
Use python-pidfile for cross-platform compat
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Mar 29, 2018
1 parent d355ea5 commit 6f3348e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'json-object-factory',
'zeroconf',
'python-osc',
'pid',
'python-pidfile',
],
entry_points={
'console_scripts':[
Expand Down
13 changes: 13 additions & 0 deletions tests/test_runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

import vidhubcontrol
from vidhubcontrol.runserver import PID_FILENAME
from vidhubcontrol.utils import find_ip_addresses
from vidhubcontrol.config import Config
from vidhubcontrol.backends import DummyBackend
Expand Down Expand Up @@ -57,11 +58,17 @@ async def test_runserver(tempconfig, mocked_vidhub_telnet_device, runserver_scri

while True:
line = await proc.stdout.readline()
if not len(line):
raise Exception('process terminated')
print(repr(line))
if b'Ready' in line:
break
print('subprocess ready')

with open(PID_FILENAME, 'r') as pid_fh:
pid_str = pid_fh.read()
assert int(pid_str) == int(proc.pid)

client_node = OscNode('vidhubcontrol')
client_dispatcher = OscDispatcher()
client_node.osc_dispatcher = client_dispatcher
Expand Down Expand Up @@ -132,5 +139,11 @@ def on_node_msg(node, client_addr, *messages):
print('shutting down subprocess')
proc.send_signal(signal.SIGINT)
err = await proc.wait()
if err != 0:
while True:
line = await proc.stdout.readline()
if not len(line):
break
print(repr(line))

assert err == 0
45 changes: 42 additions & 3 deletions vidhubcontrol/runserver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
#! /usr/bin/env python

import os
import sys
import tempfile
import signal
import asyncio
import argparse
import logging

from pid import PidFile
from pidfile import PIDFile

if sys.platform == 'win32':
PIDPATH = os.path.join(os.env['APPDATA'], 'vidhubcontrol')
if not os.path.exists(PIDPATH):
os.makedirs(PIDPATH)
else:
PIDPATH = tempfile.gettempdir()
PID_FILENAME = os.path.join(PIDPATH, 'vidhubcontrol-server.pid')

class PIDFileWrapper(PIDFile):
@property
def filename(self):
return self._PIDFile__file
@property
def checked(self):
return self._PIDFile__checked
@checked.setter
def checked(self, value):
self._PIDFile__checked = value
def __enter__(self):
super().__enter__()
self.checked = True
return self
def __exit__(self, *args):
super().__exit__(*args)
if self.checked and os.path.exists(self.filename):
os.unlink(self.filename)


logging.basicConfig(
level=logging.INFO,
Expand Down Expand Up @@ -75,11 +105,20 @@ def on_sigint(config, interfaces):
asyncio.ensure_future(stop(config, interfaces))

def main():
with PidFile(pidname='vidhubcontrolserver.pid', force_tmpdir=True) as pf:
with PIDFileWrapper(PID_FILENAME):
opts = parse_args()
loop = asyncio.get_event_loop()
logger.info('Running server. Press CTRL+c to exit')
loop.run_until_complete(run(loop, opts))

if __name__ == '__main__':
main()
try:
main()
sys.exit(0)
except RuntimeError as e:
if 'program already running' in str(e).lower():
print('vidhub-control server already running')
# EX_OSERR 71 /* system error (e.g., can't fork) */
sys.exit(71)
else:
raise

0 comments on commit 6f3348e

Please sign in to comment.