entrypoint.py 3.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/python3

# Copyright (c) 2016, Daniele Venzano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

18 19
"""Zoe Master main entrypoint."""

20
import logging
21
import os
22

23
import zoe_lib.config as config
24
from zoe_lib.metrics.influxdb import InfluxDBMetricSender
25
from zoe_lib.metrics.logging import LogMetricSender
26
from zoe_lib.state import SQLManager
Daniele Venzano's avatar
Daniele Venzano committed
27 28 29

import zoe_master.scheduler
import zoe_master.backends.interface
30
from zoe_master.preprocessing import restart_resubmit_scheduler
31
from zoe_master.master_api import APIManager
32
from zoe_master.exceptions import ZoeException
33
from zoe_master.gelf_listener import GELFListener
34 35

log = logging.getLogger("main")
Daniele Venzano's avatar
Daniele Venzano committed
36
LOG_FORMAT = '%(asctime)-15s %(levelname)s %(threadName)s->%(name)s: %(message)s'
37 38


39 40 41 42 43 44 45
def _check_configuration_sanity():
    if not os.path.exists(os.path.join(config.get_conf().workspace_base_path, config.get_conf().workspace_deployment_path)):
        log.error('Workspace base directory does not exist: {}'.format(os.path.join(config.get_conf().workspace_base_path, config.get_conf().workspace_deployment_path)))
        return 1
    return 0


46 47
def main():
    """
48
    The entrypoint for the zoe-master script.
49 50
    :return: int
    """
51 52
    config.load_configuration()
    args = config.get_conf()
53 54 55 56 57 58
    log_args = {
        'level': logging.DEBUG if args.debug else logging.INFO,
        'format': LOG_FORMAT
    }
    if args.log_file != "stderr":
        log_args['filename'] = args.log_file
59
    logging.basicConfig(**log_args)
60

61 62 63 64
    ret = _check_configuration_sanity()
    if ret != 0:
        return ret

65
    if config.get_conf().influxdb_enable:
66
        metrics = InfluxDBMetricSender(config.get_conf().deployment_name, config.get_conf().influxdb_url, config.get_conf().influxdb_dbname)
67
    else:
68
        metrics = LogMetricSender(config.get_conf().deployment_name)
69

70 71
    log.info("Initializing DB manager")
    state = SQLManager(args)
72

73 74 75 76 77 78
    try:
        zoe_master.backends.interface.initialize_backend(state)
    except ZoeException as e:
        log.error('Cannot initialize backend: {}'.format(e.message))
        return 1

79
    log.info("Initializing scheduler")
80
    scheduler = getattr(zoe_master.scheduler, config.get_conf().scheduler_class)(state, config.get_conf().scheduler_policy)
81

82
    restart_resubmit_scheduler(state, scheduler)
83

84
    log.info("Starting ZMQ API server...")
85
    api_server = APIManager(metrics, scheduler, state)
86

87 88 89 90 91
    if config.get_conf().gelf_listener != 0:
        gelf_listener = GELFListener()
    else:
        gelf_listener = None

92
    try:
93
        api_server.loop()
94 95 96 97 98
    except KeyboardInterrupt:
        pass
    except Exception:
        log.exception('fatal error')
    finally:
99 100
        scheduler.quit()
        api_server.quit()
101
        zoe_master.backends.interface.shutdown_backend()
102
        metrics.quit()
103 104
        if gelf_listener is not None:
            gelf_listener.quit()