quredis 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,377 @@
1
+ (function($, undefined) {
2
+
3
+ /**
4
+ * Unobtrusive scripting adapter for jQuery
5
+ *
6
+ * Requires jQuery 1.6.0 or later.
7
+ * https://github.com/rails/jquery-ujs
8
+
9
+ * Uploading file using rails.js
10
+ * =============================
11
+ *
12
+ * By default, browsers do not allow files to be uploaded via AJAX. As a result, if there are any non-blank file fields
13
+ * in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.
14
+ *
15
+ * The `ajax:aborted:file` event allows you to bind your own handler to process the form submission however you wish.
16
+ *
17
+ * Ex:
18
+ * $('form').live('ajax:aborted:file', function(event, elements){
19
+ * // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
20
+ * // Returning false in this handler tells rails.js to disallow standard form submission
21
+ * return false;
22
+ * });
23
+ *
24
+ * The `ajax:aborted:file` event is fired when a file-type input is detected with a non-blank value.
25
+ *
26
+ * Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use
27
+ * techniques like the iframe method to upload the file instead.
28
+ *
29
+ * Required fields in rails.js
30
+ * ===========================
31
+ *
32
+ * If any blank required inputs (required="required") are detected in the remote form, the whole form submission
33
+ * is canceled. Note that this is unlike file inputs, which still allow standard (non-AJAX) form submission.
34
+ *
35
+ * The `ajax:aborted:required` event allows you to bind your own handler to inform the user of blank required inputs.
36
+ *
37
+ * !! Note that Opera does not fire the form's submit event if there are blank required inputs, so this event may never
38
+ * get fired in Opera. This event is what causes other browsers to exhibit the same submit-aborting behavior.
39
+ *
40
+ * Ex:
41
+ * $('form').live('ajax:aborted:required', function(event, elements){
42
+ * // Returning false in this handler tells rails.js to submit the form anyway.
43
+ * // The blank required inputs are passed to this function in `elements`.
44
+ * return ! confirm("Would you like to submit the form with missing info?");
45
+ * });
46
+ */
47
+
48
+ // Shorthand to make it a little easier to call public rails functions from within rails.js
49
+ var rails;
50
+
51
+ $.rails = rails = {
52
+ // Link elements bound by jquery-ujs
53
+ linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
54
+
55
+ // Select elements bound by jquery-ujs
56
+ inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
57
+
58
+ // Form elements bound by jquery-ujs
59
+ formSubmitSelector: 'form',
60
+
61
+ // Form input elements bound by jquery-ujs
62
+ formInputClickSelector: 'form input[type=submit], form input[type=image], form button[type=submit], form button:not(button[type])',
63
+
64
+ // Form input elements disabled during form submission
65
+ disableSelector: 'input[data-disable-with], button[data-disable-with], textarea[data-disable-with]',
66
+
67
+ // Form input elements re-enabled after form submission
68
+ enableSelector: 'input[data-disable-with]:disabled, button[data-disable-with]:disabled, textarea[data-disable-with]:disabled',
69
+
70
+ // Form required input elements
71
+ requiredInputSelector: 'input[name][required]:not([disabled]),textarea[name][required]:not([disabled])',
72
+
73
+ // Form file input elements
74
+ fileInputSelector: 'input:file',
75
+
76
+ // Link onClick disable selector with possible reenable after remote submission
77
+ linkDisableSelector: 'a[data-disable-with]',
78
+
79
+ // Make sure that every Ajax request sends the CSRF token
80
+ CSRFProtection: function(xhr) {
81
+ var token = $('meta[name="csrf-token"]').attr('content');
82
+ if (token) xhr.setRequestHeader('X-CSRF-Token', token);
83
+ },
84
+
85
+ // Triggers an event on an element and returns false if the event result is false
86
+ fire: function(obj, name, data) {
87
+ var event = $.Event(name);
88
+ obj.trigger(event, data);
89
+ return event.result !== false;
90
+ },
91
+
92
+ // Default confirm dialog, may be overridden with custom confirm dialog in $.rails.confirm
93
+ confirm: function(message) {
94
+ return confirm(message);
95
+ },
96
+
97
+ // Default ajax function, may be overridden with custom function in $.rails.ajax
98
+ ajax: function(options) {
99
+ return $.ajax(options);
100
+ },
101
+
102
+ // Default way to get an element's href. May be overridden at $.rails.href.
103
+ href: function(element) {
104
+ return element.attr('href');
105
+ },
106
+
107
+ // Submits "remote" forms and links with ajax
108
+ handleRemote: function(element) {
109
+ var method, url, data, crossDomain, dataType, options;
110
+
111
+ if (rails.fire(element, 'ajax:before')) {
112
+ crossDomain = element.data('cross-domain') || null;
113
+ dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
114
+
115
+ if (element.is('form')) {
116
+ method = element.attr('method');
117
+ url = element.attr('action');
118
+ data = element.serializeArray();
119
+ // memoized value from clicked submit button
120
+ var button = element.data('ujs:submit-button');
121
+ if (button) {
122
+ data.push(button);
123
+ element.data('ujs:submit-button', null);
124
+ }
125
+ } else if (element.is(rails.inputChangeSelector)) {
126
+ method = element.data('method');
127
+ url = element.data('url');
128
+ data = element.serialize();
129
+ if (element.data('params')) data = data + "&" + element.data('params');
130
+ } else {
131
+ method = element.data('method');
132
+ url = rails.href(element);
133
+ data = element.data('params') || null;
134
+ }
135
+
136
+ options = {
137
+ type: method || 'GET', data: data, dataType: dataType, crossDomain: crossDomain,
138
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
139
+ beforeSend: function(xhr, settings) {
140
+ if (settings.dataType === undefined) {
141
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
142
+ }
143
+ return rails.fire(element, 'ajax:beforeSend', [xhr, settings]);
144
+ },
145
+ success: function(data, status, xhr) {
146
+ element.trigger('ajax:success', [data, status, xhr]);
147
+ },
148
+ complete: function(xhr, status) {
149
+ element.trigger('ajax:complete', [xhr, status]);
150
+ },
151
+ error: function(xhr, status, error) {
152
+ element.trigger('ajax:error', [xhr, status, error]);
153
+ }
154
+ };
155
+ // Only pass url to `ajax` options if not blank
156
+ if (url) { options.url = url; }
157
+
158
+ return rails.ajax(options);
159
+ } else {
160
+ return false;
161
+ }
162
+ },
163
+
164
+ // Handles "data-method" on links such as:
165
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
166
+ handleMethod: function(link) {
167
+ var href = rails.href(link),
168
+ method = link.data('method'),
169
+ target = link.attr('target'),
170
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
171
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
172
+ form = $('<form method="post" action="' + href + '"></form>'),
173
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
174
+
175
+ if (csrf_param !== undefined && csrf_token !== undefined) {
176
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
177
+ }
178
+
179
+ if (target) { form.attr('target', target); }
180
+
181
+ form.hide().append(metadata_input).appendTo('body');
182
+ form.submit();
183
+ },
184
+
185
+ /* Disables form elements:
186
+ - Caches element value in 'ujs:enable-with' data store
187
+ - Replaces element text with value of 'data-disable-with' attribute
188
+ - Sets disabled property to true
189
+ */
190
+ disableFormElements: function(form) {
191
+ form.find(rails.disableSelector).each(function() {
192
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
193
+ element.data('ujs:enable-with', element[method]());
194
+ element[method](element.data('disable-with'));
195
+ element.prop('disabled', true);
196
+ });
197
+ },
198
+
199
+ /* Re-enables disabled form elements:
200
+ - Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
201
+ - Sets disabled property to false
202
+ */
203
+ enableFormElements: function(form) {
204
+ form.find(rails.enableSelector).each(function() {
205
+ var element = $(this), method = element.is('button') ? 'html' : 'val';
206
+ if (element.data('ujs:enable-with')) element[method](element.data('ujs:enable-with'));
207
+ element.prop('disabled', false);
208
+ });
209
+ },
210
+
211
+ /* For 'data-confirm' attribute:
212
+ - Fires `confirm` event
213
+ - Shows the confirmation dialog
214
+ - Fires the `confirm:complete` event
215
+
216
+ Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
217
+ Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
218
+ Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
219
+ return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
220
+ */
221
+ allowAction: function(element) {
222
+ var message = element.data('confirm'),
223
+ answer = false, callback;
224
+ if (!message) { return true; }
225
+
226
+ if (rails.fire(element, 'confirm')) {
227
+ answer = rails.confirm(message);
228
+ callback = rails.fire(element, 'confirm:complete', [answer]);
229
+ }
230
+ return answer && callback;
231
+ },
232
+
233
+ // Helper function which checks for blank inputs in a form that match the specified CSS selector
234
+ blankInputs: function(form, specifiedSelector, nonBlank) {
235
+ var inputs = $(), input,
236
+ selector = specifiedSelector || 'input,textarea';
237
+ form.find(selector).each(function() {
238
+ input = $(this);
239
+ // Collect non-blank inputs if nonBlank option is true, otherwise, collect blank inputs
240
+ if (nonBlank ? input.val() : !input.val()) {
241
+ inputs = inputs.add(input);
242
+ }
243
+ });
244
+ return inputs.length ? inputs : false;
245
+ },
246
+
247
+ // Helper function which checks for non-blank inputs in a form that match the specified CSS selector
248
+ nonBlankInputs: function(form, specifiedSelector) {
249
+ return rails.blankInputs(form, specifiedSelector, true); // true specifies nonBlank
250
+ },
251
+
252
+ // Helper function, needed to provide consistent behavior in IE
253
+ stopEverything: function(e) {
254
+ $(e.target).trigger('ujs:everythingStopped');
255
+ e.stopImmediatePropagation();
256
+ return false;
257
+ },
258
+
259
+ // find all the submit events directly bound to the form and
260
+ // manually invoke them. If anyone returns false then stop the loop
261
+ callFormSubmitBindings: function(form, event) {
262
+ var events = form.data('events'), continuePropagation = true;
263
+ if (events !== undefined && events['submit'] !== undefined) {
264
+ $.each(events['submit'], function(i, obj){
265
+ if (typeof obj.handler === 'function') return continuePropagation = obj.handler(event);
266
+ });
267
+ }
268
+ return continuePropagation;
269
+ },
270
+
271
+ // replace element's html with the 'data-disable-with' after storing original html
272
+ // and prevent clicking on it
273
+ disableElement: function(element) {
274
+ element.data('ujs:enable-with', element.html()); // store enabled state
275
+ element.html(element.data('disable-with')); // set to disabled state
276
+ element.bind('click.railsDisable', function(e) { // prevent further clicking
277
+ return rails.stopEverything(e)
278
+ });
279
+ },
280
+
281
+ // restore element to its original state which was disabled by 'disableElement' above
282
+ enableElement: function(element) {
283
+ if (element.data('ujs:enable-with') !== undefined) {
284
+ element.html(element.data('ujs:enable-with')); // set to old enabled state
285
+ // this should be element.removeData('ujs:enable-with')
286
+ // but, there is currently a bug in jquery which makes hyphenated data attributes not get removed
287
+ element.data('ujs:enable-with', false); // clean up cache
288
+ }
289
+ element.unbind('click.railsDisable'); // enable element
290
+ }
291
+
292
+ };
293
+
294
+ $.ajaxPrefilter(function(options, originalOptions, xhr){ if ( !options.crossDomain ) { rails.CSRFProtection(xhr); }});
295
+
296
+ $(document).delegate(rails.linkDisableSelector, 'ajax:complete', function() {
297
+ rails.enableElement($(this));
298
+ });
299
+
300
+ $(document).delegate(rails.linkClickSelector, 'click.rails', function(e) {
301
+ var link = $(this), method = link.data('method'), data = link.data('params');
302
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
303
+
304
+ if (link.is(rails.linkDisableSelector)) rails.disableElement(link);
305
+
306
+ if (link.data('remote') !== undefined) {
307
+ if ( (e.metaKey || e.ctrlKey) && (!method || method === 'GET') && !data ) { return true; }
308
+
309
+ if (rails.handleRemote(link) === false) { rails.enableElement(link); }
310
+ return false;
311
+
312
+ } else if (link.data('method')) {
313
+ rails.handleMethod(link);
314
+ return false;
315
+ }
316
+ });
317
+
318
+ $(document).delegate(rails.inputChangeSelector, 'change.rails', function(e) {
319
+ var link = $(this);
320
+ if (!rails.allowAction(link)) return rails.stopEverything(e);
321
+
322
+ rails.handleRemote(link);
323
+ return false;
324
+ });
325
+
326
+ $(document).delegate(rails.formSubmitSelector, 'submit.rails', function(e) {
327
+ var form = $(this),
328
+ remote = form.data('remote') !== undefined,
329
+ blankRequiredInputs = rails.blankInputs(form, rails.requiredInputSelector),
330
+ nonBlankFileInputs = rails.nonBlankInputs(form, rails.fileInputSelector);
331
+
332
+ if (!rails.allowAction(form)) return rails.stopEverything(e);
333
+
334
+ // skip other logic when required values are missing or file upload is present
335
+ if (blankRequiredInputs && form.attr("novalidate") == undefined && rails.fire(form, 'ajax:aborted:required', [blankRequiredInputs])) {
336
+ return rails.stopEverything(e);
337
+ }
338
+
339
+ if (remote) {
340
+ if (nonBlankFileInputs) {
341
+ return rails.fire(form, 'ajax:aborted:file', [nonBlankFileInputs]);
342
+ }
343
+
344
+ // If browser does not support submit bubbling, then this live-binding will be called before direct
345
+ // bindings. Therefore, we should directly call any direct bindings before remotely submitting form.
346
+ if (!$.support.submitBubbles && $().jquery < '1.7' && rails.callFormSubmitBindings(form, e) === false) return rails.stopEverything(e);
347
+
348
+ rails.handleRemote(form);
349
+ return false;
350
+
351
+ } else {
352
+ // slight timeout so that the submit button gets properly serialized
353
+ setTimeout(function(){ rails.disableFormElements(form); }, 13);
354
+ }
355
+ });
356
+
357
+ $(document).delegate(rails.formInputClickSelector, 'click.rails', function(event) {
358
+ var button = $(this);
359
+
360
+ if (!rails.allowAction(button)) return rails.stopEverything(event);
361
+
362
+ // register the pressed submit button
363
+ var name = button.attr('name'),
364
+ data = name ? {name:name, value:button.val()} : null;
365
+
366
+ button.closest('form').data('ujs:submit-button', data);
367
+ });
368
+
369
+ $(document).delegate(rails.formSubmitSelector, 'ajax:beforeSend.rails', function(event) {
370
+ if (this == event.target) rails.disableFormElements($(this));
371
+ });
372
+
373
+ $(document).delegate(rails.formSubmitSelector, 'ajax:complete.rails', function(event) {
374
+ if (this == event.target) rails.enableFormElements($(this));
375
+ });
376
+
377
+ })( jQuery );
@@ -0,0 +1 @@
1
+ //
@@ -0,0 +1,3 @@
1
+ module Quredis
2
+ Version = VERSION = '0.5.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ <% unless items.empty? %>
2
+ <table class="table table-striped">
3
+ <thead>
4
+ <tr>
5
+ <th>Name</th>
6
+ <th>Ingress</th>
7
+ <th>Transit</th>
8
+ <th>Escape</th>
9
+ <th>Destroy</th>
10
+ </tr>
11
+ </thead>
12
+ <tbody>
13
+ <% for item in items %>
14
+ <tr>
15
+ <td><%=item['name']%></td>
16
+ <td><a href="/queue/<%=item['name']%>/ingress"><span class="badge badge-info"><%=item['ingress_count']%></span> <%=item['ingress']%></a></td>
17
+ <td><a href="/queue/<%=item['name']%>/transit"><span class="badge badge-info"><%=item['transit_count']%></span> <%=item['transit']%></a></td>
18
+ <td><a href="/queue/<%=item['name']%>/escape"><span class="badge badge-info"><%=item['escape_count']%></span> <%=item['escape']%></a></td>
19
+ <td><a class="btn btn-danger" href="/queue/<%=item['name']%>" data-confirm="About to remove this queue completely. Are you sure?" data-method="delete">Destroy</a></td>
20
+
21
+ <!-- <td><a class="btn btn-danger" href="/destroy/<%=item['name']%>" data-confirm="About to remove this queue completely. Are you sure?" data-method="delete" data-disable-with="Deleting" data-remote>Destroy</a></td>
22
+ --> </tr>
23
+ <% end %>
24
+ </tbody>
25
+ </table>
26
+ <% else %>
27
+ <p>No queues found</p>
28
+ <% end %>
@@ -0,0 +1,62 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Quredis Admin</title>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <meta name="description" content="">
8
+ <meta name="author" content="">
9
+
10
+ <!-- Le styles -->
11
+ <link href="/css/bootstrap.css" rel="stylesheet">
12
+ <style>
13
+ body {
14
+ padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
15
+ }
16
+ </style>
17
+ <link href="/css/bootstrap-responsive.css" rel="stylesheet">
18
+ <link href="/css/quredis.css" rel="stylesheet">
19
+ <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
20
+ <!--[if lt IE 9]>
21
+ <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
22
+ <![endif]-->
23
+
24
+ <!-- Le fav and touch icons -->
25
+ <link rel="shortcut icon" href="ico/favicon.ico">
26
+ <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
27
+ <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
28
+ <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
29
+ <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
30
+ </head>
31
+
32
+ <body>
33
+
34
+ <div class="navbar navbar-fixed-top">
35
+ <div class="navbar-inner">
36
+ <div class="container">
37
+ <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
38
+ <span class="icon-bar"></span>
39
+ <span class="icon-bar"></span>
40
+ <span class="icon-bar"></span>
41
+ </a>
42
+ <a class="brand" href="/">Quredis</a>
43
+ </div>
44
+ </div>
45
+ </div>
46
+
47
+ <div class="container">
48
+ <%= yield %>
49
+ <footer class="footer">
50
+ <p class="pull-right">Powered by <a href="http://github.com/cpatni/quredis" target="_blank">quredis</a></p>
51
+ </footer>
52
+ </div> <!-- /container -->
53
+
54
+ <!-- Le javascript
55
+ ================================================== -->
56
+ <!-- Placed at the end of the document so the pages load faster -->
57
+ <script src="/js/jquery.min.js"></script>
58
+ <script src="/js/bootstrap.min.js"></script>
59
+ <script src="/js/jquery_ujs.js"></script>
60
+ <script src="/js/quredis.js"></script>
61
+ </body>
62
+ </html>
@@ -0,0 +1,23 @@
1
+ <a class="btn btn-danger" href="/purge/<%=queue%>/<%=type%>" data-confirm="It will remove all the messages from this queue. Are you sure?" data-method="delete">Purge</a>
2
+ <% unless type == 'ingress'%>
3
+ <a class="btn btn-success" href="/retry/<%=queue%>/<%=type%>" data-confirm="It will re-enqueue all the messages to the ingress queue. Are you sure?" data-method="post">Retry</a>
4
+ <% end %>
5
+
6
+ <% unless items.empty? %>
7
+ <table class="table table-striped">
8
+ <thead>
9
+ <tr>
10
+ <th>Item</th>
11
+ </tr>
12
+ </thead>
13
+ <tbody>
14
+ <% for item in items %>
15
+ <tr>
16
+ <td><%=item%></td>
17
+ </tr>
18
+ <% end %>
19
+ </tbody>
20
+ </table>
21
+ <% else %>
22
+ <p>No items found</p>
23
+ <% end %>
@@ -0,0 +1,64 @@
1
+ require 'sinatra'
2
+ require 'json'
3
+ require 'redis'
4
+ require 'sinatra/base'
5
+ require 'quredis/admin'
6
+
7
+ module Quredis
8
+ class Web < Sinatra::Base
9
+
10
+ configure do
11
+ enable :method_override
12
+ enable :logging
13
+ #defer the evaluation so that redis_host and redis_port are set properly
14
+ set :admin, Proc.new {
15
+ redis_opts = {}
16
+ if settings.respond_to? :redis_host
17
+ redis_opts[:host] = settings.redis_host
18
+ end
19
+
20
+ if settings.respond_to? :redis_port
21
+ redis_opts[:port] = settings.redis_port
22
+ end
23
+ Quredis::Admin.new(redis_opts)
24
+ }
25
+ end
26
+
27
+
28
+ def admin
29
+ settings.admin
30
+ end
31
+
32
+ get '/' do
33
+ erb :index, :locals => admin.queues(params)
34
+ end
35
+
36
+ get '/queue/:name/:type' do |name, type|
37
+ erb :queue, :locals => admin.queue(name, type)
38
+ end
39
+
40
+ delete '/queue/:name' do |name|
41
+ admin.destroy_queue(name)
42
+ redirect '/'
43
+ end
44
+
45
+ delete '/purge/:name/:type' do |name, type|
46
+ admin.purge(name, type)
47
+ redirect '/'
48
+ end
49
+
50
+ post '/retry/:name/:type' do |name, type|
51
+ admin.retry(name, type)
52
+ redirect '/'
53
+ end
54
+
55
+ get '/api/queues' do
56
+ content_type :json
57
+ admin.queues(params).to_json
58
+ end
59
+
60
+ run! if app_file == $0
61
+
62
+ end
63
+ end
64
+
data/lib/quredis.rb ADDED
@@ -0,0 +1,121 @@
1
+ require "logger"
2
+ require 'redis'
3
+ require 'json'
4
+
5
+ module Quredis
6
+ attr_reader :name, :ingress, :transit, :escape
7
+
8
+ attr_reader :logger, :redis
9
+
10
+ def quredis(name, options = {}, &block)
11
+ @name = name
12
+ @ingress = options.fetch(:ingress, "ingress:#{name}")
13
+ @transit = options.fetch(:transit, "transit:#{name}")
14
+ @escape = options.fetch(:escape, "escape:#{name}")
15
+ @block = block
16
+ @redis_timeout = options.fetch(:redis_timeout, 0)
17
+ @enable_worker_info = options.fetch(:enable_worker_info, false)
18
+ end
19
+
20
+ def logger
21
+ @logger ||= Logger.new(STDOUT)
22
+ end
23
+
24
+ def redis
25
+ @redis ||= Redis.new
26
+ end
27
+
28
+ def host
29
+ `hostname`.strip
30
+ end
31
+
32
+ def pid
33
+ Process.pid
34
+ end
35
+
36
+ def worker_id
37
+ @worker_id ||= "#{host}:#{pid}:#{@name}"
38
+ end
39
+
40
+ def register
41
+ redis.multi do |multi|
42
+ multi.set("quredis:queue:#{name}", {
43
+ :name => name,
44
+ :ingress => ingress,
45
+ :transit => transit,
46
+ :escape => escape
47
+ }.to_json)
48
+ multi.zadd('quredis:queues', Time.now.to_i, name)
49
+ if @enable_worker_info
50
+ multi.hmset("quredis:worker:#{worker_id}", *{
51
+ :worker_id => worker_id,
52
+ :host => host,
53
+ :pid => pid,
54
+ :messages => 0,
55
+ :errors => 0,
56
+ :start_time => Time.now.to_i
57
+ }.to_a.flatten)
58
+ multi.sadd("quredis:queue:#{@name}:workers", worker_id)
59
+ multi.zadd('quredis:workers', Time.now.to_i, worker_id)
60
+ end
61
+ end
62
+ end
63
+
64
+ def start
65
+ connect unless redis
66
+ register
67
+ recover
68
+ logger.info "Quredis starting..."
69
+ logger.info "#{name} Queues: ingress -> #{ingress} transit -> #{transit} escape -> #{escape}"
70
+ loop do
71
+ connect unless redis
72
+ pipe
73
+ end
74
+ end
75
+
76
+ #Not intended to run if we are running clustered service
77
+ def recover
78
+ loop do
79
+ element = redis.rpoplpush(transit, escape)
80
+ if element
81
+ begin
82
+ logger.debug("recovering #{element}")
83
+ @block.call element
84
+ redis.lrem(escape, -1, element)
85
+ rescue Exception => e
86
+ logger.error e
87
+ end
88
+ else
89
+ break
90
+ end
91
+ end
92
+ end
93
+
94
+ def pipe
95
+ element = nil
96
+ begin
97
+ element = redis.brpoplpush(ingress, transit, @redis_timeout || 0)
98
+ if element
99
+ begin
100
+ logger.debug("dequeued #{element}")
101
+ @block.call element
102
+ rescue StandardError, Timeout::Error => e
103
+ logger.error e
104
+ replies = redis.multi do |multi|
105
+ multi.lrem(transit, -1, element)
106
+ multi.lpush(escape, element)
107
+ end
108
+ element = nil if replies
109
+ ensure
110
+ redis.lrem(transit, -1, element) if element
111
+ end
112
+ else
113
+ on_timeout if respond_to? :on_timeout
114
+ end
115
+ rescue Errno::EINTR => e
116
+ raise e
117
+ rescue StandardError, Timeout::Error => e
118
+ logger.error e
119
+ end
120
+ end
121
+ end