#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# pylint: disable=cmk-module-layer-violation

from logging import getLogger

from cmk.gui import main_modules
from cmk.gui.utils.script_helpers import gui_context
from cmk.gui.watolib.search import build_index_background


def main():
    logger = getLogger("init-redis")
    try:
        main_modules.load_plugins()
        with gui_context():
            # the redis unix socket might not yet be open when the background process is launched, therefore, we allow
            # for several attempts (sleep up to 6 * 5s = 30s at most)
            build_index_background(
                n_attempts_redis_connection=6,
                sleep_time=5,
            )
    except Exception as excpt:
        logger.error("ERROR: Failed to launch building of search index")
        logger.error(excpt)


if __name__ == "__main__":
    main()
