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 +4 -4
- data/README.md +33 -3
- data/app/controllers/jobler/jobs_controller.rb +6 -3
- data/app/joblers/jobler/models/destroyer_jobler.rb +65 -0
- data/app/models/jobler/job.rb +3 -6
- data/app/models/jobler/result.rb +2 -2
- data/db/migrate/20170130220604_create_jobs.rb +1 -1
- data/db/migrate/20170130220734_create_jobler_results.rb +1 -1
- data/db/migrate/20170203161045_add_locale_to_jobs.rb +1 -1
- data/db/migrate/20170206100737_add_error_message_and_error_backtrace_to_jobs.rb +1 -1
- data/db/migrate/20170206104246_add_slug_to_jobs.rb +1 -1
- data/db/migrate/20170217082955_add_host_protocol_and_port_to_jobler_jobs.rb +1 -1
- data/lib/jobler/base_jobler.rb +8 -4
- data/lib/jobler/job_runner.rb +1 -1
- data/lib/jobler/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 449bec194fa9aa9bc0a4e57ab17a5998b479c4b4
|
4
|
+
data.tar.gz: d97d7f6c3a7800265a5907508c29cb83275b2be4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
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 @
|
18
|
-
|
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
|
data/app/models/jobler/job.rb
CHANGED
@@ -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
|
-
|
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?
|
data/app/models/jobler/result.rb
CHANGED
@@ -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
|
data/lib/jobler/base_jobler.rb
CHANGED
@@ -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
|
data/lib/jobler/job_runner.rb
CHANGED
data/lib/jobler/version.rb
CHANGED
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.
|
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:
|
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.
|
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
|