resque-poll 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42a70fa63c42ea38dbe133b2d9d390691ec81281
4
+ data.tar.gz: 9f958909b5186766beb043b7903c5d612acff9b3
5
+ SHA512:
6
+ metadata.gz: 62d23d4d941f13cca8e7f85b51a4a86872b61cd3bf8eb48b31921e0a6b01055843b393d4c73afea63ff25c62756dde9c7d9071fe0b2fb8b07669ecdd3b78f19e
7
+ data.tar.gz: fdc12f80f6d37b60a961f3c460011f925245f1e19120c5bc32095887d1b40d0c6cef88c4cad3bec8f820ab1d2479c0809740076a7acf145197602759573cab61
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Lumos Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ [![Build Status](https://api.travis-ci.org/lumoslabs/resque-poll.png)](https://travis-ci.org/lumoslabs/resque-poll)
2
+
3
+ # resque-poll
4
+
5
+ resque-poll is a Rails engine that allows you to easily do long polling for Resque jobs. resque-poll depends on [resque-status](https://github.com/quirkey/resque-status).
6
+
7
+ ## Installation
8
+
9
+ Include resque-poll in your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'resque-poll'
13
+ ```
14
+
15
+ Mount the engine in your `routes.rb`:
16
+
17
+ ```ruby
18
+ mount ResquePoll::Engine => '/resque_poll'
19
+ ```
20
+
21
+ Require the javascript files & the dependencies inyour application.js javascript file (requires Asset Pipeline):
22
+
23
+ ```js
24
+ //= require jquery
25
+ //= require resque_poll
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ In a form, the `data-resque-poll` attribute to your form tag:
31
+
32
+ ```erb
33
+ <%= form_for @movie, html: { data: { 'resque-poll' => true } } do |f| %>
34
+ <%= f.input :title %>
35
+ <%= f.submit %>
36
+ ```
37
+
38
+ Your controller can then use ResquePoll to return the appropriate response to begin polling:
39
+
40
+ ```ruby
41
+ class MoviesController < ApplicationController
42
+ def create
43
+ poll = ResquePoll.create(LongRunningMoveJob, {title: params[:title]})
44
+ render json: poll.to_json
45
+ end
46
+ end
47
+ ```
48
+
49
+ resque-poll will fire events when the polling has completed (successfully or unsuccessfully). To make use of this, you can hook into the triggered events and take action on them. Any data passed back from the resque job will also be available to you in the response.
50
+
51
+ The following events are fired:
52
+
53
+ * `resque:poll:stopped` - fired when processing has finished successfully or unsuccessfully
54
+ * `resque:poll:success` - fired when processing has finished successfully
55
+ * `resque:poll:stopped` - fired when processing has finished unsuccessfully
56
+
57
+ Continuing with our form from above:
58
+
59
+ ```javascript
60
+ $('form')
61
+ .bind('resque:poll:stopped', function(event, response) {
62
+ console.log('Processing finished!');
63
+ }).bind('resque:poll:success', function(event, response) {
64
+ console.log('Processing successful!');
65
+ }).bind('resque:poll:error', function(event, response) {
66
+ console.log('Processing failed!');
67
+ });
68
+ ```
69
+
70
+ Since a common use case would be to enable/disable the submit button during the course of processing the background job, the `resque-poll-disable-with` attribute can be used to automatically disable/enable a button with a given value when processing has begun or completed.
71
+
72
+ ```erb
73
+ <%= f.submit :'data-resque-poll-disable-with' => 'Creating...' %>
74
+ ```
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ResquePoll'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
@@ -0,0 +1,5 @@
1
+ #= require_tree .
2
+
3
+ $ ->
4
+ $(document).on 'ajax:success', 'form[data-resque-poll]', (data, status, xhr) ->
5
+ new ResquePoller(elem: $(this), url: status.poll) if status.poll
@@ -0,0 +1,38 @@
1
+ class ResquePoller
2
+ @INTERVAL: 2000
3
+
4
+ constructor: (opts) ->
5
+ @$elem = opts.elem
6
+ @url = opts.url
7
+ @intervalID = setInterval(@_poll, opts.interval || ResquePoller.INTERVAL)
8
+
9
+ # private
10
+
11
+ _poll: => $.getJSON @url, (resp) => @_handleResponse(resp)
12
+
13
+ _handleResponse: (resp) ->
14
+ return if resp.status is 'queued'
15
+ clearInterval @intervalID if @intervalID
16
+ switch resp.status
17
+ when 'completed'
18
+ @$elem.trigger 'resque:poll:stopped', resp
19
+ @$elem.trigger 'resque:poll:success', resp
20
+ when 'failed'
21
+ @$elem.trigger 'resque:poll:stopped', resp
22
+ @$elem.trigger 'resque:poll:error', resp
23
+
24
+ window.ResquePoller = ResquePoller
25
+
26
+ $ ->
27
+ $(document).on 'ajax:before', 'form[data-resque-poll]', ->
28
+ $(this).find('[data-resque-poll-disable-with]').each (i) ->
29
+ $(this)
30
+ .data('resque-poll-enable-with', $(this).val())
31
+ .attr('disabled', 'disabled')
32
+ .attr('value', $(this).data('resque-poll-disable-with'))
33
+
34
+ $(document).on 'resque:poll:stopped', 'form[data-resque-poll]', ->
35
+ $(this).find('[data-resque-poll-disable-with]').each (i) ->
36
+ $(this)
37
+ .removeAttr('disabled')
38
+ .attr('value', $(this).data('resque-poll-enable-with'))
@@ -0,0 +1,5 @@
1
+ module ResquePoll
2
+ class ApplicationController < ActionController::Base
3
+ include ::SslRequirement
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ class ResquePoll::JobsController < ResquePoll::ApplicationController
2
+ respond_to :json
3
+
4
+ ssl_allowed :show
5
+
6
+ def show
7
+ status = Resque::Plugins::Status::Hash.get(params.require(:id))
8
+ if status.nil?
9
+ head :not_found
10
+ else
11
+ render json: status, status: :ok
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ class ResquePoll::Job
2
+ extend ActiveModel::Naming
3
+
4
+ attr_reader :klass, :params, :job_id
5
+
6
+ def initialize(klass, params)
7
+ @klass = klass
8
+ @params = params
9
+ end
10
+
11
+ def self.create(*args)
12
+ new(*args).create
13
+ end
14
+
15
+ def create
16
+ @job_id = klass.create(params)
17
+ self
18
+ end
19
+
20
+ def as_json(options = {})
21
+ {poll: job_path}
22
+ end
23
+
24
+ private
25
+
26
+ def job_path
27
+ job_id ? ResquePoll::Engine.routes.url_helpers.job_path(id: job_id) : nil
28
+ end
29
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ ResquePoll::Engine.routes.draw do
2
+ resources :jobs, only: :show
3
+ end
@@ -0,0 +1,5 @@
1
+ require "resque_poll/engine"
2
+ require "ssl_requirement"
3
+
4
+ module ResquePoll
5
+ end
@@ -0,0 +1,5 @@
1
+ module ResquePoll
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ResquePoll
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ResquePoll
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :resque-poll do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-poll
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Zacharakis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bartt-ssl_requirement
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.15
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.15
41
+ - !ruby/object:Gem::Dependency
42
+ name: resque-status
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: strong_parameters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fakeredis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '2.14'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '2.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A resque-based web poller
126
+ email:
127
+ - anthony@lumoslabs.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.md
134
+ - Rakefile
135
+ - app/assets/javascripts/resque_poll/index.coffee
136
+ - app/assets/javascripts/resque_poll/poller.coffee
137
+ - app/controllers/resque_poll/application_controller.rb
138
+ - app/controllers/resque_poll/jobs_controller.rb
139
+ - app/models/resque_poll/job.rb
140
+ - config/routes.rb
141
+ - lib/resque-poll.rb
142
+ - lib/resque_poll/engine.rb
143
+ - lib/resque_poll/version.rb
144
+ - lib/tasks/resque-poll_tasks.rake
145
+ homepage: https://www.lumosity.com
146
+ licenses: []
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.4.1
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: A resque-based web poller
168
+ test_files: []
169
+ has_rdoc: