async_request 0.0.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/async_request/jobs_controller.rb +36 -6
  3. data/app/models/async_request/job.rb +49 -2
  4. data/app/poros/async_request/json_web_token.rb +26 -0
  5. data/app/workers/async_request/job_processor.rb +6 -6
  6. data/config/routes.rb +1 -1
  7. data/lib/async_request.rb +28 -0
  8. data/lib/async_request/engine.rb +2 -1
  9. data/lib/async_request/version.rb +1 -1
  10. data/lib/generators/async_request_generator.rb +6 -3
  11. data/lib/templates/async_request.rb +5 -0
  12. data/lib/templates/create_async_request_jobs.rb +1 -1
  13. data/spec/controllers/async_request/jobs_controller_spec.rb +80 -41
  14. data/spec/controllers/dummy_controller_spec.rb +49 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  16. data/spec/dummy/app/controllers/dummy_controller.rb +11 -0
  17. data/spec/dummy/app/workers/worker_returning_nil.rb +5 -0
  18. data/spec/dummy/app/workers/worker_with_errors.rb +8 -0
  19. data/spec/dummy/app/workers/worker_with_symbol.rb +5 -0
  20. data/spec/dummy/app/workers/worker_without_errors.rb +5 -0
  21. data/spec/dummy/bin/bundle +1 -0
  22. data/spec/dummy/bin/rails +1 -0
  23. data/spec/dummy/bin/rake +1 -0
  24. data/spec/dummy/bin/setup +9 -8
  25. data/spec/dummy/config/application.rb +1 -2
  26. data/spec/dummy/config/initializers/async_request.rb +5 -0
  27. data/spec/dummy/config/routes.rb +2 -2
  28. data/spec/dummy/db/migrate/20170815023204_create_async_request_jobs.rb +16 -0
  29. data/spec/dummy/db/schema.rb +1 -1
  30. data/spec/dummy/log/test.log +0 -5472
  31. data/spec/factories/async_request_job.rb +6 -0
  32. data/spec/models/async_request/job_spec.rb +40 -0
  33. data/spec/spec_helper.rb +1 -0
  34. data/spec/support/helpers.rb +5 -0
  35. data/spec/workers/async_request/job_processor_spec.rb +47 -17
  36. metadata +85 -54
  37. data/app/assets/javascripts/async_request/application.js +0 -13
  38. data/app/assets/stylesheets/async_request/application.css +0 -15
  39. data/app/helpers/async_request/application_helper.rb +0 -15
  40. data/lib/tasks/async_request_tasks.rake +0 -4
  41. data/spec/dummy/app/workers/test.rb +0 -7
  42. data/spec/dummy/log/development.log +0 -827
  43. data/spec/helpers/async_request/application_helper_spec.rb +0 -39
@@ -19,5 +19,11 @@ module AsyncRequest
19
19
  trait :processed do
20
20
  status Job.statuses[:processed]
21
21
  end
22
+
23
+ trait :failed do
24
+ status Job.statuses[:failed]
25
+ status_code 500
26
+ response { {} }
27
+ end
22
28
  end
23
29
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AsyncRequest::Job do
4
+ describe '.create_and_enqueue' do
5
+ let(:worker) { WorkerWithoutErrors }
6
+
7
+ context 'when no worker class is passed' do
8
+ it 'fails with ArgumentError' do
9
+ expect { described_class.create_and_enqueue(nil, []) }.to raise_error(ArgumentError)
10
+ end
11
+ end
12
+
13
+ context 'when called with valid params' do
14
+ it 'creates a new job object' do
15
+ expect { described_class.create_and_enqueue(worker, 'a') }
16
+ .to change { described_class.count }.by(1)
17
+ end
18
+
19
+ it 'creates a new job object with WorkerWithoutErrors as worker' do
20
+ job = described_class.create_and_enqueue(worker, 'a')
21
+ expect(job.worker).to eq worker.to_s
22
+ end
23
+
24
+ it "creates a new job object with ['a'] as params" do
25
+ job = described_class.create_and_enqueue(worker, 'a')
26
+ expect(job.params).to eq ['a']
27
+ end
28
+
29
+ it 'creates a new job object with waiting status' do
30
+ job = described_class.create_and_enqueue(worker, 'a')
31
+ expect(job.waiting?).to be_truthy
32
+ end
33
+
34
+ it 'serializes complex params' do
35
+ job = described_class.create_and_enqueue(worker, { a: 'a' }, 3, 'a')
36
+ expect(job.params).to eq [{ a: 'a' }, 3, 'a']
37
+ end
38
+ end
39
+ end
40
+ end
@@ -45,6 +45,7 @@ RSpec.configure do |config|
45
45
  end
