jobler 0.0.12 → 0.0.13
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.
- checksums.yaml +4 -4
- data/app/controllers/jobler/downloads_controller.rb +5 -1
- data/app/models/jobler/result.rb +2 -0
- data/app/views/jobler/jobs/show.html.erb +5 -1
- data/lib/jobler/base_controller.rb +1 -1
- data/lib/jobler/base_jobler.rb +67 -17
- data/lib/jobler/file_download.rb +8 -4
- data/lib/jobler/job_runner.rb +7 -1
- data/lib/jobler/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 549a9f294ff023f7c67091e91120706db134cd9639d9f8b2d60d9860c5562788
|
4
|
+
data.tar.gz: '097746283982a71eeffdd1a006e2e8a6d178c5b847e6854ddae0696075da4490'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3204ebaef1538454ea89a42066fc498d554317f14a9dd72e03356c1c1cb58ef5aac58909246c750b17c785b4610b33cef3a7b19ab93cc3877297ae0b659025e6
|
7
|
+
data.tar.gz: e7ffb22951a39418e2ffd48c7f58b69b7465dbf41419583c5b49ed8ae71fbcbd15ca5dd7f58491df70ec7769c2e1dadb2416bb7ceac974910d50ef8af7617a23
|
@@ -4,7 +4,11 @@ class Jobler::DownloadsController < Jobler::ApplicationController
|
|
4
4
|
@result = @job.jobler.result
|
5
5
|
|
6
6
|
if @result.is_a?(Jobler::FileDownload)
|
7
|
-
|
7
|
+
if @result.url.present?
|
8
|
+
redirect_to @result.url
|
9
|
+
else
|
10
|
+
send_file @result.temp_file.path, disposition: "attachment", filename: @result.file_name
|
11
|
+
end
|
8
12
|
else
|
9
13
|
flash[:error] = "The result wasn't a file download"
|
10
14
|
redirect_to :back
|
data/app/models/jobler/result.rb
CHANGED
@@ -4,7 +4,11 @@
|
|
4
4
|
|
5
5
|
<% if @job.completed? %>
|
6
6
|
<% if @result.is_a?(Jobler::FileDownload) %>
|
7
|
-
|
7
|
+
<% if @result.url.present? %>
|
8
|
+
<%= link_to "Download", @result.url %>
|
9
|
+
<% else %>
|
10
|
+
<%= link_to "Download", download_path(@job) %>
|
11
|
+
<% end %>
|
8
12
|
<% elsif @result.is_a?(Jobler::PageRender) %>
|
9
13
|
<%= @result.body.html_safe %>
|
10
14
|
<% end %>
|
@@ -1,2 +1,2 @@
|
|
1
|
-
class Jobler::BaseController < ActionController::Base
|
1
|
+
class Jobler::BaseController < ActionController::Base # rubocop:disable Rails/ApplicationController
|
2
2
|
end
|
data/lib/jobler/base_jobler.rb
CHANGED
@@ -2,24 +2,52 @@ class Jobler::BaseJobler
|
|
2
2
|
attr_accessor :controller, :format
|
3
3
|
attr_reader :args, :job
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
5
|
+
def self.before_jobling(&blk)
|
6
|
+
@@before_jobling ||= [] # rubocop:disable Style/ClassVars
|
7
|
+
@@before_jobling << blk
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def self.after_jobling(&blk)
|
11
|
+
@@after_jobling ||= [] # rubocop:disable Style/ClassVars
|
12
|
+
@@after_jobling << blk
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(args:, job:)
|
16
|
+
@args = args
|
17
|
+
@job = job
|
18
|
+
end
|
19
|
+
|
20
|
+
def call_before_callbacks
|
21
|
+
@@before_jobling&.each do |before_callback|
|
22
|
+
instance_eval(&before_callback)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def call_after_callbacks
|
27
|
+
@@after_jobling&.each do |after_callback|
|
28
|
+
instance_eval(&after_callback)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_result!(content: nil, name:, result: nil, temp_file: nil, save_in_database: false)
|
33
|
+
jobler_result = job.results.new(name: name)
|
34
|
+
|
35
|
+
if content && !temp_file
|
36
|
+
temp_file = Tempfile.new(name)
|
37
|
+
temp_file.write(content)
|
38
|
+
temp_file.close
|
39
|
+
end
|
40
|
+
|
41
|
+
if result
|
42
|
+
jobler_result.result = result
|
15
43
|
else
|
16
|
-
|
44
|
+
raise "No tempfile could be found" unless temp_file
|
45
|
+
|
46
|
+
handle_file(jobler_result: jobler_result, save_in_database: save_in_database, temp_file: temp_file)
|
17
47
|
end
|
18
48
|
|
19
|
-
|
20
|
-
|
21
|
-
result: content
|
22
|
-
)
|
49
|
+
jobler_result.save!
|
50
|
+
jobler_result
|
23
51
|
end
|
24
52
|
|
25
53
|
def execute!
|
@@ -89,10 +117,9 @@ class Jobler::BaseJobler
|
|
89
117
|
raise NoMethodError, "You should define the 'result' method on #{self.class.name}"
|
90
118
|
end
|
91
119
|
|
92
|
-
def temp_file_for_result(
|
93
|
-
job_result = job.results.where(name:
|
94
|
-
|
95
|
-
raise "No result by that name: #{args.fetch(:name)}" unless job_result
|
120
|
+
def temp_file_for_result(name:)
|
121
|
+
job_result = job.results.where(name: name).first
|
122
|
+
raise "No result by that name: #{name}" unless job_result
|
96
123
|
|
97
124
|
temp_file = ::Tempfile.new("jobler_tempfile")
|
98
125
|
temp_file.binmode
|
@@ -100,4 +127,27 @@ class Jobler::BaseJobler
|
|
100
127
|
temp_file.close
|
101
128
|
temp_file
|
102
129
|
end
|
130
|
+
|
131
|
+
def url_for_result(name:)
|
132
|
+
job_result = job.results.where(name: name).first
|
133
|
+
raise "No result by that name: #{name}" unless job_result
|
134
|
+
|
135
|
+
Rails.application.routes.url_helpers.rails_blob_path(job_result.file.attachment, only_path: true)
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
def handle_file(jobler_result:, save_in_database:, temp_file:)
|
141
|
+
if save_in_database
|
142
|
+
temp_file = temp_file
|
143
|
+
temp_file.close unless temp_file.closed?
|
144
|
+
content = File.read(temp_file.path)
|
145
|
+
jobler_result.result = content
|
146
|
+
else
|
147
|
+
jobler_result.file.attach(
|
148
|
+
filename: File.basename(temp_file.path),
|
149
|
+
io: File.open(temp_file.path)
|
150
|
+
)
|
151
|
+
end
|
152
|
+
end
|
103
153
|
end
|
data/lib/jobler/file_download.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
class Jobler::FileDownload
|
2
|
-
attr_reader :file_name, :temp_file
|
2
|
+
attr_reader :file_name, :temp_file, :url
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
|
6
|
-
|
4
|
+
def initialize(file_name: nil, temp_file: nil, url: nil)
|
5
|
+
raise "Temp file or URL should be given" if !temp_file && !url
|
6
|
+
raise "No filename given with temp-file" if temp_file && file_name.blank?
|
7
|
+
|
8
|
+
@file_name = file_name
|
9
|
+
@temp_file = temp_file
|
10
|
+
@url = url
|
7
11
|
end
|
8
12
|
end
|
data/lib/jobler/job_runner.rb
CHANGED
@@ -7,7 +7,13 @@ class Jobler::JobRunner < ActiveJob::Base # rubocop:disable Rails/ApplicationJob
|
|
7
7
|
|
8
8
|
begin
|
9
9
|
with_locale do
|
10
|
-
@job.jobler.
|
10
|
+
@job.jobler.call_before_callbacks
|
11
|
+
|
12
|
+
begin
|
13
|
+
@job.jobler.execute!
|
14
|
+
ensure
|
15
|
+
@job.jobler.call_after_callbacks
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
@job.update!(ended_at: Time.zone.now, progress: 1.0, state: "completed")
|
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.13
|
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: 2020-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|