jenkinsci / lockable-resources-plugin

Lock resources against concurrent use
https://plugins.jenkins.io/lockable-resources
MIT License
87 stars 185 forks source link

Aquire lock without using try-with-resources notation #589

Closed royteeuwen closed 11 months ago

royteeuwen commented 12 months ago

Describe your use-case which is not covered by existing documentation.

I'm trying to get a lock without using the try-with-resources notation, is this possible and if not through declarative then is it possible by using a scripted approach?

What I'm trying to achieve is the following:

seeing as Jenkins does not allow parallel in parallel, I cant switch it to:

-build stage

So having the option to start the lock X after the build stage and stop after test suites for X would be ideal, which is not feasible by using the lock(X) { } wrapping because then Y is also locked for the duration of the X test suites

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

Are you interested in contributing to the documentation?

Not sure if it's feasible, so can't really contribute :/

mPokornyETM commented 12 months ago

"seeing as Jenkins does not allow parallel in parallel, I cant switch it to:" it is not true, it is allowed, and it also works, but there are no plugins (even core) which can show it correctly

I am not pretty sure, wha you need, but in scripted pipeline you can use lock() in lock()

stage('Init') {
    echo 'hello world'
}

def stages = [:]
stages['suite1'] {
  node('windows') {
    doTest('TC-1');
    doTest('TC-2');
    doTest('TC-3');
  }
}
stages['suite2'] {
  node('debian') {
    doTest('TC-1');
    doTest('TC-2');
    doTest('TC-3');
  }
}
stages['suite2'] {
  node('RHEL') {
    doTest('TC-1');
    doTest('TC-2');
    doTest('TC-3');
  }
}

stage('Testing') {
    parallel stages;
}

void doTest(final String testCase) {
    lock('test-infra-' + testCase) {
        echo 'execute test case: ' + testCase
        sleep 60
        lock('Publish results') {
            sh 'echo "I am done ' + testCase + ' on node ' + env.NODE_NAME + '" >> /mnt/some/shared/file.txt'
        }
    }
}

When it is not what are you @royteeuwen looking for, pls paste here some diagram (UML) or somthing like that, to explain, what you want to reached.

royteeuwen commented 12 months ago

I indeed figured out in the meantime that scripted does allow parallel in parallel, it just doesnt visualise (correctly), which is a shame to be honest, it shouldn’t be difficult to visualise it correctly.

doing this approach does indeed achieve what I want, but just out of curiosity, it wasn’t an actual answar to what I was looking for, my question was rather, can i do the following in any way (scripted or not):

stage { lock(‘label’) do stuff } stage(2) { do sture unlock(‘label’) }

mPokornyETM commented 11 months ago

The answer is easy. Currently no. In the meaintime, there was few questions to do that like you propose, but in all cases we provide just an other working example and it was unnecessary. Why I dont like it. It is danguerous.

in your shart example will this works

lock(‘label’) {
  stage {
    do stuff
  }
  stage(2) {
    do sture
  }
}

But I don`t think, this is waht you want

royteeuwen commented 11 months ago

OK, I can definitely understand why it isn't ideal to do it!

In the meantime I already decided to rewrite it to parallel in parallel, meaning that I had to drop the declarative pipeline and replace it with scripted, plus blue ocean doesn't render it correctly anymore. But puristically it's more logical for it to be parallel in parallel, so very sad Jenkins doesn't support it correctly in declarative

Thanks for the help!