46
46
 
47
47
  config.include Response::JSONParser, type: :controller
48
+ config.include Helpers
48
49
 
49
50
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
50
51
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
@@ -0,0 +1,5 @@
1
+ module Helpers
2
+ def job_id_from_token(token)
3
+ AsyncRequest::JsonWebToken.decode(token)['job_id']
4
+ end
5
+ end
@@ -2,29 +2,59 @@ require 'spec_helper'
2
2
 
3
3
  module AsyncRequest
4
4
  describe JobProcessor do
5
- include AsyncRequest::Engine.helpers
6
- describe '.perform' do
7
- let(:uid) { execute_async(Test, { a: 'a' }, 3, 'a') }
5
+ describe '#perform' do
6
+ let(:job) { AsyncRequest::Job.create_and_enqueue(worker, { a: 'a' }, 3, 'a') }
8
7
 
9
- before(:each) do
10
- Test.any_instance.stub(:execute).and_return([200, { status: 'success' }])
11
- end
8
+ context 'when executing the worker' do
9
+ context 'when it finishes without errors' do
10
+ let(:worker) { WorkerWithoutErrors }
11
+
12
+ it 'calls Test class' do
13
+ expect_any_instance_of(worker).to receive(:execute).with({ a: 'a' }, 3, 'a')
14
+ described_class.new.perform(job.id)
15
+ end
16
+
17
+ it 'saves the worker status code' do
18
+ described_class.new.perform(job.id)
19
+ expect(job.reload.status_code).to eq 200
20
+ end
12
21
 
13
- context 'When executing the worker' do
14
- it 'calls Test class' do
15
- expect_any_instance_of(Test).to receive(:execute).with({ a: 'a' }, 3, 'a')
16
- JobProcessor.new.perform(Job.find_by(uid: uid).id)
22
+ it 'saves the worker response' do
23
+ described_class.new.perform(job.id)
24
+ expect(job.reload.response).to eq({ 'message' => 'success' }.to_json)
25
+ end
17
26
  end
18
27
 
19
- it 'saves the worker status code' do
20
- JobProcessor.new.perform(Job.find_by(uid: uid).id)
21
- expect(Job.find_by(uid: uid).status_code).to eq 200
28
+ context 'when the worker returns a symbol as status code' do
29
+ let(:worker) { WorkerWithSymbol }
30
+
31
+ it 'saves the worker status code' do
32
+ described_class.new.perform(job.id)
33
+ expect(job.reload.status_code).to eq 200
34
+ end
22
35
  end
23
36
 
24
- it 'saves the worker response' do
25
- JobProcessor.new.perform(Job.find_by(uid: uid).id)
26
- response = Job.find_by(uid: uid).response
27
- expect(response).to eq({ status: 'success' }.to_json)
37
+ context 'when the worker returns nil as the response' do
38
+ let(:worker) { WorkerReturningNil }
39
+
40
+ it 'saves the worker status code' do
41
+ described_class.new.perform(job.id)
42
+ expect(job.reload.response).to eq ''
43
+ end
44
+ end
45
+
46
+ context 'when it raises an error' do
47
+ let(:worker) { WorkerWithErrors }
48
+
49
+ it 'updates the job with the right status' do
50
+ described_class.new.perform(job.id)
51
+ expect(job.reload.failed?).to be_truthy
52
+ end
53
+
54
+ it 'saves the job with a 500 as status code' do
55
+ described_class.new.perform(job.id)
56
+ expect(job.reload.status_code).to eq 500
57
+ end
28
58
  end
