package com.docus.server.archivefile.service.impl;

import com.docus.core.util.json.JSON;
import com.docus.log.annotation.TrackLogGroup;
import com.docus.log.executor.TrackRetrySpringExecutor;
import com.docus.log.handler.IJobHandler;
import com.docus.log.handler.impl.MethodJobHandler;
import com.docus.server.archivefile.process.RetryProcessor;
import com.docus.server.entity.TaskConfig;
import com.docus.server.entity.TaskMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 重试service
 */
@Service
@Slf4j
public class TrackRetryService {

    @TrackLogGroup(group = "doRetryTaskMessage", processor = RetryProcessor.class)
    @Async("recordMessage")
    public void doRetryTaskMessage(TaskMessage message) throws Exception {
        IJobHandler jobHandler = TrackRetrySpringExecutor.loadJobHandler(message.getRetryKey());
        String name = ((MethodJobHandler) jobHandler).getMethod().getParameterTypes()[0].getName();
        Object o = JSON.fromJSON(message.getJsonStr(), Class.forName(name));

        jobHandler.execute(o);
    }

    @TrackLogGroup(group = "doRetryTaskConfig", processor = RetryProcessor.class)
    @Async("recordMessage")
    public void doRetryTaskConfig(TaskConfig config) throws Exception {
        IJobHandler jobHandler = TrackRetrySpringExecutor.loadJobHandler(config.getRetryKey());

        jobHandler.execute(String.valueOf(config.getId()));
    }
}
