package com.zachpendleton.sidekiq.reporter;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

/**
 * A Sidekiq queue with a name and a length.
 */
class SidekiqQueue {
    private static final Logger logger = Logger.getLogger(SidekiqQueue.class);

    private Jedis connection;

    private final String name;

    SidekiqQueue(String name) {
        this.name = name;
    }

    /**
     * Connect to the Redis server hosting this queue.
     *
     * @param host the hostname of the Redis server
     * @param port the port the Redis server is listening on
     *
     * @return success boolean
     */
    public boolean connect(String host, int port) {
        return connect(host, port, 0);
    }

    /**
     * Connect to the Redis server hosting this queue.
     *
     * @param host the hostname of the Redis server
     * @param port the port the Redis server is listening on
     * @param databaseId the database ID containing Sidekiq information
     *
     * @return success boolean
     */
    public boolean connect(String host, int port, int databaseId) {
        connection = new Jedis(host, port);

        if (connection.isConnected()) {
            connection.select(databaseId);
        }

        return connection.isConnected();
    }

    /**
     * Disconnect from the Redis server.
     */
    public void disconnect() {
        if (connection != null && connection.isConnected()) {
            connection.disconnect();
        }
    }

    /**
     * Fetch the queue size from the Redis server.
     *
     * @return the size of the queue
     */
    public long getSize() {
        if (connection == null || !connection.isConnected()) {
            logger.warn("attempted to access queue '" + queueKey() + "' without connecting to redis");
            return 0;
        }

        return connection.llen(queueKey());
    }

    /**
     * Construct the Redis key for this queue.
     *
     * @return a key name
     */
    private String queueKey() {
        return "queue:" + name;
    }
}

@@ -1126,16 +1126,46 @@
 , port);
+%0A        connection.connect();
 %0A%0A      
@@ -1242,33 +1242,141 @@
 seId);%0A        %7D
-%0A
+ else %7B%0A            logger.error(String.format(%22unable to connect to redis on %25s:%25d%22, host, port));%0A        %7D
 %0A        return 

connect to redis before issuing commands