Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #14 from davidlmorton/devserver
Browse files Browse the repository at this point in the history
Added generic devserver package with run() function
  • Loading branch information
davidlmorton committed Jan 29, 2015
2 parents b15db23 + 4e22fcb commit 5b00431
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions ptero_common/devserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import errno
import os
import sys
import psutil
import signal
import time

honcho_process = None
children = set()


# This is from a stackoverflow answer:
# http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise


def service_command_line(procfile_path, workers):
return ['honcho', 'start', '-f', procfile_path,
'-c', ] + ["%s=%d" % (key, value) for key, value in workers.items()]


def shutdown():
if signal_processes(children, signal.SIGINT):
time.sleep(3)
signal_processes(children, signal.SIGKILL)

def signal_processes(processes, sig):
signaled = []
for p in processes:
try:
p.send_signal(sig)
signaled.append(p.pid)
except psutil.NoSuchProcess:
pass

if len(signaled) > 0:
sys.stderr.write(
"Sent signal (%s) to processes: %s\n" % (sig, signaled))
return True
else:
return False

def expand_children():
for process in children:
try:
children.update(process.get_children(recursive=True))
except psutil.NoSuchProcess:
pass


def cleanup(*args, **kwargs):
expand_children()
try:
honcho_process.send_signal(signal.SIGINT)
except psutil.NoSuchProcess:
return

try:
honcho_process.wait(timeout=3)
except psutil.TimeoutExpired:
pass

shutdown()


def setup_signal_handlers():
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)


def run(logdir, procfile_path, workers):
global honcho_process

setup_signal_handlers()

if (logdir == '-'):
outlog = sys.stdout
errlog = sys.stderr
else:
mkdir_p(logdir)
sys.stderr.write('Starting up devserver... logging to: %s' % logdir)
outlog = open(os.path.join(logdir, 'honcho.out'), 'w')
errlog = open(os.path.join(logdir, 'honcho.err'), 'w')

honcho_process = psutil.Popen(
service_command_line(procfile_path, workers), shell=False,
stdout=outlog, stderr=errlog)
time.sleep(3)
sys.stderr.write('devserver is now up...')
children.update(psutil.Process().get_children(recursive=True))

honcho_process.wait()
cleanup()

0 comments on commit 5b00431

Please sign in to comment.