package com.lanyuanxiaoyao.service.scheduler.quartz.distribute.strategy;

import com.lanyuanxiaoyao.service.configuration.utils.QueueUtil;
import org.springframework.cloud.client.discovery.DiscoveryClient;

/**
 * 根据队列剩余任务数限制
 *
 * @author ZhangJiacheng
 * @date 2023-06-08
 */
public class QueueSizeLimit implements AvailableStrategy {
    private final DiscoveryClient discoveryClient;
    private final String queue;
    private final Integer limit;

    public QueueSizeLimit(DiscoveryClient discoveryClient, String queue, Integer limit) {
        this.discoveryClient = discoveryClient;
        this.queue = queue;
        this.limit = limit;
    }

    @Override
    public boolean available() {
        return QueueUtil.size(discoveryClient, queue) < limit;
    }
}
