jobler 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8a34dc95e9f05a8e717f4e003238c90982cac21
4
- data.tar.gz: 61c2b0d82a6d344784f06aee51f94eb3a46787f9
3
+ metadata.gz: 449bec194fa9aa9bc0a4e57ab17a5998b479c4b4
4
+ data.tar.gz: d97d7f6c3a7800265a5907508c29cb83275b2be4
5
5
  SHA512:
6
- metadata.gz: d3a46fbed9c8c2593a48427e748760aeaa6ad6d8a9dd8c2ff9795ea271ef68f158dc4bd00ddb712c0c23bee2026116ee4ff802f93152a91e0f15f6e86c4bd13f
7
- data.tar.gz: 2c54a5329bed09ada71f73e266929ea5881b89c4dfedaba04c9e927205c2d7a5ae6653eb732043c09aa480780e5c1c01ec720208933efbe647f6630a445aebcc
6
+ metadata.gz: 3309a15edb45be132a8a171ca226bf5599df4b81559c860906f33662a2f1a3211c9a695d6b37fd133ee33106f3bad315f048e583df1884f4e6302c5e9a064720
7
+ data.tar.gz: 67f27819835fe30014672e4c72543e49d9e47f113cfe10f596607cbd8357e28bd4d214a7b2d643ff158f86a1a7ac7c10c1fc14612119abb2327d2854065b7e99
data/README.md CHANGED
@@ -34,7 +34,11 @@ class ApplicationJobler < Jobler::BaseJobler
34
34
  end
35
35
  ```
36
36
 
37
- Jobler is going to queue its jobs through the ActiveJob queue called `:jobler`, so make sure a worker is listening to that queue.
37
+ Jobler is going to queue its jobs through the ActiveJob queue called `:jobler`, so make sure a worker is listening to that queue. This is done like this in Sidekiq:
38
+
39
+ ```bash
40
+ bundle exec sidekiq --queue default --queue jobler --queue mailers
41
+ ```
38
42
 
39
43
 
40
44
  ## Usage
@@ -111,7 +115,7 @@ You should then create a controller something like this:
111
115
  ```ruby
112
116
  class JoblerJobsController < ApplicationController
113
117
  def show
114
- @job = Jobler::Job.find_by!(slug: param[:id])
118
+ @job = Jobler::Job.find_by!(slug: params[:id])
115
119
  @result = @job.results.find_by!(name: "render")
116
120
  end
117
121
  end
@@ -119,7 +123,7 @@ end
119
123
 
120
124
  And a view in "app/views/jobler_jobs/show.html.erb":
121
125
  ```erb
122
- <%= @result.result.force_encoding("utf-8").html_safe
126
+ <%= @result.result.force_encoding("utf-8").html_safe %>
123
127
  ```
124
128
 
125
129
  You should also add a route like this:
@@ -129,6 +133,32 @@ Rails.application.routes.draw do
129
133
  end
