Class Delayed::Backend::ActiveRecord::Job
In: lib/delayed/backend/active_record.rb
Parent: ::ActiveRecord::Base

A job object that is persisted to the database. Contains the work object as a YAML field.

Methods

Included Modules

Delayed::Backend::Base

Public Class methods

[Source]

    # File lib/delayed/backend/active_record.rb, line 33
33:         def self.after_fork
34:           ::ActiveRecord::Base.connection.reconnect!
35:         end

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

[Source]

    # File lib/delayed/backend/active_record.rb, line 38
38:         def self.clear_locks!(worker_name)
39:           update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name])
40:         end

Get the current time (GMT or local depending on DB) Note: This does not ping the DB to get the time, so all your clients must have syncronized clocks.

[Source]

    # File lib/delayed/backend/active_record.rb, line 79
79:         def self.db_time_now
80:           if Time.zone
81:             Time.zone.now
82:           elsif ::ActiveRecord::Base.default_timezone == :utc
83:             Time.now.utc
84:           else
85:             Time.now
86:           end
87:         end

Find a few candidate jobs to run (in case some immediately get locked by others).

[Source]

    # File lib/delayed/backend/active_record.rb, line 43
43:         def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
44:           scope = self.ready_to_run(worker_name, max_run_time)
45:           scope = scope.scoped(:conditions => ['priority >= ?', Worker.min_priority]) if Worker.min_priority
46:           scope = scope.scoped(:conditions => ['priority <= ?', Worker.max_priority]) if Worker.max_priority
47:       
48:           ::ActiveRecord::Base.silence do
49:             scope.by_priority.all(:limit => limit)
50:           end
51:         end

Public Instance methods

Lock this job for this worker. Returns true if we have the lock, false otherwise.

[Source]

    # File lib/delayed/backend/active_record.rb, line 55
55:         def lock_exclusively!(max_run_time, worker)
56:           now = self.class.db_time_now
57:           affected_rows = if locked_by != worker
58:             # We don't own this job so we will update the locked_by name and the locked_at
59:             self.class.update_all(["locked_at = ?, locked_by = ?", now, worker], ["id = ? and (locked_at is null or locked_at < ?) and (run_at <= ?)", id, (now - max_run_time.to_i), now])
60:           else
61:             # We already own this job, this may happen if the job queue crashes.
62:             # Simply resume and update the locked_at
63:             self.class.update_all(["locked_at = ?", now], ["id = ? and locked_by = ?", id, worker])
64:           end
65:           if affected_rows == 1
66:             self.locked_at = now
67:             self.locked_by = worker
68:             self.locked_at_will_change!
69:             self.locked_by_will_change!
70:             return true
71:           else
72:             return false
73:           end
74:         end

[Validate]