{"action":"created","comment":{"url":"https://api.github.com/repos/lookout/whoas/pulls/comments/26692318","id":26692318,"diff_hunk":"@@ -0,0 +1,138 @@\n+package com.github.lookout.whoas\n+\n+import com.fasterxml.jackson.databind.ObjectMapper\n+import redis.clients.jedis.Jedis;\n+\n+/**\n+ * A redis queue that offers distributed and persistent queue\n+ */\n+class RedisQueue extends AbstractHookQueue {\n+    private final RedisClientFactory redisClientFactory\n+    private static nextId = 0\n+    private String hostname\n+    private Integer port\n+\n+    /**\n+     * Create the RedisQueue with valid config\n+     */\n+    RedisQueue(WhoasQueueConfig queueConfig) {\n+        redisClientFactory = RedisClientFactory.getInstance()\n+        this.hostname = queueConfig.hostname\n+        this.port = queueConfig.port\n+    }\n+\n+    /**\n+     * Default constructor\n+     */\n+    RedisQueue() {\n+        redisClientFactory = RedisClientFactory.getInstance()\n+        this.hostname = \"localhost\"\n+        this.port = 6379\n+    }\n+    /**\n+     * Return the number of elements in the queue\n+     */\n+    Long getSize() {\n+        if (!this.started) {\n+            throw new Exception(\"Queue must be started before this operation is invoked\")\n+        }\n+        Jedis redisclient = null\n+        Long queueSize = 0\n+        try {\n+            redisclient = redisClientFactory.getJedis()\n+            queueSize = redisclient.llen(\"queue\")\n+        } catch (all) {\n+        } finally {\n+            if (redisclient != null) {\n+                redisClientFactory.returnJedis(redisclient)\n+            }\n+        }\n+        return queueSize\n+    }\n+\n+    /**\n+     * Setup the Redis client factory\n+     */\n+    @Override\n+    void start() {\n+        super.start()\n+        redisClientFactory.start(this.hostname, this.port)\n+    }\n+\n+    /**\n+     * Stop the Redis client factory\n+     */\n+    @Override\n+    void stop() {\n+        super.stop()\n+        redisClientFactory.stop()\n+    }\n+\n+    /**\n+     * Performs a blocking pop on the queue and invokes the closure with the\n+     * item popped from the queue\n+     *\n+     * If the Closure throws an exception, the dequeued item will be returned\n+     * to the tail end of the queue\n+     */\n+    void pop(Closure action) {\n+        if (action == null) {\n+            throw new Exception(\"Must provide a Closure to RedisQueue.pop()\")\n+        }\n+\n+        if (!this.started) {\n+            throw new Exception(\"Queue must be started before this operation is invoked\")\n+        }\n+\n+        List<String> messages = null\n+        Jedis redisclient = null\n+        try {\n+            redisclient = redisClientFactory.getJedis()\n+            messages = redisclient.blpop(0, \"queue\");\n+\n+            /* Decode message */\n+            if (messages) {\n+                ObjectMapper mapper = new ObjectMapper()\n+                HookRequest request = mapper.readValue(messages.get(1), HookRequest.class)\n+                action.call(request)\n+            }\n+        } catch (all) {\n+            /* Put this back on the front of the queue */\n+            if (messages) {\n+                redisclient.lpush(\"queue\", messages.get(1))\n+            }\n+        } finally {\n+            if (redisclient != null) {\n+                redisClientFactory.returnJedis(redisclient)\n+            }\n+        }\n+    }\n+\n+    /**\n+     * Attempt to insert the request into the queue\n+     *\n+     * If the request cannot be inserted, this method will return false,\n+     * otherwise true.\n+     */\n+    Boolean push(HookRequest request) {\n+        if (!this.started) {\n+            throw new Exception(\"Queue must be started before this operation is invoked\")\n+        }\n+\n+        request.id = ++nextId\n+        ObjectMapper mapper = new ObjectMapper()\n+        String jsonPayload = mapper.writeValueAsString(request)\n+        Jedis redisclient = null\n+        Integer ret = 0\n+        try {\n+            redisclient = redisClientFactory.getJedis()\n+            ret = redisclient.rpush(\"queue\", jsonPayload)\n+        } catch (Exception e) {\n+        } finally {\n+            if (redisclient != null) {\n+                redisClientFactory.returnJedis(redisclient)\n+            }\n+        }\n+        return ret == 1;","path":"src/main/groovy/com/github/lookout/whoas/RedisQueue.groovy","position":136,"original_position":136,"commit_id":"4c98a7e0e7ddd86915b3c8d6092f9be303130aff","original_commit_id":"4c98a7e0e7ddd86915b3c8d6092f9be303130aff","user":{"login":"maheshkelkar","id":10839177,"avatar_url":"https://avatars.githubusercontent.com/u/10839177?v=3","gravatar_id":"","url":"https://api.github.com/users/maheshkelkar","html_url":"https://github.com/maheshkelkar","followers_url":"https://api.github.com/users/maheshkelkar/followers","following_url":"https://api.github.com/users/maheshkelkar/following{/other_user}","gists_url":"https://api.github.com/users/maheshkelkar/gists{/gist_id}","starred_url":"https://api.github.com/users/maheshkelkar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maheshkelkar/subscriptions","organizations_url":"https://api.github.com/users/maheshkelkar/orgs","repos_url":"https://api.github.com/users/maheshkelkar/repos","events_url":"https://api.github.com/users/maheshkelkar/events{/privacy}","received_events_url":"https://api.github.com/users/maheshkelkar/received_events","type":"User","site_admin":false},"body":"In this case we are pushing only 1 messages, then it will be either 0 or 1","created_at":"2015-03-18T18:26:25Z","updated_at":"2015-03-18T18:26:25Z","html_url":"https://github.com/lookout/whoas/pull/18#discussion_r26692318","pull_request_url":"https://api.github.com/repos/lookout/whoas/pulls/18","_links":{"self":{"href":"https://api.github.com/repos/lookout/whoas/pulls/comments/26692318"},"html":{"href":"https://github.com/lookout/whoas/pull/18#discussion_r26692318"},"pull_request":{"href":"https://api.github.com/repos/lookout/whoas/pulls/18"}}},"pull_request":{"url":"https://api.github.com/repos/lookout/whoas/pulls/18","id":31449529,"html_url":"https://github.com/lookout/whoas/pull/18","diff_url":"https://github.com/lookout/whoas/pull/18.diff","patch_url":"https://github.com/lookout/whoas/pull/18.patch","issue_url":"https://api.github.com/repos/lookout/whoas/issues/18","number":18,"state":"open","locked":false,"title":"Added RedisQueue using Jedis for Redis as a queue provider","user":{"login":"maheshkelkar","id":10839177,"avatar_url":"https://avatars.githubusercontent.com/u/10839177?v=3","gravatar_id":"","url":"https://api.github.com/users/maheshkelkar","html_url":"https://github.com/maheshkelkar","followers_url":"https://api.github.com/users/maheshkelkar/followers","following_url":"https://api.github.com/users/maheshkelkar/following{/other_user}","gists_url":"https://api.github.com/users/maheshkelkar/gists{/gist_id}","starred_url":"https://api.github.com/users/maheshkelkar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maheshkelkar/subscriptions","organizations_url":"https://api.github.com/users/maheshkelkar/orgs","repos_url":"https://api.github.com/users/maheshkelkar/repos","events_url":"https://api.github.com/users/maheshkelkar/events{/privacy}","received_events_url":"https://api.github.com/users/maheshkelkar/received_events","type":"User","site_admin":false},"body":"Added spock tests, Jedis client for Redis client, Jedis pool for multi-threading support, Added new queue configurations (new struct), updated docs\r\n\r\nReferences #3 ","created_at":"2015-03-18T17:01:46Z","updated_at":"2015-03-18T18:26:25Z","closed_at":null,"merged_at":null,"merge_commit_sha":"451b8da8db3d406556d84d0ce2f87c5178b512bb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lookout/whoas/pulls/18/commits","review_comments_url":"https://api.github.com/repos/lookout/whoas/pulls/18/comments","review_comment_url":"https://api.github.com/repos/lookout/whoas/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lookout/whoas/issues/18/comments","statuses_url":"https://api.github.com/repos/lookout/whoas/statuses/4c98a7e0e7ddd86915b3c8d6092f9be303130aff","head":{"label":"maheshkelkar:issues/3-add-redis","ref":"issues/3-add-redis","sha":"4c98a7e0e7ddd86915b3c8d6092f9be303130aff","user":{"login":"maheshkelkar","id":10839177,"avatar_url":"https://avatars.githubusercontent.com/u/10839177?v=3","gravatar_id":"","url":"https://api.github.com/users/maheshkelkar","html_url":"https://github.com/maheshkelkar","followers_url":"https://api.github.com/users/maheshkelkar/followers","following_url":"https://api.github.com/users/maheshkelkar/following{/other_user}","gists_url":"https://api.github.com/users/maheshkelkar/gists{/gist_id}","starred_url":"https://api.github.com/users/maheshkelkar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maheshkelkar/subscriptions","organizations_url":"https://api.github.com/users/maheshkelkar/orgs","repos_url":"https://api.github.com/users/maheshkelkar/repos","events_url":"https://api.github.com/users/maheshkelkar/events{/privacy}","received_events_url":"https://api.github.com/users/maheshkelkar/received_events","type":"User","site_admin":false},"repo":{"id":31934327,"name":"whoas","full_name":"maheshkelkar/whoas","owner":{"login":"maheshkelkar","id":10839177,"avatar_url":"https://avatars.githubusercontent.com/u/10839177?v=3","gravatar_id":"","url":"https://api.github.com/users/maheshkelkar","html_url":"https://github.com/maheshkelkar","followers_url":"https://api.github.com/users/maheshkelkar/followers","following_url":"https://api.github.com/users/maheshkelkar/following{/other_user}","gists_url":"https://api.github.com/users/maheshkelkar/gists{/gist_id}","starred_url":"https://api.github.com/users/maheshkelkar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maheshkelkar/subscriptions","organizations_url":"https://api.github.com/users/maheshkelkar/orgs","repos_url":"https://api.github.com/users/maheshkelkar/repos","events_url":"https://api.github.com/users/maheshkelkar/events{/privacy}","received_events_url":"https://api.github.com/users/maheshkelkar/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/maheshkelkar/whoas","description":"Web Hooks on a Service, a most excellent adventure","fork":true,"url":"https://api.github.com/repos/maheshkelkar/whoas","forks_url":"https://api.github.com/repos/maheshkelkar/whoas/forks","keys_url":"https://api.github.com/repos/maheshkelkar/whoas/keys{/key_id}","collaborators_url":"https://api.github.com/repos/maheshkelkar/whoas/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/maheshkelkar/whoas/teams","hooks_url":"https://api.github.com/repos/maheshkelkar/whoas/hooks","issue_events_url":"https://api.github.com/repos/maheshkelkar/whoas/issues/events{/number}","events_url":"https://api.github.com/repos/maheshkelkar/whoas/events","assignees_url":"https://api.github.com/repos/maheshkelkar/whoas/assignees{/user}","branches_url":"https://api.github.com/repos/maheshkelkar/whoas/branches{/branch}","tags_url":"https://api.github.com/repos/maheshkelkar/whoas/tags","blobs_url":"https://api.github.com/repos/maheshkelkar/whoas/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/maheshkelkar/whoas/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/maheshkelkar/whoas/git/refs{/sha}","trees_url":"https://api.github.com/repos/maheshkelkar/whoas/git/trees{/sha}","statuses_url":"https://api.github.com/repos/maheshkelkar/whoas/statuses/{sha}","languages_url":"https://api.github.com/repos/maheshkelkar/whoas/languages","stargazers_url":"https://api.github.com/repos/maheshkelkar/whoas/stargazers","contributors_url":"https://api.github.com/repos/maheshkelkar/whoas/contributors","subscribers_url":"https://api.github.com/repos/maheshkelkar/whoas/subscribers","subscription_url":"https://api.github.com/repos/maheshkelkar/whoas/subscription","commits_url":"https://api.github.com/repos/maheshkelkar/whoas/commits{/sha}","git_commits_url":"https://api.github.com/repos/maheshkelkar/whoas/git/commits{/sha}","comments_url":"https://api.github.com/repos/maheshkelkar/whoas/comments{/number}","issue_comment_url":"https://api.github.com/repos/maheshkelkar/whoas/issues/comments{/number}","contents_url":"https://api.github.com/repos/maheshkelkar/whoas/contents/{+path}","compare_url":"https://api.github.com/repos/maheshkelkar/whoas/compare/{base}...{head}","merges_url":"https://api.github.com/repos/maheshkelkar/whoas/merges","archive_url":"https://api.github.com/repos/maheshkelkar/whoas/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/maheshkelkar/whoas/downloads","issues_url":"https://api.github.com/repos/maheshkelkar/whoas/issues{/number}","pulls_url":"https://api.github.com/repos/maheshkelkar/whoas/pulls{/number}","milestones_url":"https://api.github.com/repos/maheshkelkar/whoas/milestones{/number}","notifications_url":"https://api.github.com/repos/maheshkelkar/whoas/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/maheshkelkar/whoas/labels{/name}","releases_url":"https://api.github.com/repos/maheshkelkar/whoas/releases{/id}","created_at":"2015-03-10T02:32:24Z","updated_at":"2015-03-10T02:32:24Z","pushed_at":"2015-03-18T16:56:50Z","git_url":"git://github.com/maheshkelkar/whoas.git","ssh_url":"git@github.com:maheshkelkar/whoas.git","clone_url":"https://github.com/maheshkelkar/whoas.git","svn_url":"https://github.com/maheshkelkar/whoas","homepage":"","size":288,"stargazers_count":0,"watchers_count":0,"language":"Groovy","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lookout:master","ref":"master","sha":"551bf94274e35efcd1e4159d58ea4fd00b358730","user":{"login":"lookout","id":47237,"avatar_url":"https://avatars.githubusercontent.com/u/47237?v=3","gravatar_id":"","url":"https://api.github.com/users/lookout","html_url":"https://github.com/lookout","followers_url":"https://api.github.com/users/lookout/followers","following_url":"https://api.github.com/users/lookout/following{/other_user}","gists_url":"https://api.github.com/users/lookout/gists{/gist_id}","starred_url":"https://api.github.com/users/lookout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lookout/subscriptions","organizations_url":"https://api.github.com/users/lookout/orgs","repos_url":"https://api.github.com/users/lookout/repos","events_url":"https://api.github.com/users/lookout/events{/privacy}","received_events_url":"https://api.github.com/users/lookout/received_events","type":"Organization","site_admin":false},"repo":{"id":28651565,"name":"whoas","full_name":"lookout/whoas","owner":{"login":"lookout","id":47237,"avatar_url":"https://avatars.githubusercontent.com/u/47237?v=3","gravatar_id":"","url":"https://api.github.com/users/lookout","html_url":"https://github.com/lookout","followers_url":"https://api.github.com/users/lookout/followers","following_url":"https://api.github.com/users/lookout/following{/other_user}","gists_url":"https://api.github.com/users/lookout/gists{/gist_id}","starred_url":"https://api.github.com/users/lookout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lookout/subscriptions","organizations_url":"https://api.github.com/users/lookout/orgs","repos_url":"https://api.github.com/users/lookout/repos","events_url":"https://api.github.com/users/lookout/events{/privacy}","received_events_url":"https://api.github.com/users/lookout/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lookout/whoas","description":"Web Hooks on a Service, a most excellent adventure","fork":false,"url":"https://api.github.com/repos/lookout/whoas","forks_url":"https://api.github.com/repos/lookout/whoas/forks","keys_url":"https://api.github.com/repos/lookout/whoas/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lookout/whoas/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lookout/whoas/teams","hooks_url":"https://api.github.com/repos/lookout/whoas/hooks","issue_events_url":"https://api.github.com/repos/lookout/whoas/issues/events{/number}","events_url":"https://api.github.com/repos/lookout/whoas/events","assignees_url":"https://api.github.com/repos/lookout/whoas/assignees{/user}","branches_url":"https://api.github.com/repos/lookout/whoas/branches{/branch}","tags_url":"https://api.github.com/repos/lookout/whoas/tags","blobs_url":"https://api.github.com/repos/lookout/whoas/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lookout/whoas/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lookout/whoas/git/refs{/sha}","trees_url":"https://api.github.com/repos/lookout/whoas/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lookout/whoas/statuses/{sha}","languages_url":"https://api.github.com/repos/lookout/whoas/languages","stargazers_url":"https://api.github.com/repos/lookout/whoas/stargazers","contributors_url":"https://api.github.com/repos/lookout/whoas/contributors","subscribers_url":"https://api.github.com/repos/lookout/whoas/subscribers","subscription_url":"https://api.github.com/repos/lookout/whoas/subscription","commits_url":"https://api.github.com/repos/lookout/whoas/commits{/sha}","git_commits_url":"https://api.github.com/repos/lookout/whoas/git/commits{/sha}","comments_url":"https://api.github.com/repos/lookout/whoas/comments{/number}","issue_comment_url":"https://api.github.com/repos/lookout/whoas/issues/comments{/number}","contents_url":"https://api.github.com/repos/lookout/whoas/contents/{+path}","compare_url":"https://api.github.com/repos/lookout/whoas/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lookout/whoas/merges","archive_url":"https://api.github.com/repos/lookout/whoas/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lookout/whoas/downloads","issues_url":"https://api.github.com/repos/lookout/whoas/issues{/number}","pulls_url":"https://api.github.com/repos/lookout/whoas/pulls{/number}","milestones_url":"https://api.github.com/repos/lookout/whoas/milestones{/number}","notifications_url":"https://api.github.com/repos/lookout/whoas/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lookout/whoas/labels{/name}","releases_url":"https://api.github.com/repos/lookout/whoas/releases{/id}","created_at":"2014-12-31T01:30:54Z","updated_at":"2015-03-18T17:02:04Z","pushed_at":"2015-03-18T17:02:06Z","git_url":"git://github.com/lookout/whoas.git","ssh_url":"git@github.com:lookout/whoas.git","clone_url":"https://github.com/lookout/whoas.git","svn_url":"https://github.com/lookout/whoas","homepage":"","size":288,"stargazers_count":0,"watchers_count":0,"language":"Groovy","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":3,"mirror_url":null,"open_issues_count":7,"forks":3,"open_issues":7,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lookout/whoas/pulls/18"},"html":{"href":"https://github.com/lookout/whoas/pull/18"},"issue":{"href":"https://api.github.com/repos/lookout/whoas/issues/18"},"comments":{"href":"https://api.github.com/repos/lookout/whoas/issues/18/comments"},"review_comments":{"href":"https://api.github.com/repos/lookout/whoas/pulls/18/comments"},"review_comment":{"href":"https://api.github.com/repos/lookout/whoas/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lookout/whoas/pulls/18/commits"},"statuses":{"href":"https://api.github.com/repos/lookout/whoas/statuses/4c98a7e0e7ddd86915b3c8d6092f9be303130aff"}}}}