/**
 * 
 */
package org.springblade.etl.task.service;

import java.util.concurrent.TimeUnit;

import org.springblade.etl.task.model.DaemonTaskConfig;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
public abstract class AbstractDaemonTask implements DaemonTask {

	protected DaemonTaskConfig config;
	
	@Override
	public void deal() {
		long gap = System.currentTimeMillis() - config.getAddTimeMillis();
		long timeMillisDiff = config.getType().getSleepMills() - gap;
		if (timeMillisDiff > 0) {
			try {
              TimeUnit.MILLISECONDS.sleep(timeMillisDiff);
          } catch (InterruptedException e) {
              log.error(e.getMessage(), e);
          }
		}
        invoke(Integer.valueOf(config.getId()));
	}

	@Override
	public DaemonTask setConfig(DaemonTaskConfig config) {
		this.config = config;
		return this;
	}

	/**
	 * @param dataId
	 */
	public abstract void invoke(Integer dataId);

}
