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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b799fadf62229d053e33b4f073fecc0962e944d9e2dee1dc8ae9f213c4a425ba
4
- data.tar.gz: d7a89ad05effc901eb99b11b944450091f63b4ed7aca467f58f1203118e19e56
3
+ metadata.gz: 549a9f294ff023f7c67091e91120706db134cd9639d9f8b2d60d9860c5562788
4
+ data.tar.gz: '097746283982a71eeffdd1a006e2e8a6d178c5b847e6854ddae0696075da4490'
5
5
  SHA512:
6
- metadata.gz: 0ae5ea9a10281c55fcb793679dcd0b46a879938a333b9839b285013521021077ee6eeb510f2c5cd6a475151083e3a703230c71dd8bb91843e0f49e422b66cb35
7
- data.tar.gz: dea066d7849a36caf18f2a6755481efb06159ca805326fef94e20d09c9fa047bab121c692f652cfb4c15b16ab43eded86e827b6e318f516d1909e3dd373501ef
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
- send_file @result.temp_file.path, disposition: "attachment", filename: @result.file_name
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
@@ -1,5 +1,7 @@
1
1
  class Jobler::Result < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
2
2
  belongs_to :job, class_name: "Jobler::Job", inverse_of: :results
3
3
 
4
+ has_one_attached :file
5
+
4
6
  validates :job, :name, presence: true
5
7
  end
@@ -4,7 +4,11 @@
4
4
 
5
5
  <% if @job.completed? %>
6
6
  <% if @result.is_a?(Jobler::FileDownload) %>
7
- <%= link_to "Download", download_path(@job) %>
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
@@ -2,24 +2,52 @@ class Jobler::BaseJobler
2
2
  attr_accessor :controller, :format
3
3
  attr_reader :args, :job
4
4
 
5
- def initialize(args = {})
6
- @args = args[:args]
7
- @job = args[:job]
5
+ def self.before_jobling(&blk)
6
+ @@before_jobling ||= [] # rubocop:disable Style/ClassVars
7
+ @@before_jobling << blk
8
8
  end
9
9
 
10
- def create_result!(args)
11
- if args[:temp_file]
12
- temp_file = args.fetch(:temp_file)
13
- temp_file.close unless temp_file.closed?
14
- content = File.read(temp_file.path)
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
- content = args.fetch(:content)
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
- job.results.create!(
20
- name: args.fetch(:name),
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(args)
93
- job_result = job.results.where(name: args.fetch(:name)).first
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
@@ -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(args)
5
- @file_name = args.fetch(:file_name)
6
- @temp_file = args.fetch(:temp_file)
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
@@ -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.execute!
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")
@@ -1,3 +1,3 @@
1
1
  module Jobler
2
- VERSION = "0.0.12".freeze
2
+ VERSION = "0.0.13".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.12
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: 2019-11-07 00:00:00.000000000 Z
11
+ date: 2020-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails