Ankit Automation,Jenkins Why my Jenkins instance is taking a lot of time to load ?

Why my Jenkins instance is taking a lot of time to load ?

Why my Jenkins instance is taking a lot of time to load ? post thumbnail image

Sometimes Jenkins take a lot of time to load. This is very annoying when one need to edit the jobs in Jenkins and there are performance issues just on loading home page.

Problem Statement

We were facing Jenkins slow loading issues and tried multiple options with no luck. We worked with the annoying slow loading homepage of Jenkins for several weeks and it remained to be the hot topic of our discussion for daily meetings.

Possible Causes of Performance Issues:

While working with Jenkins we faced performance issues multiple times, and it happens for many different reasons. Some of the most common reasons are as follows:

  1. Jenkins is starting up and taking time to get ready.
  2. Jenkins is writing long error dump files in filesystem filling up all the space hence not getting resources to run jobs
  3. Jenkins might be loaded with a lot of unnecessary plugins which might be slowing down the performance.

In our scenario, the Jenkins server took its time to startup and was ready after startup, we already checked that there is not disk space related issues and we compared the plugins of the server with plugins of newly setup server which wasn’t having the performance issues.

The only difference between new server and old server was that Jenkins on old server had run some jobs for 1000 to 10000 times and had a large job run logs.

Resolution

Keeping large history for all jobs isn’t necessary for us. We can store some of the recent job logs and delete old logs. Doing this manually isn’t a very good option as it will take a lot of time because hundreds to thousands of logs get generated everyday.

The only way is to do it through script or find logs in some folder and delete them. I am not sure if deleting the logs from a folder is a feasible option but we got some information that Jenkins can be managed through groovy script and we ran following groovy script to delete all job log except last 100 logs from all jobs in our Jenkins instance:

To run groovy in Jenkins

1) Open “Jenkins > Manage Jenkins > Script Console”

2) Copy paste following groovy script and run.

Jenkins.instance.getAllItems(Job.class).each( jobitem ->
    def jobName = jobitem.name
    def leaveLastXBuild = 100
    // if ( jobName[0..2] == "ab_" || jobName[0..2] == "bc_" ) { // filter based on jobname
    def job = Jenkins.instance.getItem(jobName)
    def lastBuild = job.getLastBuild()
    if (lastBuild != null){
        lastBuildNum = lastBuild.getNumber()
        print(jobName + " - "
        println lastBuildNum
        job.getBuilds().each { if(it.number <= (lastBuildNum - leaveLastXBuild)) {it.delete()}}
        job.save()
    }
    // } // filter based on job name closed.
}
    

You can modify the script to run for specific jobs only by filtering the jobs using different criteria. The commented lines in the groovy script can be used to match the exact jobName or compare some of it’s part as showin in comments.

Leave a Reply

Your email address will not be published. Required fields are marked *