wanderso
wanderso

Reputation: 1845

Is there a way to make jobs in Jenkins mutually exclusive?

I have a few jobs in Jenkins that use Selenium to modify a database through a website's front end. If some of these jobs run at the same time, errors due to dirty reads can result. Is there a way to force certain jobs in Jenkins to be unable to run at the same time? I would prefer not to have to place or pick up a lock on the database, which could be read or modified by any number of users who are also testing.

Upvotes: 31

Views: 11403

Answers (3)

pitseeker
pitseeker

Reputation: 2543

If you regard the database as a shared resource that can only be used exclusively then this fits the usecase of the Lockable resources plugin.

It is being actively developed and improved and is very versatile.

Upvotes: 3

recampbell
recampbell

Reputation: 1247

You want the Throttle Concurrent Builds plugin which lets you define global and per-node semaphores.

Locks and latches is being deprecated in favor of Throttle Concurrent builds.

Upvotes: 23

overthink
overthink

Reputation: 24443

I've tried both the locks & latches plugin and the port allocator plugin as ways to achieve what you're trying to do. Neither worked reliably for me. Locks & latches worked some of the time, but I'd occasionally get hung jobs. Using port allocator as a hack will work unless you have multiple jenkins nodes, but the config overhead is kind of high. What I've ultimately settled upon is another hack, but it works reliably and uses core Jenkins stuff (no plugins):

  • create a slave node running on the same box as the master (or not, if you have lots of boxes)
  • give this slave a single executor (key)
  • tie the 2 (or n) jobs that must not run together to this new slave node
  • optionally set the slave's usage to 'tied jobs only' if it'll screw up your other jobs if they happen to run on the new slave

Since the slave has only one executor, the jobs tied to it can never run together.

Upvotes: 4

Related Questions