package xyz.wbsite.task;

import xyz.wbsite.action.GlobalController;
import xyz.wbsite.frame.auth.LocalData;
import xyz.wbsite.frame.schedule.RunDelayRepeatTask;
import xyz.wbsite.module.conf.ent.Message;
import xyz.wbsite.module.conf.ent.MessageType;
import xyz.wbsite.module.conf.ent.NginxCtrl;
import xyz.wbsite.module.conf.ent.State;

import java.time.Duration;

public class StatusTask extends RunDelayRepeatTask {

    @Override
    public String taskId() {
        return "task1";
    }

    @Override
    public Duration interval() {
        return Duration.ofSeconds(3);
    }

    @Override
    public void run() {
        NginxCtrl nginxCtrl = LocalData.getBean(NginxCtrl.class);
        GlobalController globalController = LocalData.getBean(GlobalController.class);
        if (nginxCtrl == null || globalController == null) {
            return;
        }

        Message message = new Message();
        message.setType(MessageType.NGINX_STATE);
        State state = new State();
        state.setRun(nginxCtrl.isRunning());
        message.setObject(state);
        globalController.pushAll(message);
    }
}
