Exemplo n.º 1
0
    def _start(self, process):
        try:
            if process in self._process:
                self.logger.debug('process already running', 'process')
                return
            if process not in self._configuration:
                self.logger.debug(
                    'can not start process, no configuration for it',
                    'process')
                return
            # Prevent some weird termcap data to be created at the start of the PIPE
            # \x1b[?1034h (no-eol) (esc)
            os.environ['TERM'] = 'dumb'

            configuration = self._configuration[process]

            run = configuration.get('run', '')
            if run:
                api = configuration.get('encoder', '')
                self._encoder[process] = Response.Text(
                    text_version) if api == 'text' else Response.JSON(
                        json_version)

                self._process[process] = subprocess.Popen(
                    run,
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    preexec_fn=preexec_helper
                    # This flags exists for python 2.7.3 in the documentation but on on my MAC
                    # creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
                )
                self._update_fds()
                fcntl.fcntl(self._process[process].stdout.fileno(),
                            fcntl.F_SETFL, os.O_NONBLOCK)

                self.logger.debug('forked process %s' % process, 'process')

                around_now = int(time.time()) & self.respawn_timemask
                if process in self._respawning:
                    if around_now in self._respawning[process]:
                        self._respawning[process][around_now] += 1
                        # we are respawning too fast
                        if self._respawning[process][
                                around_now] > self.respawn_number:
                            self.logger.critical(
                                'Too many death for %s (%d) terminating program'
                                % (process, self.respawn_number), 'process')
                            raise ProcessError()
                    else:
                        # reset long time since last respawn
                        self._respawning[process] = {around_now: 1}
                else:
                    # record respawing
                    self._respawning[process] = {around_now: 1}

        except (subprocess.CalledProcessError, OSError, ValueError) as exc:
            self._broken.append(process)
            self.logger.debug('could not start process %s' % process,
                              'process')
            self.logger.debug('reason: %s' % str(exc), 'process')
Exemplo n.º 2
0
    def _start(self, process):
        events = self.reactor.configuration.processes[process]
        for event, present in events.iteritems():
            if event in ('run', 'encoder'):
                continue
            if present:
                self._events.setdefault(process, []).append(event)

        try:
            if process in self._process:
                self.logger.processes("process already running")
                return
            if process not in self.reactor.configuration.processes:
                self.logger.processes(
                    "Can not start process, no configuration for it (anymore ?)"
                )
                return

            # Prevent some weird termcap data to be created at the start of the PIPE
            # \x1b[?1034h (no-eol) (esc)
            os.environ['TERM'] = 'dumb'

            run = self.reactor.configuration.processes[process].get('run', '')
            if run:
                api = self.reactor.configuration.processes[process]['encoder']
                self._encoder[process] = Response.Text(
                    text_version) if api == 'text' else Response.JSON(
                        json_version, self.highres)

                self._process[process] = subprocess.Popen(
                    run,
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    preexec_fn=preexec_helper
                    # This flags exists for python 2.7.3 in the documentation but on on my MAC
                    # creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
                )
                fcntl.fcntl(self._process[process].stdout.fileno(),
                            fcntl.F_SETFL, os.O_NONBLOCK)

                self.logger.processes("Forked process %s" % process)

                around_now = int(time.time()) & self.respawn_timemask
                if process in self._respawning:
                    if around_now in self._respawning[process]:
                        self._respawning[process][around_now] += 1
                        # we are respawning too fast
                        if self._respawning[process][
                                around_now] > self.respawn_number:
                            self.logger.processes(
                                "Too many respawn for %s (%d) terminating program"
                                % (process, self.respawn_number), 'critical')
                            raise ProcessError()
                    else:
                        # reset long time since last respawn
                        self._respawning[process] = {around_now: 1}
                else:
                    # record respawing
                    self._respawning[process] = {around_now: 1}

            neighbor = self.reactor.configuration.processes[process][
                'neighbor']
            self._neighbor_process.setdefault(neighbor, []).append(process)
        except (subprocess.CalledProcessError, OSError, ValueError), exc:
            self._broken.append(process)
            self.logger.processes("Could not start process %s" % process)
            self.logger.processes("reason: %s" % str(exc))