29
59
  end
30
60
  end
metadata CHANGED
@@ -1,14 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matias De Santi
8
+ - Alejandro Bezdjian
9
+ - Alan Halatian
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2018-02-15 00:00:00.000000000 Z
13
+ date: 2018-04-16 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
16
  name: rails
@@ -28,16 +30,36 @@ dependencies:
28
30
  name: sidekiq
29
31
  requirement: !ruby/object:Gem::Requirement
30
32
  requirements:
31
- - - "~>"
33
+ - - ">="
32
34
  - !ruby/object:Gem::Version
33
35
  version: '4.0'
36
+ - - "<"
37
+ - !ruby/object:Gem::Version
38
+ version: '6'
34
39
  type: :runtime
35
40
  prerelease: false
36
41
  version_requirements: !ruby/object:Gem::Requirement
37
42
  requirements:
38
- - - "~>"
43
+ - - ">="
39
44
  - !ruby/object:Gem::Version
40
45
  version: '4.0'
46
+ - - "<"
47
+ - !ruby/object:Gem::Version
48
+ version: '6'
49
+ - !ruby/object:Gem::Dependency
50
+ name: jwt
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.1'
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.1'
41
63
  - !ruby/object:Gem::Dependency
42
64
  name: pg
43
65
  requirement: !ruby/object:Gem::Requirement
@@ -140,34 +162,30 @@ dependencies:
140
162
  name: rubocop
141
163
  requirement: !ruby/object:Gem::Requirement
142
164
  requirements:
143
- - - ">="
165
+ - - "~>"
144
166
  - !ruby/object:Gem::Version
145
- version: '0'
167
+ version: '0.52'
146
168
  type: :development
147
169
  prerelease: false
148
170
  version_requirements: !ruby/object:Gem::Requirement
149
171
  requirements:
150
- - - ">="
172
+ - - "~>"
151
173
  - !ruby/object:Gem::Version
