Skip to content

Instantly share code, notes, and snippets.

@snallami
Forked from andyjones/kill-scm-threads.groovy
Created January 6, 2017 20:27
Show Gist options
  • Save snallami/a17d1a141892b09e27210f6018bf4c5e to your computer and use it in GitHub Desktop.
Save snallami/a17d1a141892b09e27210f6018bf4c5e to your computer and use it in GitHub Desktop.
Kills long running SCM polling threads in Jenkins
Jenkins.instance.getTrigger("SCMTrigger").getRunners().each()
{
item ->
long millis = Calendar.instance.time.time - item.getStartTime()
if(millis > (1000 * 60 * 60)) // 1000 millis in a second * 60 seconds in a minute * 3 minutes
{
Thread.getAllStackTraces().keySet().each()
{
tItem ->
if (tItem.getName().contains("SCM polling") && tItem.getName().contains(item.getTarget().name))
{
println(item.getTarget().name)
println(item.getDuration())
println(item.getStartTime())
println "Interrupting thread " + tItem.getName();
tItem.interrupt()
}
}
}
}
@snallami
Copy link
Author

snallami commented Jan 6, 2017

@snallami
Copy link
Author

Thread.getAllStackTraces().keySet().each() {
item ->
if (item.getName().contains("SCM polling") && item.getName().contains("waiting for hudson.remoting")) {
println "Interrupting thread " + item.getId();
item.interrupt()
}
}

@snallami
Copy link
Author

import java.lang.management.*;

def threadBean = ManagementFactory.getThreadMXBean();
def osBean = ManagementFactory.getOperatingSystemMXBean();

println "\n\n\n[Checking state of (master)]";

println "Current CPU Time used by Jenkins: " + threadBean.getCurrentThreadCpuTime() + "ns";

double processLoad = (osBean.getProcessCpuLoad() * 100).round(2);
double cpuLoad = (osBean.getSystemCpuLoad() * 100).round(2);
println "Process CPU Load: " + processLoad + "%";
println "CPU Load: " + cpuLoad + "%";

if (processLoad < 90) {
println "\n\n\n === Load is less than 90%, nothing to do ===\n\n\n";
println "\n\n\n[Done checking: CPU Load: " + cpuLoad + "%]\n\n\n";
return;
} else {
println "\n\n\n === Load is more than 90%, checking for stuck threads! ===\n\n\n";
}

println "\n\n\n[Checking all threads]\n\n\n";
def threadNum = 0;
def killThreadNum = 0;

def stacktraces = Thread.getAllStackTraces();
stacktraces.each { thread, stack ->
if (thread.getName().contains("trigger/TimerTrigger/check") ) {
println "=== Interrupting thread " + thread.getName()+ " ===";
thread.interrupt();
killThreadNum++;
}
threadNum++;
}

println "\n\n\n[Done checking: " + threadNum + " threads, killed " + killThreadNum + "]\n\n\n";

return; // Suppress groovy state dump

@snallami
Copy link
Author

Thread.getAllStackTraces().keySet().each() {
item ->
if (item.getName().contains("Computer.threadPoolForRemoting")) {
println "Interrupting thread " + item.getId();
item.interrupt()
}
}

@snallami
Copy link
Author

Thread.getAllStackTraces().keySet().each(){ item ->
if(item.getName().contains("SCM polling")){ println "Interrupting thread " + item.getId() + " " + item.getName(); item.interrupt() }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment