/**
 * 
 */
package org.springblade.etl.engine.datax.event;

import java.util.LinkedList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
public final class JobRunInstance {

	private final LinkedList<JobRunTarget> queue = new LinkedList<>();
	
	private final Object lock = new Object();
	
	private final static JobRunInstance defaultInstance = new JobRunInstance();

    private JobRunInstance() {}

    public static JobRunInstance getInstance() {
        return defaultInstance;
    }
	
	/**
	 * 添加任务
	 * @param ids
	 */
	public void add(List<JobRunTarget> targets) {
		synchronized (lock) {
			targets.stream().forEach(queue::addLast);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public JobRunTarget get() {
		synchronized (lock) {
			log.info("dsyWaitingInstance:num: {}", queue.size());
			if(queue.isEmpty()) {
				return null;
			}
			return queue.removeFirst();
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public int getTaskSize() {
        synchronized (lock) {
            return queue.size();
        }
    }
	
}
