Class: Jobit::Jobby

Inherits:
Struct show all
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

Instance Method Summary collapse

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

#to_hash

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, options = {})
  if job.nil?
    create_new
    if options
      for key, val in options
        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

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def args
  @args
end

#created_atObject

Returns the value of attribute created_at

Returns:

  • (Object)

    the current value of created_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def created_at
  @created_at
end

#errorObject

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def error
  @error
end

#failed_atObject

Returns the value of attribute failed_at

Returns:

  • (Object)

    the current value of failed_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def failed_at
  @failed_at
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def id
  @id
end

#keepObject

Returns the value of attribute keep

Returns:

  • (Object)

    the current value of keep



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def keep
  @keep
end

#locked_atObject

Returns the value of attribute locked_at

Returns:

  • (Object)

    the current value of locked_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def locked_at
  @locked_at
end

#locked_byObject

Returns the value of attribute locked_by

Returns:

  • (Object)

    the current value of locked_by



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def locked_by
  @locked_by
end

#messageObject

Returns the value of attribute message

Returns:

  • (Object)

    the current value of message



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def message
  @message
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def name
  @name
end

#objectObject

Returns the value of attribute object

Returns:

  • (Object)

    the current value of object



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def object
  @object
end

#priorityObject

Returns the value of attribute priority

Returns:

  • (Object)

    the current value of priority



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def priority
  @priority
end

#progressObject

Returns the value of attribute progress

Returns:

  • (Object)

    the current value of progress



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def progress
  @progress
end

#repeatObject

Returns the value of attribute repeat

Returns:

  • (Object)

    the current value of repeat



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def repeat
  @repeat
end

#repeat_delayObject

Returns the value of attribute repeat_delay

Returns:

  • (Object)

    the current value of repeat_delay



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def repeat_delay
  @repeat_delay
end

#run_atObject

Returns the value of attribute run_at

Returns:

  • (Object)

    the current value of run_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def run_at
  @run_at
end

#scheduleObject

Returns the value of attribute schedule

Returns:

  • (Object)

    the current value of schedule



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def schedule
  @schedule
end

#started_atObject

Returns the value of attribute started_at

Returns:

  • (Object)

    the current value of started_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def started_at
  @started_at
end

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def status
  @status
end

#stopped_atObject

Returns the value of attribute stopped_at

Returns:

  • (Object)

    the current value of stopped_at



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def stopped_at
  @stopped_at
end

#triesObject

Returns the value of attribute tries

Returns:

  • (Object)

    the current value of tries



5
6
7
# File 'lib/jobit/jobby.rb', line 5

def tries
  @tries
end

Instance Method Details

#destroyObject



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.message} - #{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.message}\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