package cc.smtweb.framework.core.systask;

import cc.smtweb.framework.core.common.SwConsts;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by Akmm at 2018/11/27 23:46
 * 系统任务基类
 */
@Slf4j
public abstract class BaseSysService implements Runnable {
    public boolean isPrintLog() {
        return true;
    }

    //标题，描述性文字
    public abstract String getTitle();

    //间隔时间，单位秒
    public abstract int getInterval();

    @Override
    public void run() {
        try {
            final String id = this.getClass().getName();
            SingleRequestHelper.singleRequest(SwConsts.REDIS_KEY_BASE_JOB, id, "任务执行中，本次忽略.........", () -> {
                long time = System.currentTimeMillis();
                if (isPrintLog()) {
                    log.debug(getTitle() + ":::开始执行.........");
                }
                work();
                if (isPrintLog()) {
                    log.debug(getTitle() + ":::执行完成，耗时：" + (System.currentTimeMillis() - time) + "毫秒.........");
                }
            });
        } catch (Exception e) {
            log.error(getTitle() + ":::执行失败", e);
        }
    }

    //主要要干的活
    protected abstract void work();
}
