gresrun / jesque

An implementation of Resque in Java.
http://gresrun.github.io/jesque
Apache License 2.0
630 stars 131 forks source link

Batch job queuing? #123

Closed vincentjames501 closed 5 years ago

vincentjames501 commented 7 years ago

It would be nice to add to the Client interface the ability to batch queue jobs. In our case we need to batch queue around 40-100k jobs and it would be nice to do it in a single pipeline w/ Jedis.

void enqueue(String queue, List<Job> jobs); // Or Job...
wbijen commented 6 years ago

This works! Here is a link to my repo: https://github.com/wbijen/jesque

public static void doBulkEnqueue(Jedis jedis, String namespace, String queue, List<String> jobJsonList, int batchSize) {
        jedis.sadd(JesqueUtils.createKey(namespace, new String[]{"queues"}), new String[]{queue});
        Pipeline pipeline = jedis.pipelined();
        String key = JesqueUtils.createKey(namespace, new String[]{"queue", queue});
        int i = 0;

        for (String jobJson : jobJsonList) {
            String jobJson = it.next();
            jedis.rpush(key, new String[]{jobJson});
            ++i;
            if (i == batchSize) {
                pipeline.sync();
                pipeline = jedis.pipelined();
                i = 0;
            }
        }
        if (i > 0) {
            pipeline.sync();
        }
    }