`before_transition` triggered multiple times because of optimistic locking

Description

Our Ruby code for auto-retrying a build is defined in before_transition callback, and it appears that before_transition can be called multiple twice when using optimistic locking and reapplying the code when lock has been found.

This can lead to unexpected results.

Edited by Grzegorz Bizon