Class: Jobit::Jobby
- Includes:
- Jobit::Jobs::Commands, Jobit::Jobs::Statuses
- Defined in:
- lib/jobit/jobby.rb
Constant Summary collapse
- MAX_ATTEMPTS =
25- MAX_RUN_TIME =
hours
4
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#error ⇒ Object
Returns the value of attribute error.
-
#failed_at ⇒ Object
Returns the value of attribute failed_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#keep ⇒ Object
Returns the value of attribute keep.
-
#locked_at ⇒ Object
Returns the value of attribute locked_at.
-
#locked_by ⇒ Object
Returns the value of attribute locked_by.
-
#message ⇒ Object
Returns the value of attribute message.
-
#name ⇒ Object
Returns the value of attribute name.
-
#object ⇒ Object
Returns the value of attribute object.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#repeat ⇒ Object
Returns the value of attribute repeat.
-
#repeat_delay ⇒ Object
Returns the value of attribute repeat_delay.
-
#run_at ⇒ Object
Returns the value of attribute run_at.
-
#schedule ⇒ Object
Returns the value of attribute schedule.
-
#started_at ⇒ Object
Returns the value of attribute started_at.
-
#status ⇒ Object
Returns the value of attribute status.
-
#stopped_at ⇒ Object
Returns the value of attribute stopped_at.
-
#tries ⇒ Object
Returns the value of attribute tries.
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(job = nil, options = {}) ⇒ Jobby
constructor
A new instance of Jobby.
- #log_exception(error) ⇒ Object
- #process_job(args) ⇒ Object
- #run_job(worker_name = 'worker') ⇒ Object
Methods included from Jobit::Jobs::Commands
#add_message, #increase_tries, #lock!, #set_error, #set_progress, #set_start_time, #set_status, #set_stop_time, #set_tries, #unlock!
Methods included from Jobit::Jobs::Statuses
#complete?, #current_job, #failed?, #keep?, #locked?, #new?, #running?, #session, #stopped?, #time_to_run?
Methods inherited from Struct
Constructor Details
#initialize(job = nil, options = {}) ⇒ Jobby
Returns a new instance of Jobby.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jobit/jobby.rb', line 17 def initialize(job = nil, = {}) if job.nil? create_new if for key, val in self[key] = val end end self.id = "#{priority}.#{id}" Jobit::Storage.create(id, self) else job.each_with_index do |val, index| self[index] = val end end end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def args @args end |
#created_at ⇒ Object
Returns the value of attribute created_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def created_at @created_at end |
#error ⇒ Object
Returns the value of attribute error
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def error @error end |
#failed_at ⇒ Object
Returns the value of attribute failed_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def failed_at @failed_at end |
#id ⇒ Object
Returns the value of attribute id
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def id @id end |
#keep ⇒ Object
Returns the value of attribute keep
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def keep @keep end |
#locked_at ⇒ Object
Returns the value of attribute locked_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def locked_at @locked_at end |
#locked_by ⇒ Object
Returns the value of attribute locked_by
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def locked_by @locked_by end |
#message ⇒ Object
Returns the value of attribute message
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def @message end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def name @name end |
#object ⇒ Object
Returns the value of attribute object
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def object @object end |
#priority ⇒ Object
Returns the value of attribute priority
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def priority @priority end |
#progress ⇒ Object
Returns the value of attribute progress
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def progress @progress end |
#repeat ⇒ Object
Returns the value of attribute repeat
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def repeat @repeat end |
#repeat_delay ⇒ Object
Returns the value of attribute repeat_delay
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def repeat_delay @repeat_delay end |
#run_at ⇒ Object
Returns the value of attribute run_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def run_at @run_at end |
#schedule ⇒ Object
Returns the value of attribute schedule
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def schedule @schedule end |
#started_at ⇒ Object
Returns the value of attribute started_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def started_at @started_at end |
#status ⇒ Object
Returns the value of attribute status
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def status @status end |
#stopped_at ⇒ Object
Returns the value of attribute stopped_at
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def stopped_at @stopped_at end |
#tries ⇒ Object
Returns the value of attribute tries
5 6 7 |
# File 'lib/jobit/jobby.rb', line 5 def tries @tries end |
Instance Method Details
#destroy ⇒ Object
34 35 36 37 38 |
# File 'lib/jobit/jobby.rb', line 34 def destroy return unless id Jobit::Storage.destroy(id) clear end |
#log_exception(error) ⇒ Object
80 81 82 83 |
# File 'lib/jobit/jobby.rb', line 80 def log_exception(error) logger.error "* [JOB:#{name}] failed with #{error.class.name}: #{error.} - #{tries} failed attempts" logger.error(error) end |
#process_job(args) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jobit/jobby.rb', line 40 def process_job(args) begin logger.info "* [JOB:#{name}] acquiring lock" runtime = Benchmark.realtime do self.status = 'running' self.started_at = Time.now.to_f update self.send("#{object}_task",*args) self.status = 'complete' self.progress = 100 self.stopped_at = Time.now.to_f update end logger.info "* [JOB:#{name}] completed after %.4f" % runtime rescue Exception => e log_exception(e) msg = "#{e.}\n\n#{e.backtrace.join("\n")}" set_error(msg) end end |
#run_job(worker_name = 'worker') ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jobit/jobby.rb', line 63 def run_job(worker_name = 'worker') return nil if locked? lock!(worker_name) self.class.send(:include, JobitItems) job_args = Marshal.restore(args) num = repeat.to_i == 0 ? 1 : repeat.to_i delay = repeat_delay.to_i num.times do process_job(job_args) break if failed? # stop loop if job failed increase_tries sleep delay if delay > 0 end cleanup end |