Class: Cloudtasker::UniqueJob::Lock::UntilCompleted

Inherits:
BaseLock
  • Object
show all
Defined in:
lib/cloudtasker/unique_job/lock/until_completed.rb

Overview

Conflict if any other job with the same args is scheduled or moved to execution while the first job is pending or executing. Unlocks only on successful completion or when a DeadWorkerError is raised.

Instance Attribute Summary

Attributes inherited from BaseLock

#job

Instance Method Summary collapse

Methods inherited from BaseLock

#conflict_instance, #default_conflict_strategy, #initialize, #options

Constructor Details

This class inherits a constructor from Cloudtasker::UniqueJob::Lock::BaseLock

Instance Method Details

#execute(&block) ⇒ Object

Acquire a lock for the job and trigger a conflict if the lock could not be acquired.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cloudtasker/unique_job/lock/until_completed.rb', line 28

def execute(&block)
  job.lock!
  yield
  # Unlock on successful completion
  job.unlock!
rescue LockError
  conflict_instance.on_execute(&block)
rescue Cloudtasker::DeadWorkerError
  # Unlock when DeadWorkerError is raised
  job.unlock!
  raise
end

#schedule(&block) ⇒ Object

Acquire a lock for the job and trigger a conflict if the lock could not be acquired.



14
15
16
17
18
19
20
21
22
# File 'lib/cloudtasker/unique_job/lock/until_completed.rb', line 14

def schedule(&block)
  job.lock_for_scheduling!(&block)
rescue LockError
  conflict_instance.on_schedule(&block)
rescue StandardError
  # Unlock the job if any error arises during scheduling
  job.unlock!
  raise
end