ActiveRecord::StaleObjectError: Attempted to update a stale object: Ci::Build.

Problem

It seems sometimes the system fails to update the pipeline jobs that related to CI resource group. We should handle the race condition correctly with OptimisticLocking module to process all resource depended builds correctly.

Sentry

https://sentry.gitlab.net/gitlab/gitlabcom/issues/2472904/?referrer=gitlab_plugin

ActiveRecord::StaleObjectError: Attempted to update a stale object: Ci::Build.
  active_record/locking/optimistic.rb:95:in `_update_row'
    raise ActiveRecord::StaleObjectError.new(self, attempted_action)
  active_record/persistence.rb:918:in `_update_record'
    affected_rows = _update_row(attribute_names)
  active_record/attribute_methods/dirty.rb:205:in `_update_record'
    affected_rows = super
  active_record/callbacks.rb:335:in `block in _update_record'
    _run_update_callbacks { super }
  active_support/callbacks.rb:135:in `run_callbacks'
    env.value = !env.halted && (!block_given? || yield)
...
(151 additional frame(s) were not displayed)

Attempted to update a stale object: Ci::Build.
Edited by Shinya Maeda