
package cn.orionsec.ops.handler.app.release;

import cn.orionsec.kit.lang.utils.Exceptions;
import cn.orionsec.kit.lang.utils.Threads;
import cn.orionsec.ops.constant.MessageConst;
import cn.orionsec.ops.constant.SchedulerPools;
import cn.orionsec.ops.constant.app.ActionStatus;
import cn.orionsec.ops.handler.app.machine.ReleaseMachineProcessor;

import java.util.Collection;

public class ParallelReleaseProcessor extends AbstractReleaseProcessor {

    public ParallelReleaseProcessor(Long releaseId) {
        super(releaseId);
    }

    @Override
    protected void handler() throws Exception {
        Collection<ReleaseMachineProcessor> processor = machineProcessors.values();
        Threads.blockRun(processor, SchedulerPools.RELEASE_MACHINE_SCHEDULER);
        // 检查是否停止
        if (terminated) {
            return;
        }
        // 全部停止
        final boolean allTerminated = processor.stream()
                .map(ReleaseMachineProcessor::getStatus)
                .allMatch(ActionStatus.TERMINATED::equals);
        if (allTerminated) {
            this.terminated = true;
            return;
        }
        // 全部完成
        boolean allFinish = processor.stream()
                .map(ReleaseMachineProcessor::getStatus)
                .filter(s -> !ActionStatus.TERMINATED.equals(s))
                .allMatch(ActionStatus.FINISH::equals);
        if (!allFinish) {
            throw Exceptions.log(MessageConst.OPERATOR_NOT_ALL_SUCCESS);
        }
    }

    @Override
    public void terminateAll() {
        super.terminateAll();
        machineProcessors.values().forEach(ReleaseMachineProcessor::terminate);
    }

}
