Class: Jobler::BaseJobler
- Inherits:
-
Object
- Object
- Jobler::BaseJobler
- Defined in:
- lib/jobler/base_jobler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#format ⇒ Object
Returns the value of attribute format.
-
#job ⇒ Object
readonly
Returns the value of attribute job.
Instance Method Summary collapse
- #create_result!(args) ⇒ Object
- #execute! ⇒ Object
- #increment_progress!(value: 1.0) ⇒ Object
-
#initialize(args = {}) ⇒ BaseJobler
constructor
A new instance of BaseJobler.
- #jobler_name ⇒ Object
- #progress_total(new_total) ⇒ Object
- #render(template_path, locals = {}) ⇒ Object
- #result ⇒ Object
- #temp_file_for_result(args) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ BaseJobler
Returns a new instance of BaseJobler.
5 6 7 8 |
# File 'lib/jobler/base_jobler.rb', line 5 def initialize(args = {}) @args = args[:args] @job = args[:job] end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
3 4 5 |
# File 'lib/jobler/base_jobler.rb', line 3 def args @args end |
#controller ⇒ Object
Returns the value of attribute controller.
2 3 4 |
# File 'lib/jobler/base_jobler.rb', line 2 def controller @controller end |
#format ⇒ Object
Returns the value of attribute format.
2 3 4 |
# File 'lib/jobler/base_jobler.rb', line 2 def format @format end |
#job ⇒ Object (readonly)
Returns the value of attribute job.
3 4 5 |
# File 'lib/jobler/base_jobler.rb', line 3 def job @job end |
Instance Method Details
#create_result!(args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/jobler/base_jobler.rb', line 10 def create_result!(args) if args[:temp_file] temp_file = args.fetch(:temp_file) temp_file.close unless temp_file.closed? content = File.read(temp_file.path) else content = args.fetch(:content) end job.results.create!( name: args.fetch(:name), result: content ) end |
#execute! ⇒ Object
25 26 27 |
# File 'lib/jobler/base_jobler.rb', line 25 def execute! raise NoMethodError, "You should define the 'execute!' method on #{self.class.name}" end |
#increment_progress!(value: 1.0) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/jobler/base_jobler.rb', line 41 def increment_progress!(value: 1.0) @_progress_count ||= 0.0 @_progress_count += value.to_f new_progress = @_progress_count / @_progress_total if @_current_progress.nil? update = true else progress_difference = new_progress - @_current_progress update = true if progress_difference > 0.01 end if update job.update!(progress: new_progress) @_current_progress = new_progress end end |
#jobler_name ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jobler/base_jobler.rb', line 29 def jobler_name new_name = "" parts = self.class.name.split("::") parts.each do |part| new_name << "/" unless new_name.empty? new_name << part.underscore end new_name end |
#progress_total(new_total) ⇒ Object
60 61 62 |
# File 'lib/jobler/base_jobler.rb', line 60 def progress_total(new_total) @_progress_total = new_total.to_f end |
#render(template_path, locals = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jobler/base_jobler.rb', line 64 def render(template_path, locals = {}) template_path = "joblers/#{jobler_name}/#{template_path}" if template_path.is_a?(Symbol) request = ActionDispatch::Request.new( "HTTP_HOST" => "#{job.host}:#{job.port}", "HTTP_X_FORWARDED_PROTO" => job.protocol ) controller = ::ApplicationJoblerController.new controller.instance_variable_set(:@jobler, self) controller.request = request controller.response = ActionDispatch::Response.new render_result = controller.render(template_path, layout: false, locals: {jobler: self}.merge(locals)) if render_result.is_a?(String) # Rails 5 behaviour render_result else # Rails 4 behaviour render_result.join end end |
#result ⇒ Object
88 89 90 |
# File 'lib/jobler/base_jobler.rb', line 88 def result raise NoMethodError, "You should define the 'result' method on #{self.class.name}" end |
#temp_file_for_result(args) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jobler/base_jobler.rb', line 92 def temp_file_for_result(args) job_result = job.results.where(name: args.fetch(:name)).first raise "No result by that name: #{args.fetch(:name)}" unless job_result temp_file = ::Tempfile.new("jobler_tempfile") temp_file.binmode temp_file.write(job_result.result) temp_file.close temp_file end |