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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb25cd2885f557fea859f6e816317e78312656a6
4
+ data.tar.gz: 7aacc6c82c3dac0e0607f0e77fc8e9543980bea4
5
+ SHA512:
6
+ metadata.gz: c7795566421a69f86f759c1c0ee6067f3d8e1aa929c19e702f8d57ce0c275941eb96a41b80149490ca4fe8f35aba62b6c16d0f253e381859f8f456ddce261881
7
+ data.tar.gz: e2a3fa90cf85b1f3c8fd499b4ee9612a414db4c457568301cea420011d8c7473a8d3878537d45e7f57a46d952b83b0ad67232591fe636367e47bd37ac3fa252d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 adrien
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = ActiveBatch
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActiveBatch'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,13 @@
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/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,15 @@
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
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }
@@ -0,0 +1,4 @@
1
+ module ActiveBatch
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,49 @@
1
+ require_dependency "active_batch/application_controller"
2
+
3
+ module ActiveBatch
4
+ class BatchesController < ApplicationController
5
+ before_action :set_batch, only: [:show, :destroy]
6
+
7
+ # GET /batches
8
+ def index
9
+ @batches = Batch.all
10
+ end
11
+
12
+ # GET /batches/1
13
+ def show
14
+ end
15
+
16
+ # GET /batches/new
17
+ def new
18
+ @batch = Batch.new
19
+ end
20
+
21
+ # POST /batches
22
+ def create
23
+ @batch = Batch.new(batch_params)
24
+
25
+ if @batch.save
26
+ redirect_to @batch, notice: 'Batch was successfully created.'
27
+ else
28
+ render :new
29
+ end
30
+ end
31
+
32
+ # DELETE /batches/1
33
+ def destroy
34
+ @batch.destroy
35
+ redirect_to batches_url, notice: 'Batch was successfully destroyed.'
36
+ end
37
+
38
+ private
39
+ # Use callbacks to share common setup or constraints between actions.
40
+ def set_batch
41
+ @batch = Batch.find(params[:id])
42
+ end
43
+
44
+ # Only allow a trusted parameter "white list" through.
45
+ def batch_params
46
+ params.require(:batch).permit(:job_class, :timestamps)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,29 @@
1
+ require_dependency "active_batch/application_controller"
2
+
3
+ module ActiveBatch
4
+ class WorkUnitsController < ApplicationController
5
+ before_action :set_work_unit, only: [:show, :destroy]
6
+
7
+ # GET /jobs
8
+ def index
9
+ @work_units = WorkUnit.all
10
+ end
11
+
12
+ # GET /jobs/1
13
+ def show
14
+ end
15
+
16
+ # DELETE /jobs/1
17
+ def destroy
18
+ @work_unit.destroy
19
+ redirect_to work_units_url, notice: 'Job was successfully destroyed.'
20
+ end
21
+
22
+ private
23
+ # Use callbacks to share common setup or constraints between actions.
24
+ def set_work_unit
25
+ @work_unit = WorkUnit.find(params[:id])
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveBatch
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveBatch
2
+ module BatchesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveBatch
2
+ module JobsHelper
3
+ end
4
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveBatch
2
+ class BatchSchedulerJob < ActiveJob::Base
3
+
4
+ queue_as ActiveBatch.batch_scheduling_queue || :default
5
+
6
+ def perform(job_class, *args)
7
+ batch = Batch.create(job_class: job_class, job_id: job_id, arguments: args)
8
+ batch.job.each_work_unit(*args) do |*work_unit_args|
9
+ work_unit_job = batch.job.perform_later(*work_unit_args)
10
+ batch.work_units.create(job_id: work_unit_job.job_id)
11
+ end
12
+ BatchStatusCheckJob.perform_later(batch)
13
+ self
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveBatch
2
+ class BatchStatusCheckJob < ActiveJob::Base
3
+
4
+ queue_as do
5
+ batch = arguments.first
6
+ batch.job.new.queue_name
7
+ end
8
+
9
+ def perform(batch)
10
+ if batch.work_units.not_done.exists?
11
+ self.class.set(wait: 1.minute).perform_later(batch)
12
+ else
13
+ batch.perform_after_batch
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ require 'json'
2
+
3
+ module ActiveBatch
4
+ class Batch < ActiveRecord::Base
5
+ include ActiveJob::Arguments
6
+
7
+ has_many :work_units, foreign_key: 'active_batch_batches_id'
8
+
9
+ enum status: %i(open closed)
10
+
11
+ def perform_after_batch
12
+ job.after_batch(*arguments, work_units.map(&:work_result))
13
+ update!(status: :closed)
14
+ end
15
+
16
+ def arguments=(arguments)
17
+ write_attribute(:arguments, serialize(arguments).to_json)
18
+ end
19
+
20
+ def arguments
21
+ deserialize(JSON.parse(read_attribute(:arguments)))
22
+ end
23
+
24
+ def job
25
+ @job ||= job_class.constantize
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveBatch
2
+ class WorkUnit < ActiveRecord::Base
3
+
4
+ enum status: [:enqueued, :running, :done, :failed]
5
+
6
+ scope :not_done, -> { where.not(status: statuses[:done])}
7
+
8
+ belongs_to :batch, foreign_key: 'active_batch_batches_id'
9
+
10
+ def error(exception)
11
+ self.update!(
12
+ status: :failed,
13
+ work_result: "#{exception.message}\n#{exception.backtrace.join("\n")}"
14
+ )
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ <%= form_for(@batch) do |f| %>
2
+ <% if @batch.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@batch.errors.count, "error") %> prohibited this batch from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @batch.errors.full_messages.each do |message| %>
8
+ <li><%= message %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :job_class %><br>
16
+ <%= f.text_field :job_class %>
17
+ </div>
18
+ <div class="actions">
19
+ <%= f.submit %>
20
+ </div>
21
+ <% end %>
@@ -0,0 +1,30 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <h1>Listing Batches</h1>
4
+
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <th>Job class</th>
9
+ <th>Created at</th>
10
+ <th>Status</th>
11
+ <th colspan="2"></th>
12
+ </tr>
13
+ </thead>
14
+
15
+ <tbody>
16
+ <% @batches.each do |batch| %>
17
+ <tr>
18
+ <td><%= batch.job_class %></td>
19
+ <td><%= batch.created_at %></td>
20
+ <td><%= batch.status %></td>
21
+ <td><%= link_to 'Show', batch %></td>
22
+ <td><%= link_to 'Destroy', batch, method: :delete, data: { confirm: 'Are you sure?' } %></td>
23
+ </tr>
24
+ <% end %>
25
+ </tbody>
26
+ </table>
27
+
28
+ <br>
29
+
30
+ <%= link_to 'New Batch', new_batch_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New Batch</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', batches_path %>
@@ -0,0 +1,31 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <strong>Job class:</strong>
5
+ <%= @batch.job_class %>
6
+ </p>
7
+
8
+ <p>
9
+ <strong>Status</strong>
10
+ <%= @batch.status %>
11
+ </p>
12
+
13
+ <table>
14
+ <thead>
15
+ <tr>
16
+ <th>job_id</th>
17
+ <th>status</th>
18
+ <th>result</th>
19
+ </tr>
20
+ </thead>
21
+ <tbody>
22
+ <% @batch.work_units.each do |work_unit| %>
23
+ <tr>
24
+ <td><%= work_unit.job_id %></td>
25
+ <td><%= work_unit.status %></td>
26
+ <td><%= work_unit.work_result %></td>
27
+ </tr>
28
+ <% end %>
29
+ </tbody>
30
+ </table>
31
+ <%= link_to 'Back', batches_path %>