def module_should_run()

in elkserver/docker/redelk-base/redelkinstalldata/scripts/modules/helpers.py [0:0]


def module_should_run(module_name, module_type):  # pylint: disable=too-many-branches
    """Check if the module is enabled and when is the last time the module ran.
    If the last time is before now - interval, the module will be allowed to run"""
    if module_type == "redelk_alarm":

        if module_name not in config.alarms:
            logger.warning(
                "Missing configuration for alarm [%s]. Will not run!", module_name
            )
            return False

        if (
            "enabled" in config.alarms[module_name]
            and not config.alarms[module_name]["enabled"]
        ):
            logger.warning(
                "Alarm module [%s] disabled in configuration file. Will not run!",
                module_name,
            )
            return False

        if "interval" in config.alarms[module_name]:
            interval = config.alarms[module_name]["interval"]
        else:
            interval = 360

    elif module_type == "redelk_enrich":

        if module_name not in config.enrich:
            logger.warning(
                "Missing configuration for enrichment module [%s]. Will not run!",
                module_name,
            )
            return False

        if (
            "enabled" in config.enrich[module_name]
            and not config.enrich[module_name]["enabled"]
        ):
            logger.warning(
                "Enrichment module [%s] disabled in configuration file. Will not run!",
                module_name,
            )
            return False

        if "interval" in config.enrich[module_name]:
            interval = config.enrich[module_name]["interval"]
        else:
            interval = 360

    else:
        logger.warning(
            "Invalid module type for shouldModuleRun(%s, %s)", module_name, module_type
        )
        return False

    now = datetime.datetime.utcnow()
    last_run = get_last_run(module_name)
    interval = datetime.timedelta(seconds=interval)
    last_run_max = now - interval

    should_run = last_run < last_run_max

    if not should_run:
        logger.info(
            "Module [%s] already ran within the interval of %s seconds (%s)",
            module_name,
            interval,
            last_run.isoformat(),
        )
    else:
        logger.info("All checks ok for module [%s]. Module should run.", module_name)
        logger.debug(
            "Last run: %s | Last run max: %s",
            last_run.isoformat(),
            last_run_max.isoformat(),
        )
    return should_run