Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,295,598
Community Members
 
Community Events
165
Community Groups

possible full reindex schedule with scriptrunner?

According do the documentation it if a good idea to perform a full re-index once in a while, say once every two months. 

But typically this can only be performed when the system is not needed. This would ideally happen at the weekends in the evening. As no-one wants to open the Work Laptop to click full re-index, is it possible to automate it somehow? maybe with script runner?

I have searched and I do not see anythig for this, only for re-indexing issues in Projects, and not for the configuration.

3 answers

1 accepted

1 vote
Answer accepted
Clark Everson Community Leader Jan 19, 2022

Hi @ChristopherChilds 

I would not recommend trying to script re-indexing. I see you are on Server, so for now on your version one part of it is something could go wrong and you would want to make sure you have someone on call for it, thus having that person click the button is the way to go.

If you choose to stay locally hosted and not move to Cloud by 2024, then with data center it supports multiple nodes with zero down time. However, you need to take one node offline and sync it back up.

It is possible to automate through something like Ansible but I would recommend considering which path (Cloud versus Data Center) you plan on going with before investing time in automation of re-indexing as well as reporting you will need in case it fails.

Best,
Clark

Many thanks for the answer.

I had considered indexing to be a benign thing to do, and did wonder why there was no automation. This makes sense.

 

I have seen there a list of issues to look out for when performing re-index.

https://confluence.atlassian.com/jirakb/troubleshoot-index-problems-in-jira-server-203394752.html

Out of interest, how has your personal experience been with full re-indexing?
Can you provide some estimates of when to perform this.

This is a brief summary of our Server (I am not sure what relevant information is useful):

Users 1600

Projects: 371 (of which around 220 are considered active)

Can't get it to work.
Can you help?

2022-06-09 00:30:00,542 INFO [runner.ScriptBindingsManager]: Host: jira.customer.com 22022-06-09 00:30:00,637 INFO [runner.ScriptBindingsManager]: No index running 32022-06-09 00:30:00,637 INFO [runner.ScriptBindingsManager]: No reindex in progress. Attempt to start a new reindex 42022-06-09 00:30:00,668 INFO [runner.ScriptBindingsManager]: Reindex not started. com.atlassian.sal.api.net.ResponseStatusException: Unexpected response received. Status code: 411

Hi,

I have developed a ScriptRunner script that starts a background reindex using the reindex REST APIs. The script can then be set up as a SR job that runs e.g. once a week. 

For a full reindex just replace the type=BACKGROUND_PREFERRED with type=FOREGROUND

Hope it helps.

/**

 * Start Jira reindex through a REST API call

 * The script first verifies that there is no other reindex running

**/

import com.atlassian.sal.api.ApplicationProperties

import com.atlassian.sal.api.UrlMode

import com.atlassian.sal.api.net.Request

import com.atlassian.sal.api.net.TrustedRequestFactory

import com.onresolve.scriptrunner.runner.customisers.PluginModule

import groovy.json.JsonSlurper

import groovyx.net.http.URIBuilder

import com.atlassian.sal.api.net.Request

@PluginModule

TrustedRequestFactory trustedRequestFactory

@PluginModule

ApplicationProperties applicationProperties

final reindexAPI = '/rest/api/2/reindex?type=BACKGROUND_PREFERRED'

final reindexStatusAPI = '/rest/api/2/reindex/progress'

def urlIndexStatus = applicationProperties.getBaseUrl(UrlMode.CANONICAL) + reindexStatusAPI

def urlStartIndex = applicationProperties.getBaseUrl(UrlMode.CANONICAL) + reindexAPI

def requestIndexStatus = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, urlIndexStatus)

def requestStartIndex = trustedRequestFactory.createTrustedRequest(Request.MethodType.POST, urlStartIndex)

def host = new URIBuilder(urlIndexStatus).host

log.info("Host: " + host)

def indexRunning = false

try {

    requestIndexStatus.addTrustedTokenAuthentication(host)

    def responseBody = requestIndexStatus.execute()

    def responseAsMap = new JsonSlurper().parseText(responseBody) as Map

   

    if( (int) responseAsMap['currentProgress'] < 100){

        log.info("Index in progress. Percentage complete: "+ responseAsMap['currentProgress'])

        indexRunning = true

    }

}catch(def error){

    log.info("No index running")

}

if( !indexRunning ){

    log.info("No reindex in progress. Attempt to start a new reindex")

   

    try {

        requestStartIndex.addTrustedTokenAuthentication(host)

        def responseBody = requestStartIndex.execute()

        def responseAsMap = new JsonSlurper().parseText(responseBody) as Map

        log.info("Index started")

        log.info(responseAsMap)

    }catch(def error){

        log.info("Reindex not started. " + error)

    }

}else{

    log.info("NO new reindex started")

}

Hello @VF ,

Maybe you can help with Jakob error? Comment bellow. Maybe you know from the experience the root cause of that 411 error. 

It looks like the API calls are failing. And the explanation I could find in atlassian community is that this could be a proxy problem solvable by setting the content length of the call to 0. Please read more here:
https://community.atlassian.com/t5/Jira-questions/411-error-while-trying-to-POST-to-an-api/qaq-p/1064740

So what you could try is set an empty body to the request. For example by adding the following line right below line 58 requestStartIndex.addTrustedTokenAuthentication(host) :
requestStartIndex.setRequestBody("")

However I can not test if this works as I can not reproduce the error on our servers. Hope this helps.

Thanks for the replay.

I have changed to this, and now I get another Error code.

 

if( !indexRunning ){

    log.info("No reindex in progress. Attempt to start a new reindex")

    try {

        requestStartIndex.addTrustedTokenAuthentication(host)

        requestStartIndex.setRequestBody("") //Added 

        def responseBody = requestStartIndex.execute()

        def responseAsMap = new JsonSlurper().parseText(responseBody) as Map

        log.info("Index started")

        log.info(responseAsMap)

 

    }catch(def error){

        log.info("Reindex not started. " + error)

    }

 

}else{

    log.info("NO new reindex started")

}

Error that I get:

2022-06-24 13:51:41,777 INFO [runner.ScriptBindingsManager]: Host: jira.customer.com

2022-06-24 13:51:41,793 INFO [runner.ScriptBindingsManager]: No index running

2022-06-24 13:51:41,793 INFO [runner.ScriptBindingsManager]: No reindex in progress. Attempt to start a new reindex

2022-06-24 13:51:41,809 INFO [runner.ScriptBindingsManager]: Reindex not started. com.atlassian.sal.api.net.ResponseStatusException: Unexpected response received. Status code: 415

Br Jakob

Hi Jakob

Please replace the line you have just added with these 2:

requestStartIndex.addHeader("Content-Type", "application/json")
requestStartIndex.setRequestBody(new JsonBuilder([]).toString())

 

These should handle the content format.

Hi

And it works :-)

I had to import the class, though.

import groovy.json.JsonBuilder //Added

Thank you for helping, it's appreciated.

Br Jakob

Like VF likes this

Hi Jakob

Yes, the JsonBuilder library is needed. Glad you figured that one out and that it works. 

Hi Jakob. I have sent you a LinkedIn request.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.2
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you