package com.bsmlab.dfx.agent.config.jmx;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

@RequiredArgsConstructor
public class DfxAgentControl implements DfxAgentControlMBean {
    private final ConfigurableApplicationContext ctx;

    @Override
    public String status() {
        return "OK";
    }

    @Override
    public void shutdown(int exitCode, int delaySeconds) {
        new Thread(() -> {
            try {
                if (delaySeconds > 0) Thread.sleep(delaySeconds * 1000L);
            } catch (InterruptedException ignored) { }

            int code = SpringApplication.exit(ctx, () -> exitCode);
            System.exit(code);
        }, "dfxagent-jmx-shutdown").start();
    }
}
