History Build Killer Issue with build configs of regular type

I am writing a plugin that uses a listener that is triggered at the start of each build. It then looks for previous PR builds that are still running and from the same branch, checks if the previous build is running, and if it is kills it. 


public KillBuildTC(@NotNull EventDispatcher<BuildServerListener> events) {

events.addListener(new BuildServerAdapter() {
public void buildStarted(@NotNull SRunningBuild build) {

BuildPromotion promotion = build.getBuildPromotion();
BuildPromotion previous = promotion.getPreviousBuildPromotion(SelectPrevBuildPolicy.SINCE_LAST_COMPLETE_BUILD);

if(previous == null){ //if no builds set before stop the listener
Loggers.SERVER.info("No previous builds.");

final SUser killbilld = build.getOwner();
SBuild prevBuild = previous.getAssociatedBuild();
if(prevBuild instanceof SRunningBuild){
SRunningBuild runningPrev = (SRunningBuild) prevBuild;
runningPrev.stop(killbilld,"History Build (redundant build) stopped");
} else {

This works great for build configs that are part of a composite build chain but when it gets to build configs that are type regular it does not work. 

1) a build is ran using the current most up to date branch in github after a commit is made 

2) after making a change to the previously mentioned branch and committing the changes a second build is ran

this makes the first build that is still running obsolete so my plugin kills the first build. This should be where it ends but 

3) a History Build is then created and ran it is triggered by Restarted;Git and has no changes in it. 


I don't know how to fix this so there isn't a restart; git trigger. Any Ideas? 



SRunningBuild::stop methods re-adds stopped build to the queue in case when the first parameter (user) is null (it works so to handle the cases when build is crashed, but not cancelled by a user) 

As a workaround, you may try to stop builds using Super User (UserModel::getSuperUser)


Thanks for the response!

could you explain what a super user is for me? 



Please sign in to leave a comment.