active_batch 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 (101) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/javascripts/active_batch/application.js +13 -0
  6. data/app/assets/javascripts/active_batch/batches.js +2 -0
  7. data/app/assets/javascripts/active_batch/jobs.js +2 -0
  8. data/app/assets/stylesheets/active_batch/application.css +15 -0
  9. data/app/assets/stylesheets/active_batch/batches.css +4 -0
  10. data/app/assets/stylesheets/active_batch/jobs.css +4 -0
  11. data/app/assets/stylesheets/scaffold.css +56 -0
  12. data/app/controllers/active_batch/application_controller.rb +4 -0
  13. data/app/controllers/active_batch/batches_controller.rb +49 -0
  14. data/app/controllers/active_batch/work_units_controller.rb +29 -0
  15. data/app/helpers/active_batch/application_helper.rb +4 -0
  16. data/app/helpers/active_batch/batches_helper.rb +4 -0
  17. data/app/helpers/active_batch/jobs_helper.rb +4 -0
  18. data/app/jobs/active_batch/batch_scheduler_job.rb +16 -0
  19. data/app/jobs/active_batch/batch_status_check_job.rb +17 -0
  20. data/app/models/active_batch/batch.rb +28 -0
  21. data/app/models/active_batch/work_unit.rb +17 -0
  22. data/app/views/active_batch/batches/_form.html.erb +21 -0
  23. data/app/views/active_batch/batches/index.html.erb +30 -0
  24. data/app/views/active_batch/batches/new.html.erb +5 -0
  25. data/app/views/active_batch/batches/show.html.erb +31 -0
  26. data/app/views/active_batch/work_units/index.html.erb +26 -0
  27. data/app/views/active_batch/work_units/show.html.erb +18 -0
  28. data/config/routes.rb +8 -0
  29. data/db/migrate/20150319101734_create_active_batch_tables.rb +21 -0
  30. data/lib/active_batch.rb +8 -0
  31. data/lib/active_batch/batched_job.rb +36 -0
  32. data/lib/active_batch/engine.rb +5 -0
  33. data/lib/active_batch/transactional_batched_job.rb +15 -0
  34. data/lib/active_batch/version.rb +3 -0
  35. data/lib/tasks/active_batch_tasks.rake +4 -0
  36. data/test/active_batch_test.rb +7 -0
  37. data/test/controllers/active_batch/batches_controller_test.rb +45 -0
  38. data/test/controllers/active_batch/work_units_controller_test.rb +32 -0
  39. data/test/dummy/README.rdoc +28 -0
  40. data/test/dummy/Rakefile +6 -0
  41. data/test/dummy/app/assets/javascripts/application.js +13 -0
  42. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  43. data/test/dummy/app/controllers/application_controller.rb +5 -0
  44. data/test/dummy/app/helpers/application_helper.rb +2 -0
  45. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  46. data/test/dummy/bin/bundle +3 -0
  47. data/test/dummy/bin/rails +4 -0
  48. data/test/dummy/bin/rake +4 -0
  49. data/test/dummy/bin/setup +29 -0
  50. data/test/dummy/config.ru +4 -0
  51. data/test/dummy/config/application.rb +26 -0
  52. data/test/dummy/config/boot.rb +5 -0
  53. data/test/dummy/config/database.yml +25 -0
  54. data/test/dummy/config/environment.rb +5 -0
  55. data/test/dummy/config/environments/development.rb +41 -0
  56. data/test/dummy/config/environments/production.rb +79 -0
  57. data/test/dummy/config/environments/test.rb +42 -0
  58. data/test/dummy/config/initializers/assets.rb +11 -0
  59. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  60. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  61. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  62. data/test/dummy/config/initializers/inflections.rb +16 -0
  63. data/test/dummy/config/initializers/mime_types.rb +4 -0
  64. data/test/dummy/config/initializers/session_store.rb +3 -0
  65. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  66. data/test/dummy/config/locales/en.yml +23 -0
  67. data/test/dummy/config/routes.rb +4 -0
  68. data/test/dummy/config/secrets.yml +22 -0
  69. data/test/dummy/db/schema.rb +34 -0
  70. data/test/dummy/db/test.sqlite3 +0 -0
  71. data/test/dummy/log/test.log +10238 -0
  72. data/test/dummy/public/404.html +67 -0
  73. data/test/dummy/public/422.html +67 -0
  74. data/test/dummy/public/500.html +66 -0
  75. data/test/dummy/public/favicon.ico +0 -0
  76. data/test/dummy/tmp/cache/assets/test/sprockets/7e439048c89c95bfde77609c4b119aef +0 -0
  77. data/test/dummy/tmp/cache/assets/test/sprockets/91621687e7dac71b8a566c4dbcecde96 +0 -0
  78. data/test/dummy/tmp/cache/assets/test/sprockets/9d1c7539b482b1c2ffa085bd26729895 +0 -0
  79. data/test/dummy/tmp/cache/assets/test/sprockets/b706477a353ac2248f013097788efb85 +0 -0
  80. data/test/dummy/tmp/cache/assets/test/sprockets/c23568af9c4b124555ced351478e21cd +0 -0
  81. data/test/dummy/tmp/cache/assets/test/sprockets/ca1c23ae80a4f7e2f04c1e9aa40d2d42 +0 -0
  82. data/test/dummy/tmp/cache/assets/test/sprockets/d8510a98424b7a23248a15c9151f0daf +0 -0
  83. data/test/dummy/tmp/cache/assets/test/sprockets/e00f432644dd66e5bd6ae31eafdd9511 +0 -0
  84. data/test/dummy/tmp/cache/assets/test/sprockets/ec9d2f32f08b260672f48c6e00ac3c8b +0 -0
  85. data/test/dummy/tmp/cache/assets/test/sprockets/ed1bbbbceac3d159210843f2da897105 +0 -0
  86. data/test/dummy/tmp/cache/assets/test/sprockets/f603fd5b82c54beb733ddc055fd6daf1 +0 -0
  87. data/test/dummy/tmp/cache/assets/test/sprockets/f762c0c4cbb8ace1fa3d7f760c74f6c8 +0 -0
  88. data/test/dummy/tmp/cache/assets/test/sprockets/f7afdd8c341387bba687dddcc3ee6d50 +0 -0
  89. data/test/dummy/tmp/cache/assets/test/sprockets/f9920f7a4f1a87f298d2df81feb570e5 +0 -0
  90. data/test/fixtures/active_batch/batches.yml +5 -0
  91. data/test/fixtures/active_batch/work_units.yml +5 -0
  92. data/test/integration/navigation_test.rb +10 -0
  93. data/test/jobs/active_batch/batch_scheduler_job_test.rb +41 -0
  94. data/test/jobs/active_batch/batch_status_check_job_test.rb +51 -0
  95. data/test/jobs/batch_job.rb +21 -0
  96. data/test/jobs/rescue_job.rb +9 -0
  97. data/test/lib/active_batch/batched_job_test.rb +21 -0
  98. data/test/models/active_batch/batch_test.rb +9 -0
  99. data/test/models/active_batch/work_unit_test.rb +19 -0
  100. data/test/test_helper.rb +21 -0
  101. metadata +236 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ one:
