Module: ActiveJobLock::Core::OverriddenMethods

Defined in:
lib/active_job_lock/core.rb

Instance Method Summary collapse

Instance Method Details

#enqueue(*_) ⇒ Object

This method is abstract.

if the job is a ‘loner`, enqueue only if no other same job is already running/enqueued



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_job_lock/core.rb', line 72

def enqueue(*_)
  if loner
    if loner_locked?(*arguments)
      # Same job is currently running
      loner_enqueue_failed(*arguments)
      return
    else
      acquire_loner_lock!(*arguments)
    end
  end
  super
end

#perform(*arguments) ⇒ Object

Where the magic happens.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_job_lock/core.rb', line 87

def perform(*arguments)
  lock_until = acquire_lock!(*arguments)

  # Release loner lock as job has been dequeued
  release_loner_lock!(*arguments) if loner

  # Abort if another job holds the lock.
  return unless lock_until

  begin
    super(*arguments)
  ensure
    # Release the lock on success and error. Unless a lock_timeout is
    # used, then we need to be more careful before releasing the lock.
    now = Time.now.to_i
    if lock_until != true and lock_until < now
      # Eeek! Lock expired before perform finished. Trigger callback.
      lock_expired_before_release(*arguments)
    else
      release_lock!(*arguments)
    end
  end
end