Skip to content

Commit

Permalink
wip - Fix node to avoid losing track of subprocesses
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Jul 7, 2017
1 parent d4b5d2e commit 71cbce9
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion labrad/node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,68 @@
LOG_LENGTH = 1000 # maximum number of lines of stdout to keep per server


class ServerConfig(object):
@classmethod
def from_config(cls, path, filename, conf):
if isinstance(conf, bytes):
conf = conf.decode('utf-8')
scp = ConfigParser()
scp.readfp(io.StringIO(conf))

# general information
name = scp.get('info', 'name', raw=True)
description = scp.get('info', 'description', raw=True)
if scp.has_option('info', 'version'):
version = scp.get('info', 'version', raw=True)
else:
version = '0.0'
try:
instancename = scp.get('info', 'instancename', raw=True)
except:
instancename = name

# startup
platform_cmdline_option = 'cmdline_{}'.format(sys.platform)
if scp.has_option('startup', platform_cmdline_option):
# use platform-specific command line
cmdline = scp.get('startup', platform_cmdline_option, raw=True)
else:
# use generic command line
cmdline = scp.get('startup', 'cmdline', raw=True)
try:
timeout = float(scp.getint('startup', 'timeout'))
except:
timeout = None

# shutdown
if scp.has_option('shutdown', 'message'):
shutdownMode = 'message', int(scp.get('shutdown', 'message', raw=True))
elif scp.has_option('shutdown', 'setting'):
shutdownMode = 'setting', scp.get('shutdown', 'setting', raw=True)
try:
shutdownTimeout = float(scp.getint('shutdown', 'timeout'))
except:
shutdownTimeout = None

return cls(name, description, version, instancename, cmdline, path,
filename, timeout, shutdownMode, shutdownTimeout)

def __init__(self, name, description, version, instancename, cmdline, path,
filename, timeout, shutdownMode, shutdownTimeout):
self.name = name
self.description = description
self.version = version
self.instancename = instancename
self.environVars = findEnvironmentVars(instancename)
self.isLocal = len(self.environVars) > 0
self.cmdline = cmdline
self.path = path
self.filename = filename
self.timeout = timeout
self.shutdownMode = shutdownMode
self.shutdownTimeout = shutdownTimeout


class ServerProcess(ProcessProtocol):
"""A class to represent a running server instance."""

Expand Down Expand Up @@ -772,6 +834,11 @@ def start(self, c, name, environ={}):
"""Start an instance of a server."""
if name not in self.servers:
raise Exception("Unknown server: '%s'." % name)
# check whether an instance with this name already exists
if name in self.instances:
prev = self.instances[name]
if prev.status not in ['STOPPING', 'STOPPED']:
raise ValueError('server already running')
environ = dict(environ) # copy context environment
environ.update(LABRADNODE=self.nodename,
LABRADHOST=self.host,
Expand All @@ -781,7 +848,6 @@ def start(self, c, name, environ={}):
environ.update(LABRADUSER=self.credential.username,
LABRADPASSWORD=self.credential.password)
srv = self.servers[name](environ)
# TODO check whether an instance with this name already exists
self.instances[name] = srv
yield srv.start()
returnValue(srv.name)
Expand Down

0 comments on commit 71cbce9

Please sign in to comment.