152
- version: '0'
153
- description: At [Wolox](http://www.wolox.com.ar) we build Rails Apps. Some of them
154
- need heavy computing when a request is received. In order to make the App scalable,
155
- we perform those heavy actions in background. We return a job-id and the client
156
- asks for it's state a few seconds after. `async_request` gives us the possibility
157
- of handling these type of requests in a simple way.
174
+ version: '0.52'
175
+ description: Perform background jobs and ask for the result in a simple way.
158
176
  email:
159
177
  - matias.desanti@wolox.com.ar
178
+ - alejandro.bezdjian@wolox.com.ar
179
+ - alan.halatian@wolox.com.ar
160
180
  executables: []
161
181
  extensions: []
162
182
  extra_rdoc_files: []
163
183
  files:
164
184
  - Rakefile
165
- - app/assets/javascripts/async_request/application.js
166
- - app/assets/stylesheets/async_request/application.css
167
185
  - app/controllers/async_request/application_controller.rb
168
186
  - app/controllers/async_request/jobs_controller.rb
169
- - app/helpers/async_request/application_helper.rb
170
187
  - app/models/async_request/job.rb
188
+ - app/poros/async_request/json_web_token.rb
171
189
  - app/views/layouts/async_request/application.html.erb
172
190
  - app/workers/async_request/job_processor.rb
173
191
  - config/routes.rb
@@ -175,17 +193,22 @@ files:
175
193
  - lib/async_request/engine.rb
176
194
  - lib/async_request/version.rb
177
195
  - lib/generators/async_request_generator.rb
178
- - lib/tasks/async_request_tasks.rake
196
+ - lib/templates/async_request.rb
179
197
  - lib/templates/create_async_request_jobs.rb
180
198
  - spec/controllers/async_request/jobs_controller_spec.rb
199
+ - spec/controllers/dummy_controller_spec.rb
181
200
  - spec/dummy/README.rdoc
182
201
  - spec/dummy/Rakefile
183
202
  - spec/dummy/app/assets/javascripts/application.js
184
203
  - spec/dummy/app/assets/stylesheets/application.css
185
204
  - spec/dummy/app/controllers/application_controller.rb
205
+ - spec/dummy/app/controllers/dummy_controller.rb
186
206
  - spec/dummy/app/helpers/application_helper.rb
187
207
  - spec/dummy/app/views/layouts/application.html.erb
188
- - spec/dummy/app/workers/test.rb
208
+ - spec/dummy/app/workers/worker_returning_nil.rb
209
+ - spec/dummy/app/workers/worker_with_errors.rb
210
+ - spec/dummy/app/workers/worker_with_symbol.rb
211
+ - spec/dummy/app/workers/worker_without_errors.rb
189
212
  - spec/dummy/bin/bundle
190
213
  - spec/dummy/bin/rails
191
214
  - spec/dummy/bin/rake
@@ -199,6 +222,7 @@ files:
199
222
  - spec/dummy/config/environments/production.rb
200
223
  - spec/dummy/config/environments/test.rb
201
224
  - spec/dummy/config/initializers/assets.rb
225
+ - spec/dummy/config/initializers/async_request.rb
202
226
  - spec/dummy/config/initializers/backtrace_silencers.rb
203
227
  - spec/dummy/config/initializers/cookies_serializer.rb
204
228
  - spec/dummy/config/initializers/filter_parameter_logging.rb
@@ -209,16 +233,17 @@ files:
209
233
  - spec/dummy/config/locales/en.yml
210
234
  - spec/dummy/config/routes.rb
211
235
  - spec/dummy/config/secrets.yml
236
+ - spec/dummy/db/migrate/20170815023204_create_async_request_jobs.rb
212
237
  - spec/dummy/db/schema.rb
213
- - spec/dummy/log/development.log
214
238
  - spec/dummy/log/test.log
215
239
  - spec/dummy/public/404.html
216
240
  - spec/dummy/public/422.html
217
241
  - spec/dummy/public/500.html
218
242
  - spec/dummy/public/favicon.ico
219
243
  - spec/factories/async_request_job.rb
220
- - spec/helpers/async_request/application_helper_spec.rb
244
+ - spec/models/async_request/job_spec.rb
221
245
  - spec/spec_helper.rb
246
+ - spec/support/helpers.rb
222
247
  - spec/support/parsed_response_helper.rb
223
248
  - spec/workers/async_request/job_processor_spec.rb
224
249
  homepage: https://github.com/Wolox/async-requests
@@ -244,50 +269,56 @@ rubyforge_project:
244
269
  rubygems_version: 2.5.1
245
270
  signing_key:
246
271
  specification_version: 4
247
- summary: async_request gives us the possibility of handling these type of requests
248
- in a simple way.
272
+ summary: Perform background jobs and ask for the result in a simple way.
249
273
  test_files:
250
- - spec/dummy/README.rdoc
251
- - spec/dummy/Rakefile
252
- - spec/dummy/config/application.rb
253
- - spec/dummy/config/environment.rb
254
- - spec/dummy/config/environments/test.rb
255
- - spec/dummy/config/environments/development.rb
256
- - spec/dummy/config/environments/production.rb
257
- - spec/dummy/config/locales/en.yml
274
+ - spec/spec_helper.rb
275
+ - spec/dummy/app/workers/worker_without_errors.rb
276
+ - spec/dummy/app/workers/worker_with_symbol.rb
277
+ - spec/dummy/app/workers/worker_returning_nil.rb
278
+ - spec/dummy/app/workers/worker_with_errors.rb
279
+ - spec/dummy/app/controllers/application_controller.rb
280
+ - spec/dummy/app/controllers/dummy_controller.rb
281
+ - spec/dummy/app/views/layouts/application.html.erb
282
+ - spec/dummy/app/assets/javascripts/application.js
283
+ - spec/dummy/app/assets/stylesheets/application.css
284
+ - spec/dummy/app/helpers/application_helper.rb
285
+ - spec/dummy/bin/rake
286
+ - spec/dummy/bin/setup
287
+ - spec/dummy/bin/bundle
288
+ - spec/dummy/bin/rails
289
+ - spec/dummy/config/secrets.yml
258
290
  - spec/dummy/config/routes.rb
291
+ - spec/dummy/config/locales/en.yml
292
+ - spec/dummy/config/environments/production.rb
293
+ - spec/dummy/config/environments/development.rb
294
+ - spec/dummy/config/environments/test.rb
295
+ - spec/dummy/config/environment.rb
296
+ - spec/dummy/config/application.rb
259
297
  - spec/dummy/config/database.yml
260
298
  - spec/dummy/config/boot.rb
261
- - spec/dummy/config/secrets.yml
262
299
  - spec/dummy/config/initializers/backtrace_silencers.rb
300
+ - spec/dummy/config/initializers/mime_types.rb
301
+ - spec/dummy/config/initializers/filter_parameter_logging.rb
263
302
  - spec/dummy/config/initializers/session_store.rb
264
303
  - spec/dummy/config/initializers/wrap_parameters.rb
265
- - spec/dummy/config/initializers/inflections.rb
266
- - spec/dummy/config/initializers/filter_parameter_logging.rb
267
- - spec/dummy/config/initializers/mime_types.rb
268
- - spec/dummy/config/initializers/cookies_serializer.rb
269
304
  - spec/dummy/config/initializers/assets.rb
270
- - spec/dummy/db/schema.rb
305
+ - spec/dummy/config/initializers/async_request.rb
306
+ - spec/dummy/config/initializers/cookies_serializer.rb
307
+ - spec/dummy/config/initializers/inflections.rb
271
308
  - spec/dummy/config.ru
272
- - spec/dummy/log/test.log
273
- - spec/dummy/log/development.log
274
- - spec/dummy/app/views/layouts/application.html.erb
275
- - spec/dummy/app/assets/stylesheets/application.css
276
- - spec/dummy/app/assets/javascripts/application.js
277
- - spec/dummy/app/workers/test.rb
278
- - spec/dummy/app/controllers/application_controller.rb
279
- - spec/dummy/app/helpers/application_helper.rb
280
- - spec/dummy/public/500.html
309
+ - spec/dummy/Rakefile
310
+ - spec/dummy/public/favicon.ico
281
311
  - spec/dummy/public/422.html
312
+ - spec/dummy/public/500.html
282
313
  - spec/dummy/public/404.html
283
- - spec/dummy/public/favicon.ico
284
- - spec/dummy/bin/bundle
285
- - spec/dummy/bin/setup
286
- - spec/dummy/bin/rails
287
- - spec/dummy/bin/rake
288
- - spec/spec_helper.rb
289
- - spec/workers/async_request/job_processor_spec.rb
290
- - spec/factories/async_request_job.rb
314
+ - spec/dummy/db/schema.rb
315
+ - spec/dummy/db/migrate/20170815023204_create_async_request_jobs.rb
316
+ - spec/dummy/log/test.log
317
+ - spec/dummy/README.rdoc
318
+ - spec/models/async_request/job_spec.rb
319
+ - spec/support/helpers.rb
291
320
  - spec/support/parsed_response_helper.rb
321
+ - spec/factories/async_request_job.rb
322
+ - spec/workers/async_request/job_processor_spec.rb
292
323
  - spec/controllers/async_request/jobs_controller_spec.rb
293
- - spec/helpers/async_request/application_helper_spec.rb
324
+ - spec/controllers/dummy_controller_spec.rb
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any styles
10
- * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
- * file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,15 +0,0 @@
1
- module AsyncRequest
2
- module ApplicationHelper
3
- def execute_async(worker_class, *params)
4
- raise ArgumentError if worker_class.nil?
5
- job = Job.create(
6
- worker: worker_class,
7
- params: params,
8
- status: Job.statuses[:waiting],
9
- uid: SecureRandom.uuid
10
- )
11
- JobProcessor.perform_async(job.id)
12
- job.uid
13
- end
14
- end
15
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :async_request do
3
- # # Task goes here
4
- # end
@@ -1,7 +0,0 @@
1
- class Test
2
-
3
- def execute(params)
4
- puts params
5
- [200, { message: 'you did it!' }]
6
- end
7
- end