jobler 0.0.10 → 0.0.14

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
- SHA1:
3
- metadata.gz: 449bec194fa9aa9bc0a4e57ab17a5998b479c4b4
4
- data.tar.gz: d97d7f6c3a7800265a5907508c29cb83275b2be4
2
+ SHA256:
3
+ metadata.gz: 5cedd67bb154c2d2c93da4f460ea0d9eaa106d8017448a2b8c11476fc94fc618
4
+ data.tar.gz: 8c3aa61ccc372188fbdbd16395488a8e1f94a891c8ebd120fdf31efd1e1c5ed8
5
5
  SHA512:
6
- metadata.gz: 3309a15edb45be132a8a171ca226bf5599df4b81559c860906f33662a2f1a3211c9a695d6b37fd133ee33106f3bad315f048e583df1884f4e6302c5e9a064720
7
- data.tar.gz: 67f27819835fe30014672e4c72543e49d9e47f113cfe10f596607cbd8357e28bd4d214a7b2d643ff158f86a1a7ac7c10c1fc14612119abb2327d2854065b7e99
6
+ metadata.gz: b8dc4e2afb5cc0d84d4c058990323b1409ed0d8797eedc8700a5fcb02aab2740e34b824b79744d9cb1ed433bb919b2db5ca37945bf94b60e442a83e4447ab1c0
7
+ data.tar.gz: 5e32f79d3b67c6405eff5f729799d80a4f45b2598d8ffba677b6935183674180a8e4721ad2efab829cbce7ff639824bce9567f5ea17107c82db750173a85c283
data/README.md CHANGED
@@ -162,3 +162,4 @@ You can also specify a custom value if it isn't 1:
162
162
  ## License
163
163
 
164
164
  This project rocks and uses MIT-LICENSE.
165
+
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include("lib/**/*.rb")
15
15
  end
16
16
 
17
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
17
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
18
18
  load "rails/tasks/engine.rake"
19
19
 
20
20
  load "rails/tasks/statistics.rake"
@@ -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
@@ -18,7 +18,12 @@ class Jobler::JobsController < Jobler::ApplicationController
18
18
  @job.jobler.format = format
19
19
 
20
20
  @result = @job.jobler.result
21
- format.html { redirect_to @result.url } if @result.is_a?(Jobler::RedirectTo)
21
+
22
+ if @result.is_a?(Jobler::RedirectTo)
23
+ format.html { redirect_to @result.url }
24
+ else
25
+ format.html
26
+ end
22
27
  else
23
28
  format.html
24
29
  end
@@ -45,11 +45,11 @@ private
45
45
  end
46
46
 
47
47
  def model
48
- @_model ||= model_class.find(args.fetch(:model_id))
48
+ @model ||= model_class.find(args.fetch(:model_id))
49
49
  end
50
50
 
51
51
  def relationships
52
- @_relationships ||= proc do
52
+ @relationships ||= begin
53
53
  result = []
54
54
 
55
55
  model_class.reflections.each_value do |reflection|
@@ -60,6 +60,6 @@ private
60
60
  end
61
61
 
62
62
  result
63
- end.call
63
+ end
64
64
  end
65
65
  end
@@ -6,7 +6,7 @@ class Jobler::Job < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
6
6
  before_validation :set_slug
7
7
 
8
8
  def jobler
9
- @_jobler ||= jobler_type.constantize.new(args: YAML.load(parameters), job: self) # rubocop:disable Security/YAMLLoad
9
+ @jobler ||= jobler_type.constantize.new(args: YAML.load(parameters), job: self) # rubocop:disable Security/YAMLLoad
10
10
  end
11
11
 
12
12
  def completed?
@@ -19,6 +19,7 @@ class Jobler::Job < ActiveRecord::Base # rubocop:disable Rails/ApplicationRecord
19
19
 
20
20
  def to_param
21
21
  raise "No slug" unless slug?
22
+
22
23
  slug
23
24
  end
24
25
 
@@ -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,55 @@ 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
+
19
+ @@before_jobling ||= [] # rubocop:disable Style/ClassVars:
20
+ @@after_jobling ||= [] # rubocop:disable Style/ClassVars:
21
+ end
22
+
23
+ def call_before_callbacks
24
+ @@before_jobling&.each do |before_callback|
25
+ instance_eval(&before_callback)
26
+ end
27
+ end
28
+
29
+ def call_after_callbacks
30
+ @@after_jobling&.each do |after_callback|
31
+ instance_eval(&after_callback)
32
+ end
33
+ end
34
+
35
+ def create_result!(content: nil, name:, result: nil, temp_file: nil, save_in_database: false)
36
+ jobler_result = job.results.new(name: name)
37
+
38
+ if content && !temp_file
39
+ temp_file = Tempfile.new(name)
40
+ temp_file.write(content)
41
+ temp_file.close
42
+ end
43
+
44
+ if result
45
+ jobler_result.result = result
15
46
  else
16
- content = args.fetch(:content)
47
+ raise "No tempfile could be found" unless temp_file
48
+
49
+ handle_file(jobler_result: jobler_result, save_in_database: save_in_database, temp_file: temp_file)
17
50
  end
18
51
 
19
- job.results.create!(
20
- name: args.fetch(:name),
21
- result: content
22
- )
52
+ jobler_result.save!
53
+ jobler_result
23
54
  end
24
55
 
25
56
  def execute!
@@ -52,7 +83,7 @@ class Jobler::BaseJobler
52
83
  end
