package com.yfd.platform.component;

import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class TaskStatusHolder {

    private final ConcurrentHashMap<String, String> taskStatusMap = new ConcurrentHashMap<>();

    // 生成唯一Key：taskId + nodeId
    public String generateKey(String taskId, String nodeId) {
        return taskId + ":" + nodeId;
    }

    // 原子性检查并标记任务开始
    public boolean startTaskIfAbsent(String key) {
        return taskStatusMap.putIfAbsent(key, "IN_PROGRESS") == null;
    }

    // 标记任务结束
    public void finishTask(String key) {
        taskStatusMap.put(key, "COMPLETED");
    }

    // 获取任务状态
    public String getStatus(String key) {
        return taskStatusMap.getOrDefault(key, "TASK_NOT_FOUND");
    }
}
