Esempio n. 1
0
 def handle_interrupt(interrupt_code, _):
     logging.info(
         'Executor interrupted with code {}'.format(interrupt_code))
     cio.print_and_log(
         'Received kill for task {} with grace period of {}'.format(
             executor_id, config.shutdown_grace_period))
     stop_signal.set()
     non_zero_exit_signal.set()
     cu.print_memory_usage()
Esempio n. 2
0
def os_error_handler(stop_signal, status_updater, os_error):
    """Exception handler for OSError.

    Parameters
    ----------
    stop_signal: threading.Event
        Event that determines if the process was requested to terminate.
    status_updater: StatusUpdater
        Wrapper object that sends task status messages.
    os_error: OSError
        The current executor config.

    Returns
    -------
    Nothing
    """
    stop_signal.set()
    logging.exception('OSError generated, requesting process to terminate')
    reason = cook.REASON_CONTAINER_LIMITATION_MEMORY if cu.is_out_of_memory_error(
        os_error) else None
    status_updater.update_status(cook.TASK_FAILED, reason=reason)
    cu.print_memory_usage()
Esempio n. 3
0
 def print_memory_usage_task():
     cu.print_memory_usage()
     timer = Timer(config.memory_usage_interval_secs,
                   print_memory_usage_task)
     timer.daemon = True
     timer.start()
Esempio n. 4
0
def main(args=None):
    from _version import __version__

    if len(sys.argv) == 2 and sys.argv[1] == "--version":
        print(__version__)
        sys.exit(0)

    cio.print_out('Cook Executor version {}'.format(__version__), flush=True)

    environment = os.environ
    executor_id = environment.get('MESOS_EXECUTOR_ID', '1')
    log_level = environment.get('EXECUTOR_LOG_LEVEL', 'INFO')

    logging.basicConfig(level=log_level,
                        filename='executor.log',
                        format='%(asctime)s %(levelname)s %(message)s')
    logging.info('Starting Cook Executor {} for executor-id={}'.format(
        __version__, executor_id))
    logging.info('Log level is {}'.format(log_level))

    config = cc.initialize_config(environment)

    def print_memory_usage_task():
        cu.print_memory_usage()
        timer = Timer(config.memory_usage_interval_secs,
                      print_memory_usage_task)
        timer.daemon = True
        timer.start()

    print_memory_usage_task()

    stop_signal = Event()
    non_zero_exit_signal = Event()

    def handle_interrupt(interrupt_code, _):
        logging.info(
            'Executor interrupted with code {}'.format(interrupt_code))
        cio.print_and_log(
            'Received kill for task {} with grace period of {}'.format(
                executor_id, config.shutdown_grace_period))
        stop_signal.set()
        non_zero_exit_signal.set()
        cu.print_memory_usage()

    signal.signal(signal.SIGINT, handle_interrupt)
    signal.signal(signal.SIGTERM, handle_interrupt)

    try:
        executor = ce.CookExecutor(stop_signal, config)
        driver = pm.MesosExecutorDriver(executor)

        logging.info('MesosExecutorDriver is starting...')
        driver.start()

        executor.await_completion()

        logging.info('MesosExecutorDriver requested to stop')
        driver.stop()
        logging.info('MesosExecutorDriver has been stopped')

        executor.await_disconnect()
    except Exception:
        logging.exception('Error in __main__')
        stop_signal.set()
        non_zero_exit_signal.set()

    cu.print_memory_usage()
    exit_code = 1 if non_zero_exit_signal.isSet() else 0
    logging.info('Executor exiting with code {}'.format(exit_code))
    sys.exit(exit_code)