53
84
 
54
85
  if update
55
- job.update_attributes!(progress: new_progress)
86
+ job.update!(progress: new_progress)
56
87
  @_current_progress = new_progress
57
88
  end
58
89
  end
@@ -74,7 +105,7 @@ class Jobler::BaseJobler
74
105
  controller.request = request
75
106
  controller.response = ActionDispatch::Response.new
76
107
 
77
- render_result = controller.render(template_path, formats: Mime::EXTENSION_LOOKUP.keys, layout: false, locals: {jobler: self}.merge(locals))
108
+ render_result = controller.render_to_string(template_path, layout: false, locals: {jobler: self}.merge(locals))
78
109
 
79
110
  if render_result.is_a?(String)
80
111
  # Rails 5 behaviour
@@ -89,10 +120,9 @@ class Jobler::BaseJobler
89
120
  raise NoMethodError, "You should define the 'result' method on #{self.class.name}"
90
121
  end
91
122
 
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
123
+ def temp_file_for_result(name:)
124
+ job_result = job.results.where(name: name).first
125
+ raise "No result by that name: #{name}" unless job_result
96
126
 
97
127
  temp_file = ::Tempfile.new("jobler_tempfile")
98
128
  temp_file.binmode
@@ -100,4 +130,26 @@ class Jobler::BaseJobler
100
130
  temp_file.close
101
131
  temp_file
102
132
  end
133
+
134
+ def url_for_result(name:)
135
+ job_result = job.results.where(name: name).first
136
+ raise "No result by that name: #{name}" unless job_result
137
+
138
+ Rails.application.routes.url_helpers.rails_blob_path(job_result.file.attachment, only_path: true)
139
+ end
140
+
141
+ private
142
+
143
+ def handle_file(jobler_result:, save_in_database:, temp_file:)
144
+ if save_in_database
145
+ temp_file.close unless temp_file.closed?
146
+ content = File.read(temp_file.path)
147
+ jobler_result.result = content
148
+ else
149
+ jobler_result.file.attach(
150
+ filename: File.basename(temp_file.path),
151
+ io: File.open(temp_file.path)
152
+ )
153
+ end
154
+ end
103
155
  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
@@ -3,16 +3,22 @@ class Jobler::JobRunner < ActiveJob::Base # rubocop:disable Rails/ApplicationJob
3
3
 
4
4
  def perform(job_id)
5
5
  @job = Jobler::Job.find(job_id)
6
- @job.update_attributes!(started_at: Time.zone.now, state: "started")
6
+ @job.update!(started_at: Time.zone.now, state: "started")
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
- @job.update_attributes!(ended_at: Time.zone.now, progress: 1.0, state: "completed")
19
+ @job.update!(ended_at: Time.zone.now, progress: 1.0, state: "completed")
14
20
  rescue Exception => e # rubocop:disable Lint/RescueException
15
- @job.update_attributes!(
21
+ @job.update!(
16
22
  ended_at: Time.zone.now,
17
23
  error_message: e.message,
18
24
  error_type: e.class.name,
@@ -24,11 +30,9 @@ class Jobler::JobRunner < ActiveJob::Base # rubocop:disable Rails/ApplicationJob
24
30
 
25
31
  private
26
32
 
27
- def with_locale
33
+ def with_locale(&blk)
28
34
  if @job.locale?
29
- I18n.with_locale(@job.locale) do
30
- yield
31
- end
35
+ I18n.with_locale(@job.locale, &blk)
32
36
  else
33
37
  yield
34
38
  end
@@ -0,0 +1,7 @@
1
+ class Jobler::Message
2
+ attr_reader :message
3
+
4
+ def initialize(message:)
5
+ @message = message
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Jobler
2
- VERSION = "0.0.10".freeze
2
+ VERSION = "0.0.14".freeze
3
3
  end
data/lib/jobler.rb CHANGED
@@ -6,6 +6,7 @@ module Jobler
6
6
  autoload :FileDownload, "#{path}/file_download"
7
7
  autoload :JobScheduler, "#{path}/job_scheduler"
8
8
  autoload :JobRunner, "#{path}/job_runner"
9
+ autoload :Message, "#{path}/message"
9
10
  autoload :PageRender, "#{path}/page_render"
10
11
  autoload :RedirectTo, "#{path}/redirect_to"
11
12
  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.10
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-03 00:00:00.000000000 Z
11
+ date: 2021-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '4'
19
+ version: 6.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '4'
26
+ version: 6.0.0
27
27
  description: Generate pages or files in the background
28
28
  email:
29
29
  - kaspernj@gmail.com
@@ -62,6 +62,7 @@ files:
62
62
  - lib/jobler/file_download.rb
63
63
  - lib/jobler/job_runner.rb
64
64
  - lib/jobler/job_scheduler.rb
65
+ - lib/jobler/message.rb
65
66
  - lib/jobler/page_render.rb
66
67
  - lib/jobler/redirect_to.rb
67
68
  - lib/jobler/version.rb
@@ -78,15 +79,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
80
  - - ">="
80
81
  - !ruby/object:Gem::Version
81
- version: '0'
82
+ version: '2.5'
82
83
  required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
85
  - - ">="
85
86
  - !ruby/object:Gem::Version
86
87
  version: '0'
87
88
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.6.13
89
+ rubygems_version: 3.1.6
90
90
  signing_key:
91
91
  specification_version: 4
92
92
  summary: Generate pages or files in the background