Added a locking mechanism to avoid jobs to be processed twice in some edge cases