4
+ job_class: BatchJob
5
+ arguments: "[\"test\"]"
@@ -0,0 +1,5 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ one:
4
+ job_id: 1234
5
+ status: 0
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class NavigationTest < ActionDispatch::IntegrationTest
4
+ fixtures :all
5
+
6
+ # test "the truth" do
7
+ # assert true
8
+ # end
9
+ end
10
+
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveBatch
4
+ class BatchSchedulerJobTest < ActiveJob::TestCase
5
+ include ActiveJob::TestHelper
6
+
7
+ setup do
8
+ @batch_job = 'BatchJob'
9
+ @test_string = 'test'
10
+ end
11
+
12
+ test 'enqueues work unit jobs and check job' do
13
+ BatchSchedulerJob.perform_now(@batch_job, @test_string)
14
+ assert_enqueued_jobs(@test_string.length + 1)
15
+ end
16
+
17
+ test 'creates batch' do
18
+ assert_difference('WorkUnit.count', @test_string.length) do
19
+ BatchSchedulerJob.perform_now(@batch_job, @test_string)
20
+ end
21
+ end
22
+
23
+ test 'creates work units' do
24
+ assert_difference('Batch.count', 1) do
25
+ BatchSchedulerJob.perform_now(@batch_job, @test_string)
26
+ end
27
+ end
28
+
29
+ test 'batch has work units' do
30
+ job = BatchSchedulerJob.perform_now(@batch_job, @test_string)
31
+ batch = Batch.find_by(job_id: job.job_id)
32
+ assert_equal @test_string.length, batch.work_units.count
33
+ end
34
+
35
+ test 'work units have enqueued status' do
36
+ job = BatchSchedulerJob.perform_now(@batch_job, @test_string)
37
+ batch = Batch.find_by(job_id: job.job_id)
38
+ assert_equal @test_string.length, batch.work_units.enqueued.count
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveBatch
4
+ class BatchStatusCheckJobTest < ActiveJob::TestCase
5
+
6
+ fixtures :all
7
+
8
+ def random_result
9
+ rand(100).to_s
10
+ end
11
+
12
+ setup do
13
+ @first_result = random_result
14
+ @second_result = random_result
15
+ @batch = active_batch_batches(:one)
16
+ @work_unit_done = @batch.work_units.create(status: :done, work_result: @first_result)
17
+ @work_unit_running = @batch.work_units.create(status: :running, work_result: @second_result)
18
+ end
19
+
20
+ test 'reenqueues itself later if batch is not done' do
21
+ assert_enqueued_with(job: BatchStatusCheckJob, args: [@batch]) do
22
+ BatchStatusCheckJob.perform_now(@batch)
23
+ end
24
+ end
25
+
26
+ test 'uses the same queue than the work_units' do
27
+ assert_enqueued_with(job: BatchStatusCheckJob, queue: 'test_queue') do
28
+ BatchStatusCheckJob.perform_now(@batch)
29
+ end
30
+ end
31
+
32
+ test 'does not reenqueue itself later if batch is done' do
33
+ @work_unit_running.status = :done
34
+ @work_unit_running.save
35
+ assert_no_enqueued_jobs do
36
+ BatchStatusCheckJob.perform_now(@batch)
37
+ end
38
+ end
39
+
40
+ test 'calls after batch with proper args when batch is done' do
41
+ @work_unit_running.update!(status: :done)
42
+ BatchStatusCheckJob.perform_now(@batch)
43
+
44
+ assert File.exists?('/tmp/batch_job_result')
45
+ assert File.exists?('/tmp/batch_job_arguments')
46
+ assert_equal @batch.arguments.first, IO.read('/tmp/batch_job_arguments')
47
+ assert_equal [@first_result, @second_result].to_s, IO.read('/tmp/batch_job_result')
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ class BatchJob < ActiveJob::Base
2
+ include ActiveBatch::BatchedJob
3
+
4
+ queue_as :test_queue
5
+
6
+ def self.each_work_unit(string)
7
+ string.each_char do |char|
8
+ yield char
9
+ end
10
+ end
11
+
12
+ def self.after_batch(args, results)
13
+ File.open('/tmp/batch_job_arguments', 'w') { |f| f << args.first }
14
+ File.open('/tmp/batch_job_result', 'w') { |f| f << results }
15
+ end
16
+
17
+ def perform(char)
18
+ save_result(char)
19
+ end
20
+
21
+ end
@@ -0,0 +1,9 @@
1
+ class RescueJob < ActiveJob::Base
2
+ include ActiveBatch::BatchedJob
3
+
4
+ queue_as :test_queue
5
+
6
+ def perform
7
+ raise StandardError.new
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveBatch
4
+ class BatchedJobTest < ActiveSupport::TestCase
5
+
6
+ test 'can save work result' do
7
+ @batched_job = BatchJob.new
8
+ @work_unit = WorkUnit.create(job_id: @batched_job.job_id)
9
+ @batched_job.save_result('blah')
10
+ assert_equal 'blah', @work_unit.reload.work_result
11
+ end
12
+
13
+ test 'sets status to failed on exception' do
14
+ @rescue_job = RescueJob.new
15
+ @work_unit = WorkUnit.create(job_id: @rescue_job.job_id)
16
+ @rescue_job.perform_now rescue nil
17
+ assert @work_unit.reload.failed?
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveBatch
4
+ class BatchTest < ActiveSupport::TestCase
5
+ # test "the truth" do
6
+ # assert true
7
+ # end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveBatch
4
+ class JobTest < ActiveSupport::TestCase
5
+
6
+ setup do
7
+ @done_work_unit = WorkUnit.create(status: :done)
8
+ @running_work_unit = WorkUnit.create(status: :running)
9
+ @enqueued_work_unit = WorkUnit.create(status: :enqueued)
10
+ end
11
+
12
+ test 'not done scope returns work unit not done' do
13
+ assert_includes WorkUnit.not_done, @running_work_unit
14
+ assert_includes WorkUnit.not_done, @enqueued_work_unit
15
+ refute_includes WorkUnit.not_done, @done_work_unit
16
+ end
17
+
18
+ end
19
+ end