
package cn.orionsec.ops.task;

import cn.orionsec.ops.constant.Const;
import cn.orionsec.ops.task.impl.PipelineTaskImpl;
import cn.orionsec.ops.task.impl.ReleaseTaskImpl;
import cn.orionsec.ops.task.impl.SchedulerTaskImpl;
import lombok.AllArgsConstructor;

import java.util.function.Function;

@AllArgsConstructor
public enum TaskType {

    /**
     * 发布任务
     */
    RELEASE(id -> new ReleaseTaskImpl((Long) id)) {
        @Override
        public String getKey(Object params) {
            return Const.RELEASE + "-" + params;
        }
    },

    /**
     * 调度任务
     */
    SCHEDULER_TASK(id -> new SchedulerTaskImpl((Long) id)) {
        @Override
        public String getKey(Object params) {
            return Const.TASK + "-" + params;
        }
    },

    /**
     * 流水线任务
     */
    PIPELINE(id -> new PipelineTaskImpl((Long) id)) {
        @Override
        public String getKey(Object params) {
            return Const.PIPELINE + "-" + params;
        }
    },

    ;

    private final Function<Object, Runnable> factory;

    /**
     * 创建任务
     *
     * @param params params
     * @return task
     */
    public Runnable create(Object params) {
        return factory.apply(params);
    }

    /**
     * 获取 key
     *
     * @param params params
     * @return key
     */
    public abstract String getKey(Object params);

}
