Class: Delayed::Backend::Mongoid::Job

Inherits:
Object
  • Object
show all
Includes:
Base, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/delayed/backend/mongoid.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_forkObject

In a multi-process setup, this will be called to ensure fresh database connections are immediately made on each newly spawned child process.



63
64
65
66
67
68
# File 'lib/delayed/backend/mongoid.rb', line 63

def after_fork
  ::Mongoid::Clients.clients.each do |_name, client|
    client.close
    client.reconnect
  end
end

.before_forkObject

In a multi-process setup, this will be called at boot time to close unnecessary database connections on the parent process.



57
58
59
# File 'lib/delayed/backend/mongoid.rb', line 57

def before_fork
  ::Mongoid.disconnect_clients
end

.clear_locks!(worker_name) ⇒ Object

When a worker is exiting, make sure we don’t have any locked jobs.



51
52
53
# File 'lib/delayed/backend/mongoid.rb', line 51

def clear_locks!(worker_name)
  where(locked_by: worker_name).update_all(locked_at: nil, locked_by: nil)
end

.db_time_nowObject



35
36
37
# File 'lib/delayed/backend/mongoid.rb', line 35

def db_time_now
  Time.now.utc
end

.reserve(worker, max_run_time = Worker.max_run_time) ⇒ Object

Reserves one job for the worker. Atomically picks and locks one job from the collection.



41
42
43
44
45
46
47
48
# File 'lib/delayed/backend/mongoid.rb', line 41

def reserve(worker, max_run_time = Worker.max_run_time)
  right_now = db_time_now
  criteria = reservation_criteria(worker, right_now, max_run_time)
  criteria.find_one_and_update(
    { '$set' => { locked_at: right_now, locked_by: worker.name } },
    return_document: :after
  )
end

Instance Method Details

#reload(*args) ⇒ Object



29
30
31
32
# File 'lib/delayed/backend/mongoid.rb', line 29

def reload(*args)
  reset
  super
end