Class: MongoJob::Model::Job

Inherits:
Object
  • Object
show all
Extended by:
Helpers, MongoJob::Mixins::Document
Includes:
Helpers, MongoMapper::Document
Defined in:
lib/mongojob/model/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MongoJob::Mixins::Document

connection, database_name

Methods included from Helpers

classify, constantize

Class Method Details

.reserve(queue_name, worker_id) ⇒ Object

Pop the first unassigned job from the queue



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/mongojob/model/job.rb', line 98

def self.reserve(queue_name, worker_id)
  begin
    job = self.first conditions: {
      queue_name: queue_name,
      status: 'queued'
    }, order: 'release_at'
    return nil unless job
    if job
      # Might be free. Update it with the new status
      self.set({id: job.id, status: 'queued'}, {status: 'working', worker_id: worker_id, started_at: Time.now})
    end
    job.reload
  end while job.worker_id != worker_id
  job
end

Instance Method Details

#at(*args) ⇒ Object

Usage: at(0.29) - at 29% at(0.29,1.0) - at 29% at(29,100) - at 29% at(2,7) - at 2 of 7 at(2,7, {foo: ‘bar’}) - at 2 of 7, and set custom_status



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mongojob/model/job.rb', line 76

def at *args
  options = args.last.is_a?(Hash) ? args.pop : {}
  num = args[0]
  total = args[1] || 1.0
  custom_status = options[:status]
  data = { pinged_at: Time.now }
  data[:progress] = {
    at: num,
    total: total
  } if num
  data[:custom_status] = custom_status if custom_status
  set data
  reload
  
  # TODO: stop the job if cancelled, e.g. by raising an exception.
end

#completeObject



63
64
65
66
67
68
# File 'lib/mongojob/model/job.rb', line 63

def complete
  set({
    status: 'done',
    completed_at: Time.now
  })
end

#fail(error) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/mongojob/model/job.rb', line 54

def fail error
  error_text = error.is_a?(Exception) ? "#{error.message}\n\n#{error.backtrace}" : error.to_s
  self.set({
    status: 'failed',
    error: error_text
  })
  reload
end

#job_classObject



46
47
48
# File 'lib/mongojob/model/job.rb', line 46

def job_class
  @job_class ||= constantize klass
end

#job_objectObject



50
51
52
# File 'lib/mongojob/model/job.rb', line 50

def job_object
  @job_object ||= job_class.new(self)
end

#percent_doneObject



93
94
95
# File 'lib/mongojob/model/job.rb', line 93

def percent_done
  progress['at'].to_f/ progress['total'].to_f if progress['at']
end

#pingObject



114
115
116
117
# File 'lib/mongojob/model/job.rb', line 114

def ping
  set pinged_at: Time.now
  reload
end

#set_release_atObject



42
43
44
# File 'lib/mongojob/model/job.rb', line 42

def set_release_at
  self.release_at ||= Time.now
end

#setup_queueObject

Make sure the queue exists for a given queue name. The usual way to create a job is to provide a queue_name without caring about the unmet reference, so we need to fix it here.



35
36
37
38
39
40
# File 'lib/mongojob/model/job.rb', line 35

def setup_queue
  queue = Queue.find self.queue_name
  unless queue
    queue = Queue.create _id: self.queue_name
  end
end