package com.inspect.job.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class TaskStatusManager {
    private static final Logger log = LoggerFactory.getLogger(TaskStatusManager.class);
    private static final ConcurrentHashMap<String, StatusMonitor> monitorMap = new ConcurrentHashMap<>();

    public static StatusMonitor register(String taskId, Supplier<Integer> statusSupplier) {
        return monitorMap.computeIfAbsent(taskId, id -> new StatusMonitor(statusSupplier));
    }

    public static StatusMonitor get(String taskId) {
        return monitorMap.get(taskId);
    }

    public static void remove(String taskId) {
        StatusMonitor monitor = monitorMap.remove(taskId);
        if (monitor != null) {
            log.info("TaskStatusManager removed: {} ", taskId);
            monitor.stop();
        }
    }
}

