Module: MongoJob

Defined in:
lib/mongojob.rb,
lib/mongojob/job.rb,
lib/mongojob/web.rb,
lib/mongojob/deamon.rb,
lib/mongojob/worker.rb,
lib/mongojob/helpers.rb,
lib/mongojob/version.rb,
lib/mongojob/model/job.rb,
lib/mongojob/model/queue.rb,
lib/mongojob/model/worker.rb,
lib/mongojob/worker_helpers.rb,
lib/mongojob/mixins/document.rb,
lib/mongojob/mixins/fiber_runner.rb

Defined Under Namespace

Modules: Helpers, Mixins, Model, ProcessWatcher, WebHelpers, WorkerHelpers Classes: Deamon, Job, Web, Worker

Constant Summary collapse

Version =
'0.0.1'

Class Method Summary collapse

Class Method Details

.connectionObject



24
25
26
27
# File 'lib/mongojob.rb', line 24

def self.connection
  split = host.split ':'
  @connection ||= Mongo::Connection.new(split[0], (split[1] || 27017).to_i)
end

.database_nameObject



33
34
35
# File 'lib/mongojob.rb', line 33

def self.database_name
  @database_name
end

.database_name=(database_name) ⇒ Object



29
30
31
# File 'lib/mongojob.rb', line 29

def self.database_name=(database_name)
  @database_name = database_name
end

.dequeue(job_id) ⇒ Object



56
57
58
# File 'lib/mongojob.rb', line 56

def self.dequeue(job_id)
  Model::Job.delete(job_id)
end

.enqueue(klass, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/mongojob.rb', line 37

def self.enqueue(klass, options = {})
  queue_name = klass.is_a?(Class) ? queue_from_class(klass) : klass.to_s
  raise "Given class does not return any queue name" unless queue_name
  job = Model::Job.create({
    klass: klass.is_a?(Class) ? klass.to_s : nil,
    options: options,
    queue_name: queue_name
  })
  job.id.to_s
end

.find_job(job_id) ⇒ Object



52
53
54
# File 'lib/mongojob.rb', line 52

def self.find_job(job_id)
  Model::Job.find job_id
end

.hostObject



20
21
22
# File 'lib/mongojob.rb', line 20

def self.host
  @host || 'localhost'
end

.host=(host) ⇒ Object



16
17
18
# File 'lib/mongojob.rb', line 16

def self.host=(host)
  @host = host
end

.queue_from_class(klass) ⇒ Object

Given a class, try to extrapolate an appropriate queue based on a class instance variable or ‘queue` method.



62
63
64
65
# File 'lib/mongojob.rb', line 62

def self.queue_from_class(klass)
  klass.instance_variable_get(:@queue) ||
    (klass.respond_to?(:queue) and klass.queue)
end

.reserve(queue_name, worker_id) ⇒ Object



48
49
50
# File 'lib/mongojob.rb', line 48

def self.reserve(queue_name, worker_id)
  Model::Job.reserve(queue_name, worker_id)
end