Class: ForemanTasks::Lock

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/foreman_tasks/lock.rb

Defined Under Namespace

Classes: LockConflict

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.colliding_locks(resource, task, *_lock_names) ⇒ Object



72
73
74
# File 'app/models/foreman_tasks/lock.rb', line 72

def colliding_locks(resource, task, *_lock_names)
  build(resource, task).colliding_locks.to_a
end

.exclusive!(resource, task) ⇒ Object

Locks the resource so that no other task can lock it while running. No other task related to the resource is not allowed (even not-locking ones) A typical usecase is resource deletion, where it’s good idea to make sure nothing else related to the resource is really running. It also creates a Link between the task and the resource and between the task and all related resources.



60
61
62
63
64
65
# File 'app/models/foreman_tasks/lock.rb', line 60

def exclusive!(resource, task)
  lock = build(resource, task)
  lock.save!
  ForemanTasks::Link.link_resource_and_related!(resource, task)
  lock
end

.lock!(resource, task, *_lock_names) ⇒ Object

See #exclusive!



68
69
70
# File 'app/models/foreman_tasks/lock.rb', line 68

def lock!(resource, task, *_lock_names)
  exclusive!(resource, task)
end

Instance Method Details

#colliding_locksObject

returns a scope of the locks colliding with this one



40
41
42
43
44
45
# File 'app/models/foreman_tasks/lock.rb', line 40

def colliding_locks
  task_ids = task.self_and_parents.map(&:id)
  colliding_locks_scope = Lock.where(Lock.arel_table[:task_id].not_in(task_ids))
  colliding_locks_scope.where(resource_id:   resource_id,
                              resource_type: resource_type)
end

#save!Object



47
48
49
50
51
# File 'app/models/foreman_tasks/lock.rb', line 47

def save!
  super
rescue ActiveRecord::RecordNotUnique
  raise LockConflict.new(self, colliding_locks)
end