Пример #1
0
def run(reactor, command, **kwargs):
    """
    Run a process and kill it if the reactor stops.

    :param reactor: Reactor to use.
    :param list command: The command to run.

    :return Deferred: Deferred that fires when the process is ended.
    """
    if 'env' not in kwargs:
        kwargs['env'] = os.environ

    action = RUN_ACTION(command=command)

    protocol_done = Deferred()
    protocol = CommandProtocol(deferred=protocol_done, action=action)

    with action.context():
        protocol_done = DeferredContext(protocol_done)
        reactor.spawnProcess(protocol, command[0], command, **kwargs)

        def unregister_killer(result, trigger_id):
            try:
                reactor.removeSystemEventTrigger(trigger_id)
            except:
                # If we can't remove the trigger, presumably it has already
                # been removed (or run). In any case, there is nothing sensible
                # to do if this fails.
                pass
            return result
        trigger_id = reactor.addSystemEventTrigger(
            'before', 'shutdown', protocol.transport.signalProcess, 'TERM')
        protocol_done.addBoth(unregister_killer, trigger_id)

        return protocol_done.addActionFinish()
Пример #2
0
def run(reactor, command, handle_stdout=None, handle_stderr=None, **kwargs):
    """
    Run a process and kill it if the reactor stops.

    :param reactor: Reactor to use.
    :param list command: The command to run.
    :param handle_stdout: Callable that will be called with lines parsed
        from the command stdout. By default logs an Eliot message.
    :param handle_stderr: Callable that will be called with lines parsed
        from the command stderr. By default logs an Eliot message.
    :return Deferred: Deferred that fires when the process is ended.
    """
    if 'env' not in kwargs:
        kwargs['env'] = os.environ

    action = RUN_ACTION(command=command)

    if handle_stdout is None:
        def handle_stdout(line):
            RUN_OUTPUT_MESSAGE(
                line=line,
            ).write(action=action)

    if handle_stderr is None:
        def handle_stderr(line):
            RUN_ERROR_MESSAGE(
                line=line,
            ).write(action=action)

    protocol_done = Deferred()
    protocol = CommandProtocol(
        deferred=protocol_done,
        handle_stdout=handle_stdout,
        handle_stderr=handle_stderr,
    )

    with action.context():
        protocol_done = DeferredContext(protocol_done)
        reactor.spawnProcess(protocol, command[0], command, **kwargs)

        def unregister_killer(result, trigger_id):
            try:
                reactor.removeSystemEventTrigger(trigger_id)
            except:
                # If we can't remove the trigger, presumably it has already
                # been removed (or run). In any case, there is nothing sensible
                # to do if this fails.
                pass
            return result
        trigger_id = reactor.addSystemEventTrigger(
            'before', 'shutdown', protocol.transport.signalProcess, 'TERM')
        protocol_done.addBoth(unregister_killer, trigger_id)

        return protocol_done.addActionFinish()
Пример #3
0
def run(reactor, command, handle_stdout=None, handle_stderr=None, **kwargs):
    """
    Run a process and kill it if the reactor stops.

    :param reactor: Reactor to use.
    :param list command: The command to run.
    :param handle_stdout: Callable that will be called with lines parsed
        from the command stdout. By default logs an Eliot message.
    :param handle_stderr: Callable that will be called with lines parsed
        from the command stderr. By default logs an Eliot message.
    :return Deferred: Deferred that fires when the process is ended.
    """
    if 'env' not in kwargs:
        kwargs['env'] = os.environ

    action = RUN_ACTION(command=command)

    if handle_stdout is None:
        def handle_stdout(line):
            RUN_OUTPUT_MESSAGE(
                line=line,
            ).write(action=action)

    if handle_stderr is None:
        def handle_stderr(line):
            RUN_ERROR_MESSAGE(
                line=line,
            ).write(action=action)

    protocol_done = Deferred()
    protocol = CommandProtocol(
        deferred=protocol_done,
        handle_stdout=handle_stdout,
        handle_stderr=handle_stderr,
    )

    with action.context():
        protocol_done = DeferredContext(protocol_done)
        reactor.spawnProcess(protocol, command[0], command, **kwargs)

        def unregister_killer(result, trigger_id):
            try:
                reactor.removeSystemEventTrigger(trigger_id)
            except:
                # If we can't remove the trigger, presumably it has already
                # been removed (or run). In any case, there is nothing sensible
                # to do if this fails.
                pass
            return result
        trigger_id = reactor.addSystemEventTrigger(
            'before', 'shutdown', protocol.transport.signalProcess, 'TERM')
        protocol_done.addBoth(unregister_killer, trigger_id)

        return protocol_done.addActionFinish()