package cn.axzo.workflow.core.engine.job.exception.handler;

import cn.axzo.workflow.core.engine.event.ErrorInfoEvent;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.job.api.JobInfo;
import org.flowable.job.service.JobServiceConfiguration;
import org.flowable.job.service.impl.asyncexecutor.AsyncRunnableExecutionExceptionHandler;

/**
 * 自定义的异步任务执行异常处理器
 *
 * @author wangli
 * @since 2024/4/17 16:03
 */
@Slf4j
public class CustomAsyncRunnableExecutionExceptionHandler implements AsyncRunnableExecutionExceptionHandler {

    @Override
    public boolean handleException(JobServiceConfiguration jobServiceConfiguration, JobInfo job, Throwable exception) {
        log.info("discover exception, jobId: {}, exception: {}", job.getId(), exception.getMessage());

        FlowableEventDispatcher eventDispatcher = jobServiceConfiguration.getEventDispatcher();

        eventDispatcher.dispatchEvent(new ErrorInfoEvent(job, exception), jobServiceConfiguration.getEngineName());

        return false;
    }
}
