stateful_jobs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/.gitignore +18 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +147 -0
  5. data/Rakefile +2 -0
  6. data/app/assets/javascripts/stateful_jobs.js.coffee +65 -0
  7. data/lib/stateful_jobs.rb +14 -0
  8. data/lib/stateful_jobs/controller.rb +41 -0
  9. data/lib/stateful_jobs/engine.rb +11 -0
  10. data/lib/stateful_jobs/job.rb +9 -0
  11. data/lib/stateful_jobs/job/base.rb +22 -0
  12. data/lib/stateful_jobs/job/dispatcher.rb +31 -0
  13. data/lib/stateful_jobs/model.rb +36 -0
  14. data/lib/stateful_jobs/routing.rb +59 -0
  15. data/lib/stateful_jobs/version.rb +3 -0
  16. data/lib/stateful_jobs/view_helpers.rb +29 -0
  17. data/spec/controllers/test_runs_controller_spec.rb +47 -0
  18. data/spec/dummy/Rakefile +7 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  20. data/spec/dummy/app/assets/javascripts/test.js +2 -0
  21. data/spec/dummy/app/assets/javascripts/tests.js +2 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/spec/dummy/app/assets/stylesheets/test.css +4 -0
  24. data/spec/dummy/app/assets/stylesheets/tests.css +4 -0
  25. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  26. data/spec/dummy/app/controllers/test_runs_controller.rb +7 -0
  27. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  28. data/spec/dummy/app/helpers/tests_helper.rb +2 -0
  29. data/spec/dummy/app/jobs/execute_job.rb +6 -0
  30. data/spec/dummy/app/mailers/.gitkeep +0 -0
  31. data/spec/dummy/app/models/.gitkeep +0 -0
  32. data/spec/dummy/app/models/test_run.rb +10 -0
  33. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/spec/dummy/app/views/test/index.html.erb +2 -0
  35. data/spec/dummy/config.ru +4 -0
  36. data/spec/dummy/config/application.rb +59 -0
  37. data/spec/dummy/config/boot.rb +10 -0
  38. data/spec/dummy/config/database.yml +25 -0
  39. data/spec/dummy/config/environment.rb +5 -0
  40. data/spec/dummy/config/environments/development.rb +37 -0
  41. data/spec/dummy/config/environments/production.rb +67 -0
  42. data/spec/dummy/config/environments/test.rb +37 -0
  43. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/spec/dummy/config/initializers/inflections.rb +15 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  46. data/spec/dummy/config/initializers/resque.rb +1 -0
  47. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  48. data/spec/dummy/config/initializers/session_store.rb +8 -0
  49. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  50. data/spec/dummy/config/locales/en.yml +5 -0
  51. data/spec/dummy/config/routes.rb +61 -0
  52. data/spec/dummy/db/migrate/20130228143735_create_test_runs.rb +10 -0
  53. data/spec/dummy/db/schema.rb +23 -0
  54. data/spec/dummy/lib/assets/.gitkeep +0 -0
  55. data/spec/dummy/log/.gitkeep +0 -0
  56. data/spec/dummy/public/404.html +26 -0
  57. data/spec/dummy/public/422.html +26 -0
  58. data/spec/dummy/public/500.html +25 -0
  59. data/spec/dummy/public/favicon.ico +0 -0
  60. data/spec/dummy/script/rails +6 -0
  61. data/spec/lib/stateful_jobs_job_dispatcher_spec.rb +126 -0
  62. data/spec/lib/stateful_jobs_job_spec.rb +23 -0
  63. data/spec/models/test_run_spec.rb +77 -0
  64. data/spec/spec_helper.rb +35 -0
  65. data/stateful_jobs.gemspec +36 -0
  66. metadata +246 -0
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
11
+ #
12
+ # These inflection rules are supported but not enabled by default:
13
+ # ActiveSupport::Inflector.inflections do |inflect|
14
+ # inflect.acronym 'RESTful'
15
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1 @@
1
+ Resque.inline = Rails.env.development? || Rails.env.test?
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'dffa2898f783621c644616398999268745032a940fe9d4c91ca463213f9ae3819514010614a278c448d4d525bdc44f295f71f4d45c1ba56a369384a715df11ba'
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4
+
5
+ # Use the database for sessions instead of the cookie-based default,
6
+ # which shouldn't be used to store highly confidential information
7
+ # (create the session table with "rails generate session_migration")
8
+ # Dummy::Application.config.session_store :active_record_store
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
@@ -0,0 +1,61 @@
1
+ Dummy::Application.routes.draw do
2
+
3
+ stateful_jobs :test_runs
4
+
5
+ # The priority is based upon order of creation:
6
+ # first created -> highest priority.
7
+
8
+ # Sample of regular route:
9
+ # match 'products/:id' => 'catalog#view'
10
+ # Keep in mind you can assign values other than :controller and :action
11
+
12
+ # Sample of named route:
13
+ # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
14
+ # This route can be invoked with purchase_url(:id => product.id)
15
+
16
+ # Sample resource route (maps HTTP verbs to controller actions automatically):
17
+ # resources :products
18
+
19
+ # Sample resource route with options:
20
+ # resources :products do
21
+ # member do
22
+ # get 'short'
23
+ # post 'toggle'
24
+ # end
25
+ #
26
+ # collection do
27
+ # get 'sold'
28
+ # end
29
+ # end
30
+
31
+ # Sample resource route with sub-resources:
32
+ # resources :products do
33
+ # resources :comments, :sales
34
+ # resource :seller
35
+ # end
36
+
37
+ # Sample resource route with more complex sub-resources
38
+ # resources :products do
39
+ # resources :comments
40
+ # resources :sales do
41
+ # get 'recent', :on => :collection
42
+ # end
43
+ # end
44
+
45
+ # Sample resource route within a namespace:
46
+ # namespace :admin do
47
+ # # Directs /admin/products/* to Admin::ProductsController
48
+ # # (app/controllers/admin/products_controller.rb)
49
+ # resources :products
50
+ # end
51
+
52
+ # You can have the root of your site routed with "root"
53
+ # just remember to delete public/index.html.
54
+ # root :to => 'welcome#index'
55
+
56
+ # See how all your routes lay out with "rake routes"
57
+
58
+ # This is a legacy wild controller route that's not recommended for RESTful applications.
59
+ # Note: This route will make all actions in every controller accessible via GET requests.
60
+ # match ':controller(/:action(/:id))(.:format)'
61
+ end
@@ -0,0 +1,10 @@
1
+ class CreateTestRuns < ActiveRecord::Migration
2
+ def change
3
+ create_table :test_runs do |t|
4
+ t.string :current_job
5
+ t.string :current_state
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20130228143735) do
15
+
16
+ create_table "test_runs", :force => true do |t|
17
+ t.string "current_job"
18
+ t.string "current_state"
19
+ t.datetime "created_at", :null => false
20
+ t.datetime "updated_at", :null => false
21
+ end
22
+
23
+ end
File without changes
File without changes
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+
3
+ class TestJob < StatefulJobs::Job::Base
4
+ end
5
+
6
+ describe StatefulJobs::Job::Dispatcher do
7
+
8
+ describe '#perform' do
9
+ before :each do
10
+ @test_run = TestRun.create
11
+ TestRun.stub(:find).and_return(@test_run)
12
+ end
13
+
14
+ context 'with job class' do
15
+ before :each do
16
+ TestRun.stateful_jobs = {}
17
+ TestRun.stateful_job :test_job, TestJob
18
+ end
19
+
20
+ it 'calls the jobs perform method' do
21
+ TestJob.should_receive(:perform).with(@test_run)
22
+
23
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
24
+ end
25
+
26
+ it 'sets the current job' do
27
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
28
+
29
+ @test_run.reload
30
+ @test_run.current_job.should eql('test_job')
31
+ end
32
+
33
+ it 'sets the done state on successful jobs' do
34
+ TestJob.should_receive(:perform).with(@test_run).and_return true
35
+
36
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
37
+
38
+ TestRun.unstub(:find)
39
+ @test_run.reload
40
+
41
+ @test_run.current_state.should eql('done')
42
+ end
43
+
44
+ it 'sets the failure state on non successful jobs' do
45
+ TestJob.should_receive(:perform).with(@test_run).and_return false
46
+
47
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
48
+
49
+ TestRun.unstub(:find)
50
+ @test_run.reload
51
+
52
+ @test_run.current_state.should eql('failed')
53
+ end
54
+
55
+ it 'sets the error state when exception was thrown' do
56
+ TestJob.should_receive(:perform).with(@test_run).and_raise Exception
57
+
58
+ expect {
59
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
60
+ }.to raise_error
61
+
62
+ TestRun.unstub(:find)
63
+ @test_run.reload
64
+
65
+ @test_run.current_state.should eql('error')
66
+ end
67
+ end
68
+
69
+ context 'with job proc' do
70
+ before :each do
71
+ TestRun.stateful_jobs = {}
72
+ TestRun.stateful_job :test_job do
73
+ end
74
+ end
75
+
76
+ it 'calls the jobs proc' do
77
+ TestRun.stateful_jobs[:test_job].should_receive(:call).with(@test_run)
78
+
79
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
80
+ end
81
+
82
+ it 'sets the current job' do
83
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
84
+
85
+ @test_run.reload
86
+ @test_run.current_job.should eql('test_job')
87
+ end
88
+
89
+ it 'sets the done state on successful jobs' do
90
+ TestRun.stateful_jobs[:test_job].should_receive(:call).with(@test_run).and_return true
91
+
92
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
93
+
94
+ TestRun.unstub(:find)
95
+ @test_run.reload
96
+
97
+ @test_run.current_state.should eql('done')
98
+ end
99
+
100
+ it 'sets the failure state on non successful jobs' do
101
+ TestRun.stateful_jobs[:test_job].should_receive(:call).with(@test_run).and_return false
102
+
103
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
104
+
105
+ TestRun.unstub(:find)
106
+ @test_run.reload
107
+
108
+ @test_run.current_state.should eql('failed')
109
+ end
110
+
111
+ it 'sets the error state when exception was thrown' do
112
+ TestRun.stateful_jobs[:test_job].should_receive(:call).with(@test_run).and_raise Exception
113
+
114
+ expect {
115
+ StatefulJobs::Job::Dispatcher.perform 'TestRun', @test_run.id, 'test_job'
116
+ }.to raise_error
117
+
118
+ TestRun.unstub(:find)
119
+ @test_run.reload
120
+
121
+ @test_run.current_state.should eql('error')
122
+ end
123
+ end
124
+ end
125
+
126
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ class TestJob < StatefulJobs::Job::Base
4
+ end
5
+
6
+ describe StatefulJobs::Job do
7
+
8
+ describe '#enqueue' do
9
+ before :each do
10
+ TestRun.stateful_jobs = {}
11
+ TestRun.stateful_job :test_job, TestJob
12
+
13
+ @test_run = TestRun.create
14
+ end
15
+
16
+ it 'enqueues the Dispatcher with given job on resque' do
17
+ Resque.should_receive(:enqueue).with(StatefulJobs::Job::Dispatcher, 'TestRun', @test_run.id, :test_job)
18
+
19
+ StatefulJobs::Job.enqueue @test_run, :test_job
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ class TestJob < StatefulJobs::Job::Base
4
+ end
5
+
6
+ describe TestRun do
7
+
8
+ describe 'model' do
9
+ it 'implements the stateful_job class method' do
10
+ TestRun.methods.should include(:stateful_job)
11
+ end
12
+
13
+ it 'implements the stateful_job_for class method' do
14
+ TestRun.methods.should include(:stateful_job_for)
15
+ end
16
+ end
17
+
18
+ describe '#stateful_job' do
19
+ before :each do
20
+ TestRun.stateful_jobs = {}
21
+ end
22
+
23
+ context 'with block given' do
24
+ it 'adds the job to its jobs hash' do
25
+ TestRun.stateful_job :test_job do
26
+ end
27
+
28
+ TestRun.stateful_jobs.should have_key(:test_job)
29
+ TestRun.stateful_jobs[:test_job].should be_a(Proc)
30
+ end
31
+
32
+ it 'defines a ! method for enqueuing the job' do
33
+ TestRun.instance_methods.should include(:test_job!)
34
+ end
35
+ end
36
+
37
+ context 'with block given' do
38
+ it 'adds the job to its jobs hash' do
39
+ TestRun.stateful_job :test_job, TestJob
40
+
41
+ TestRun.stateful_jobs.should have_key(:test_job)
42
+ TestRun.stateful_jobs[:test_job].should eql(TestJob)
43
+ end
44
+
45
+ it 'defines a ! method for enqueuing the job' do
46
+ TestRun.instance_methods.should include(:test_job!)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#stateful_job_for' do
52
+ before :each do
53
+ TestRun.stateful_jobs = {}
54
+
55
+ TestRun.stateful_job :test_job, TestJob
56
+ end
57
+
58
+ it 'returns the job fo given key' do
59
+ TestRun.stateful_job_for(:test_job).should eql(TestJob)
60
+ end
61
+ end
62
+
63
+ describe 'enqueuing jobs' do
64
+ before :each do
65
+ TestRun.stateful_jobs = {}
66
+ TestRun.stateful_job :test_job, TestJob
67
+
68
+ @test_run = TestRun.create
69
+ end
70
+
71
+ it 'enqueues the job' do
72
+ StatefulJobs::Job.should_receive(:enqueue).with(@test_run, :test_job)
73
+
74
+ @test_run.test_job!
75
+ end
76
+ end
77
+ end