예제 #1
0
def start_runtime(plugin_location=None):
    if plugin_location is None and (len(sys.argv) < 3
                                    or sys.argv[1] != 'start_plugin'):
        sys.stderr.write('Usage: python {0} start_plugin <path>\n'.format(
            sys.argv[0]))
        sys.stderr.flush()
        sys.exit(1)
    elif not (len(sys.argv) < 3 or sys.argv[1] != 'start_plugin'):
        plugin_location = sys.argv[2]

    def watch_parent():
        parent = os.getppid()
        # If the parent process gets kills, this process will be attached to init.
        # In that case the plugin should stop running.
        while True:
            if os.getppid() != parent:
                os._exit(1)
            time.sleep(1)

    # Keep an eye on our parent process
    watcher = BaseThread(name='pluginwatch', target=watch_parent)
    watcher.daemon = True
    watcher.start()

    # Start the runtime
    try:
        runtime = PluginRuntime(path=plugin_location)
        runtime.process_stdin()
    except BaseException as ex:
        writer = PluginIPCWriter(os.fdopen(sys.stdout.fileno(), 'wb', 0))
        writer.log_exception('__main__', ex)
        os._exit(1)

    os._exit(0)
예제 #2
0
    def _handle_stop(self):
        def delayed_stop():
            time.sleep(2)
            os._exit(0)

        stop_thread = BaseThread(name='pluginstop', target=delayed_stop)
        stop_thread.daemon = True
        stop_thread.start()

        self._stopped = True
예제 #3
0
 def _start_background_tasks(self):
     # type: () -> None
     """ Start all background tasks. """
     for decorated_method in self._decorated_methods['background_task']:
         thread = BaseThread(name='plugin{}'.format(
             decorated_method.__name__),
                             target=self._run_background_task,
                             args=(decorated_method, ))
         thread.daemon = True
         thread.start()
예제 #4
0
    def _start(self):
        def stop(signum, frame):
            """ This function is called on SIGTERM. """
            _ = signum, frame
            self._stop = True
        signal(SIGTERM, stop)

        receiver = BaseThread(name='msgclientrecv', target=self._message_receiver)
        receiver.daemon = True
        receiver.start()
예제 #5
0
    def _process(self):
        now = time.time()
        self.refresh_schedules()  # Bug fix
        pending_schedules = []
        for schedule_id in list(self._schedules.keys()):
            schedule_tuple = self._schedules.get(schedule_id)
            if schedule_tuple is None:
                continue
            schedule_dto, schedule = schedule_tuple
            if schedule_dto.status != 'ACTIVE':
                continue
            if schedule_dto.end is not None and schedule_dto.end < time.time():
                schedule_dto.status = 'COMPLETED'
                schedule.status = 'COMPLETED'
                schedule.save()
                continue
            if schedule_dto.next_execution is not None and schedule_dto.next_execution < now - 60:
                continue
            pending_schedules.append(schedule_dto)
        # Sort the schedules according to their next_execution

        pending_schedules = list(
            sorted(pending_schedules, key=attrgetter('next_execution')))
        if not pending_schedules:
            return
        next_start = pending_schedules[0].next_execution
        schedules_to_execute = [
            schedule for schedule in pending_schedules
            if schedule.next_execution < next_start + 60
        ]
        if not schedules_to_execute:
            return
        # Let this thread hang until it's time to execute the schedule
        logger.debug('next pending schedule %s, waiting %ss',
                     datetime.fromtimestamp(next_start), next_start - now)
        self._event.wait(next_start - now)
        if self._event.isSet(
        ):  # If a new schedule is saved, stop hanging and refresh the schedules
            self._event.clear()
            return
        thread = BaseThread(name='schedulingexc',
                            target=self._execute_schedule,
                            args=(schedules_to_execute, schedule))
        thread.daemon = True
        thread.start()
예제 #6
0
 def _process(self):
     for schedule_id in list(self._schedules.keys()):
         schedule_tuple = self._schedules.get(schedule_id)
         if schedule_tuple is None:
             continue
         schedule_dto, schedule = schedule_tuple
         if schedule_dto.status != 'ACTIVE':
             continue
         if schedule_dto.end is not None and schedule_dto.end < time.time():
             schedule_dto.status = 'COMPLETED'
             schedule.status = 'COMPLETED'
             schedule.save()
             continue
         if schedule_dto.is_due:
             thread = BaseThread(name='schedulingexc',
                                 target=self._execute_schedule, args=(schedule_dto, schedule))
             thread.daemon = True
             thread.start()
예제 #7
0
    def send_passthrough_data(self, data):
        """ Send raw data on the serial port.

        :param data: string of bytes with raw command for the master.
        :raises: :class`InMaintenanceModeException` if master is in maintenance mode.
        """
        if self.__maintenance_mode:
            raise InMaintenanceModeException()

        if not self.__passthrough_mode:
            self.__command_lock.acquire()
            self.__passthrough_done.clear()
            self.__passthrough_mode = True
            passthrough_thread = BaseThread(name='passthroughwait', target=self.__passthrough_wait)
            passthrough_thread.daemon = True
            passthrough_thread.start()

        self.__write_to_serial(data)
예제 #8
0
    if len(sys.argv) < 3 or sys.argv[1] != 'start':
        sys.stderr.write('Usage: python {0} start <path>\n'.format(
            sys.argv[0]))
        sys.stderr.flush()
        sys.exit(1)

    def watch_parent():
        parent = os.getppid()
        # If the parent process gets kills, this process will be attached to init.
        # In that case the plugin should stop running.
        while True:
            if os.getppid() != parent:
                os._exit(1)
            time.sleep(1)

    # Keep an eye on our parent process
    watcher = BaseThread(name='pluginwatch', target=watch_parent)
    watcher.daemon = True
    watcher.start()

    # Start the runtime
    try:
        runtime = PluginRuntime(path=sys.argv[2])
        runtime.process_stdin()
    except BaseException as ex:
        writer = PluginIPCWriter(os.fdopen(sys.stdout.fileno(), 'wb', 0))
        writer.log_exception('__main__', ex)
        os._exit(1)

    os._exit(0)