130
134
  ```
131
135
 
136
+ # Progress bar
137
+
138
+ In order to utilize the progress bar and return some feedback on the progress to the user, you can implement a couple of calls to do that:
139
+
140
+ ```ruby
141
+ class MyJobler < ApplicationJobler
142
+ def execute!
143
+ progress_total collection.size
144
+
145
+ collection.find_each do |model|
146
+ increment_progress!
147
+ end
148
+ end
149
+ end
150
+ ```
151
+
152
+ You can also call it from a view, if you a doing a render like this:
153
+ ```erb
154
+ <% jobler.increment_progress! %>
155
+ ```
156
+
157
+ You can also specify a custom value if it isn't 1:
158
+ ```erb
159
+ <% jobler.increment_progress!(value: 5.0) %>
160
+ ```
161
+
132
162
  ## License
133
163
 
134
164
  This project rocks and uses MIT-LICENSE.
@@ -1,7 +1,6 @@
1
1
  class Jobler::JobsController < Jobler::ApplicationController
2
2
  def show
3
3
  @job = Jobler::Job.find_by!(slug: params[:id])
4
- @result = @job.jobler.result if @job.completed?
5
4
 
6
5
  respond_to do |format|
7
6
  format.json do
@@ -14,8 +13,12 @@ class Jobler::JobsController < Jobler::ApplicationController
14
13
  }
15
14
  end
16
15
 
17
- if @result.is_a?(Jobler::RedirectTo)
18
- format.html { redirect_to @result.url }
16
+ if @job.completed?
17
+ @job.jobler.controller = self
18
+ @job.jobler.format = format
19
+
20
+ @result = @job.jobler.result
21
+ format.html { redirect_to @result.url } if @result.is_a?(Jobler::RedirectTo)
19
22
  else
20
23
  format.html
21
24
  end
@@ -0,0 +1,65 @@
1
+ class Jobler::Models::DestroyerJobler < Jobler::BaseJobler
2
+ def execute!
3
+ calculate_numbers
4
+
5
+ model_class.transaction do
6
+ destroy_relationships
7
+ model.destroy!
8
+ end
9
+ end
10
+
11
+ def result
12
+ format.html { controller.redirect_to args.fetch(:redirect_to) }
13
+ end
14
+
15
+ private
16
+
17
+ def calculate_numbers
18
+ Rails.logger.debug "Calculate numbers"
19
+
20
+ @total = 0
21
+ relationships.each do |relationship|
22
+ Rails.logger.debug "Calculate size for #{relationship}"
23
+ @total += model.__send__(relationship).size
24
+ end
25
+
26
+ progress_total @total
27
+
28
+ Rails.logger.debug "Done calculating numbers: #{@total}"
29
+ end
30
+
31
+ def destroy_relationships
32
+ Rails.logger.debug "Destroying relationships"
33
+ relationships.each do |relationship|
34
+ Rails.logger.debug "Destroying #{relationship}"
35
+ model.__send__(relationship).find_each do |sub_model|
36
+ Rails.logger.debug "Destroying #{sub_model.id}"
37
+ sub_model.destroy!
38
+ increment_progress!
39
+ end
40
+ end
41
+ end
42
+
43
+ def model_class
44
+ @model_class ||= args.fetch(:model).constantize
45
+ end
46
+
47
+ def model
48
+ @_model ||= model_class.find(args.fetch(:model_id))
49
+ end
50
+
51
+ def relationships
52
+ @_relationships ||= proc do
53
+ result = []
54
+
55
+ model_class.reflections.each_value do |reflection|
56
+ next unless reflection.is_a?(ActiveRecord::Reflection::HasManyReflection)
57
+ next unless reflection.options[:dependent] == :destroy
58
+
59
+ result << reflection.name
60
+ end
61
+
62
+ result
63
+ end.call
64
+ end
65
+ end
@@ -1,15 +1,12 @@
1
- class Jobler::Job < ActiveRecord::Base
2
- has_many :results, class_name: "Jobler::Result", dependent: :destroy
1
+ class Jobler::Job < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
2
+ has_many :results, class_name: "Jobler::Result", dependent: :destroy, inverse_of: :job
3
3
 
4
4
  validates :jobler_type, :slug, :state, presence: true
5
5
 
6
6
  before_validation :set_slug
7
7
 
8
8
  def jobler
9
- user_jobler = jobler_type.constantize.new
10
- user_jobler.instance_variable_set(:@args, YAML.load(parameters)) # rubocop:disable Security/YAMLLoad
11
- user_jobler.instance_variable_set(:@job, self)
12
- user_jobler
9
+ @_jobler ||= jobler_type.constantize.new(args: YAML.load(parameters), job: self) # rubocop:disable Security/YAMLLoad
13
10
  end
14
11
 
15
12
  def completed?
@@ -1,5 +1,5 @@
1
- class Jobler::Result < ActiveRecord::Base
2
- belongs_to :job, class_name: "Jobler::Job"
1
+ class Jobler::Result < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
2
+ belongs_to :job, class_name: "Jobler::Job", inverse_of: :results
3
3
 
4
4
  validates :job, :name, presence: true
5
5
  end
@@ -1,4 +1,4 @@
1
- class CreateJobs < ActiveRecord::Migration
1
+ class CreateJobs < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  create_table :jobler_jobs do |t|
4
4
  t.string :jobler_type, null: false
@@ -1,4 +1,4 @@
1
- class CreateJoblerResults < ActiveRecord::Migration
1
+ class CreateJoblerResults < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  create_table :jobler_results do |t|
4
4
  t.belongs_to :job, index: true, null: false
@@ -1,4 +1,4 @@
1
- class AddLocaleToJobs < ActiveRecord::Migration
1
+ class AddLocaleToJobs < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  add_column :jobler_jobs, :locale, :string
4
4
  end
@@ -1,4 +1,4 @@
1
- class AddErrorMessageAndErrorBacktraceToJobs < ActiveRecord::Migration
1
+ class AddErrorMessageAndErrorBacktraceToJobs < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  add_column :jobler_jobs, :error_message, :text
4
4
  add_column :jobler_jobs, :error_type, :string
@@ -1,4 +1,4 @@
1
- class AddSlugToJobs < ActiveRecord::Migration
1
+ class AddSlugToJobs < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  add_column :jobler_jobs, :slug, :string
4
4
  add_index :jobler_jobs, :slug, unique: true
@@ -1,4 +1,4 @@
1
- class AddHostProtocolAndPortToJoblerJobs < ActiveRecord::Migration
1
+ class AddHostProtocolAndPortToJoblerJobs < ActiveRecord::Migration[5.1]
2
2
  def change
3
3
  add_column :jobler_jobs, :host, :string
4
4
  add_column :jobler_jobs, :protocol, :string
@@ -1,6 +1,12 @@
1
1
  class Jobler::BaseJobler
2
+ attr_accessor :controller, :format
2
3
  attr_reader :args, :job
3
4
 
5
+ def initialize(args = {})
6
+ @args = args[:args]
7
+ @job = args[:job]
8
+ end
9
+
4
10
  def create_result!(args)
5
11
  if args[:temp_file]
6
12
  temp_file = args.fetch(:temp_file)
@@ -56,9 +62,7 @@ class Jobler::BaseJobler
56
62
  end
57
63
 
58
64
  def render(template_path, locals = {})
59
- if template_path.is_a?(Symbol)
60
- template_path = "joblers/#{jobler_name}/#{template_path}"
61
- end
65
+ template_path = "joblers/#{jobler_name}/#{template_path}" if template_path.is_a?(Symbol)
62
66
 
63
67
  request = ActionDispatch::Request.new(
64
68
  "HTTP_HOST" => "#{job.host}:#{job.port}",
@@ -90,7 +94,7 @@ class Jobler::BaseJobler
90
94
 
91
95
  raise "No result by that name: #{args.fetch(:name)}" unless job_result
92
96
 
93
- temp_file = Tempfile.new
97
+ temp_file = ::Tempfile.new("jobler_tempfile")
94
98
  temp_file.binmode
95
99
  temp_file.write(job_result.result)
96
100
  temp_file.close
@@ -1,4 +1,4 @@
1
- class Jobler::JobRunner < ActiveJob::Base
1
+ class Jobler::JobRunner < ActiveJob::Base # rubocop:disable Rails/ApplicationJob
2
2
  queue_as :jobler
3
3
 
4
4
  def perform(job_id)
@@ -1,3 +1,3 @@
1
1
  module Jobler
2
- VERSION = "0.0.9".freeze
2
+ VERSION = "0.0.10".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jobler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-16 00:00:00.000000000 Z
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -43,6 +43,7 @@ files:
43
43
  - app/controllers/jobler/downloads_controller.rb
44
44
  - app/controllers/jobler/jobs_controller.rb
45
45
  - app/helpers/jobler/application_helper.rb
46
+ - app/joblers/jobler/models/destroyer_jobler.rb
46
47
  - app/models/jobler/job.rb
47
48
  - app/models/jobler/result.rb
48
49
  - app/views/jobler/jobs/show.html.erb
@@ -85,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
86
  version: '0'
86
87
  requirements: []
87
88
  rubyforge_project:
88
- rubygems_version: 2.6.8
89
+ rubygems_version: 2.6.13
89
90
  signing_key:
90
91
  specification_version: 4
91
92
  summary: Generate pages or files in the background