def on_ready_success(proto): worker.pid = proto.transport.pid worker.status = 'started' worker.started = datetime.utcnow() self.log.info("{worker} with ID '{id}' and PID {pid} started", worker=worker_logname, id=worker.id, pid=worker.pid) self._node._reactor.addSystemEventTrigger( 'before', 'shutdown', self._cleanup_worker, self._node._reactor, worker, ) # directory watcher # if 'watch' in options: if HAS_FSNOTIFY: # assemble list of watched directories watched_dirs = [] for d in options['watch'].get('directories', []): watched_dirs.append( os.path.abspath(os.path.join(self._node._cbdir, d))) worker.watch_timeout = options['watch'].get('timeout', 1) # create a directory watcher worker.watcher = DirWatcher(dirs=watched_dirs, notify_once=True) # make sure to stop the background thread running inside the # watcher upon Twisted being shut down def on_shutdown(): worker.watcher.stop() self._node._reactor.addSystemEventTrigger( 'before', 'shutdown', on_shutdown) # this handler will get fired by the watcher upon detecting an FS event def on_fsevent(evt): worker.watcher.stop() proto.signal('TERM') if options['watch'].get('action', None) == 'restart': self.log.info("Restarting guest ..") # Add a timeout large enough (perhaps add a config option later) self._node._reactor.callLater( worker.watch_timeout, self.start_guest, id, config, details) # Shut the worker down, after the restart event is scheduled worker.stop() # now run the watcher on a background thread deferToThread(worker.watcher.loop, on_fsevent) else: self.log.warn( "Warning: cannot watch directory for changes - feature DirWatcher unavailable" ) # assemble guest worker startup information # started_info = { 'id': worker.id, 'status': worker.status, 'started': utcstr(worker.started), 'who': worker.who } self.publish(started_topic, started_info, options=PublishOptions(exclude=[details.caller])) return started_info
def on_ready_success(proto): worker.pid = proto.transport.pid worker.status = 'started' worker.started = datetime.utcnow() log.msg("{} with ID '{}' and PID {} started".format( worker_logname, worker.id, worker.pid)) ## directory watcher ## if 'watch' in options: if HAS_FSNOTIFY: ## assemble list of watched directories watched_dirs = [] for d in options['watch'].get('directories', []): watched_dirs.append( os.path.abspath(os.path.join(self._node._cbdir, d))) ## create a directory watcher worker.watcher = DirWatcher(dirs=watched_dirs, notify_once=True) ## make sure to stop the background thread running inside the ## watcher upon Twisted being shut down def on_shutdown(): worker.watcher.stop() reactor.addSystemEventTrigger('before', 'shutdown', on_shutdown) ## this handler will get fired by the watcher upon detecting an FS event def on_fsevent(evt): worker.watcher.stop() proto.signal('TERM') if options['watch'].get('action', None) == 'restart': log.msg("Restarting guest ..") reactor.callLater(0.1, self.start_guest, id, config, details) ## now run the watcher on a background thread deferToThread(worker.watcher.loop, on_fsevent) else: log.msg( "Warning: cannot watch directory for changes - feature DirWatcher unavailable" ) ## assemble guest worker startup information ## started_info = { 'id': worker.id, 'status': worker.status, 'started': utcstr(worker.started), 'who': worker.who } self.publish(started_topic, started_info, options=PublishOptions(exclude=[details.caller])) return started_info