def start(self, globalVars=None): if not self.component or not self.component.host: raise AttributeError('Host is not set for %s' % str(self)) if not self.isAlive(): self.clearScreenReaders() if not self.canStart(): self.log.info('Cannot start Action "%s", no commands found' % (self.name)) return self.log.info('Starting Action "%s". "%d" Commands / "%d" dependencies found' % (self.name, len(self._startCommands), len(self.dependencies))) if len(self.dependencies): self.log.info('Action depends on: %s' % list('%s>%s' % (item.component.getName(), item.name) for item in self.dependencies)) for dep in self.dependencies: dep.start() for cmd in self._startCommands.values(): channel = self.component.host.invokeShell() command = self.createScreenCmd( cmd, globalVars ) hideLog = cmd.hideLog self.startChannels.append(channel) screen_name = self.name + '_run' screenReader = ScreenReader( screen_name, channel, self.log, notifyStop=self.screenReaderStopped) self.screenReaders['hide' if hideLog else 'show'].append(screenReader) screenReader.start() self.log.debug('Running start command "%s" by Action "%s"' % (command.replace('\n','\\n'), self.name)) channel.send(command) self.constantlyCheckState( globalVars ) # if blocking is enabled for this command, wait for the screenreader to quit if cmd.blocking and screenReader and screenReader.isAlive(): self.log.debug('Command requires blocking. Action "%s" joined the screenReader Thread. Waiting for it to finish' % self.name) screenReader.join( 5.0 ) # notify EventHandler after all cmds run if self.isAlive(): EventHistory.actionRun(self, self.component) return True else: self.log.debug('Could not start action "%s", Action still active' % self.name) return False
def stop(self, force=False, globalVars=None ): if not self.component or not self.component.host: raise AttributeError('Host is not set for %s' % str(self)) if not self.isAlive() and not force: self.log.debug('Action "%s"::stop skipped because component is not alive') return False self.killStateChannel() if not self.canStop(): self.log.info('Cannot stop Action "%s" (force=%s), no commands found' % (self.name, str(force))) return self.log.info('Stopping Action "%s" (force=%s). "%d" Commands found' % (self.name, str(force), len(self._stopCommands))) for cmd in self._stopCommands.values(): channel = self.component.host.invokeShell() command = self.createScreenCmd( cmd, globalVars ) hideLog = cmd.hideLog self.log.debug('Running stop command "%s" by Action "%s"' % (command.replace('\n','\\n'), self.name)) screen_name = self.name + '_stop' screenReader = ScreenReader( screen_name, channel, self.log, notifyStop=self.screenReaderStopped) self.screenReaders['hide' if hideLog else 'show'].append(screenReader) screenReader.start() channel.send(command) # if blocking is enabled for this command, wait for the screenreader to quit if cmd.blocking and screenReader and screenReader.isAlive(): self.log.debug('Command requires blocking. Action "%s" joined the screenReader Thread. Waiting for it to finish' % self.name) screenReader.join( 5.0 ) # Wait for all readers (start and stop) to finish for reader in self.screenReaders['hide']+self.screenReaders['show']: if reader and reader.isAlive(): self.log.debug('Action "%s"::stop joined the screenReader Thread. Waiting for it to finish' % self.name) reader.join( 5.0 ) return True