public void preStart()

in mantis-jm-akka/src/main/java/io/mantisrx/server/worker/jobmaster/akka/rules/CustomRuleActor.java [53:131]


    public void preStart() throws Exception {
        super.preStart();
        log.info("CustomRuleActor started");
        // initialize custom trigger based on rule configuration
        String customTriggerClassName = rule.getTriggerConfig().getCustomTrigger();
        if (Strings.isNullOrEmpty(customTriggerClassName)) {
            log.error("Custom trigger class name is not set in rule configuration");
            return;
        }

        // first try service locator
        try {
            this.customTrigger = this.jobScalerContext.getContext().getServiceLocator()
                .service(customTriggerClassName, JobScalingRuleCustomTrigger.class);
            log.info("Loaded custom trigger class from service locator: {}", customTriggerClassName);
        } catch (Exception e) {
            log.warn("Failed to load custom trigger class from service locator: {}", customTriggerClassName, e);
        }

        // if not found, try loading directly
        try {
            if (this.customTrigger == null) {
                log.info("Try loading custom trigger class directly: {}", customTriggerClassName);
                this.customTrigger =
                    (JobScalingRuleCustomTrigger) Class.forName(customTriggerClassName).newInstance();
                log.info("Loaded custom trigger class directly: {}", customTriggerClassName);
            }
        } catch (Exception e) {
            log.error("Failed to load custom trigger class directly: {}, no custom trigger available",
                customTriggerClassName, e);
            return;
        }

        // initialize custom trigger
        if (this.customTrigger == null) {
            log.error("[{}] Custom trigger is not available: {}, Ignore custom rule: {}.",
                this.jobScalerContext.getJobId(), customTriggerClassName, this.rule);
            return;
        }

        // capture needed pointers for callbacks
        ActorRef coordinatorActor = getContext().getParent();
        ActorRef self = getSelf();
        ExecutionContextExecutor dispatcher = getContext().getDispatcher();
        CustomRuleTriggerHandler triggerHandler = CustomRuleTriggerHandler.builder()
            .callBackExecutor(dispatcher)
            .activateCallback(rule -> coordinatorActor.tell(
                CoordinatorActor.ActivateRuleRequest.of(this.jobScalerContext.getJobId(), rule), self))
            .deactivateCallback(ruleId -> coordinatorActor.tell(
                CoordinatorActor.DeactivateRuleRequest.of(this.jobScalerContext.getJobId(), ruleId), self))
            .build();

        Future<Void> startAndRunCustomRuleFuture = future(() -> {
            try {
                this.customTrigger.init(
                    this.jobScalerContext,
                    this.rule,
                    triggerHandler
                );

                log.info("Starting custom trigger: {}", customTriggerClassName);
                this.customTrigger.run();
            } catch (Exception e) {
                log.error("Custom trigger run failed with: ", e);
                throw new RuntimeException(e);
            }
            return null;
        }, customExecutionContext);

        startAndRunCustomRuleFuture.onComplete(result -> {
            if (result.isSuccess()) {
                log.info("Custom trigger started successfully");
            } else {
                log.error("failed to run custom rule: {}",
                    this.rule, result.failed().get());
            }
            return null;
        }, getContext().dispatcher());
    }