Programmatically disabling scriptrunner Jobs, Listeners and Behaviours

Daniel Garcia June 4, 2023

How can I programmatically disable scriptrunner jobs, listeners and behaviours?

3 answers

2 accepted

3 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 4, 2023

Hi @Daniel Garcia

I assume you have many of these configurations and no direct approach to disable them in one go. But to answer your question. This is not doable.

You will have to disable your jobs, listeners and behaviours manually one by one.

You can, however, create an Improvement / New Feature Request via our support porta.

Thank you and Kind regards,

Ram

Daniel Garcia June 4, 2023

Ken's approach below seems like a reasonable approach to me

1 vote
Answer accepted
Ken McClean
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 4, 2023

Here's an example script I wrote that disables a ScriptRunner job. If you want to extend the functionality or get the specifics for your script, you'll need to watch the network traffic in your browser when you send a "stop" command to a listener, etc.  That'll give you what you need to rewrite the code


 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.config.properties.APKeys

import com.atlassian.sal.api.net.Response

import com.atlassian.sal.api.net.ResponseException

import com.atlassian.sal.api.net.ReturningResponseHandler

import com.atlassian.sal.api.net.TrustedRequest

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

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

import groovy.json.JsonSlurper

import groovyx.net.http.ContentType

import groovyx.net.http.URIBuilder

import java.net.URL;

import java.nio.file.Path;

 

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)

def trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)

 

def payload = ('{"@class":"com.onresolve.scriptrunner.scheduled.model.JiraCustomScheduledJobCommand","id":"a1c46fd2-0b69-4eeb-b955-8787a391db35","version":1,"ownedBy":null,"scheduleType":"CRON_EXPRESSION","disabled":true,"name":"Custom scheduled job","nextRunTime":1669391294170,"FIELD_USER_ID":"admin","FIELD_INTERVAL":"60","FIELD_NOTES":"Test job","FIELD_RUN_ONCE_DATE":null,"FIELD_JOB_CODE":{"script":"log.warn(\"this is a test job\")","scriptPath":null,"parameters":{}},"canned-script":"com.onresolve.scriptrunner.canned.jira.jobs.JiraCustomScheduledJob"}')

def endPointPath = '/rest/scriptrunner/latest/scheduled-jobs/com.onresolve.scriptrunner.canned.jira.jobs.JiraCustomScheduledJob'

def url = baseUrl + endPointPath

 

def request = trustedRequestFactory.createTrustedRequest(Request.MethodType.POST, url) as TrustedRequest

request.addTrustedTokenAuthentication(new URIBuilder(baseUrl).host, currentUser.name)

request.addHeader("Content-Type", ContentType.JSON.toString())

request.addHeader("X-Atlassian-Token", 'no-check')

request.setRequestBody(payload)

 

def response = request.executeAndReturn(new ReturningResponseHandler < Response, Object > () {

  Object handle(Response response) throws ResponseException {

if (response.statusCode != HttpURLConnection.HTTP_OK) {

   log.error "Received an error while posting to the rest api. StatusCode=$response.statusCode. Response Body: $response.responseBodyAsString"

   return null

} else {

   def jsonResp = new JsonSlurper().parseText(response.responseBodyAsString)

   log.warn("REST API reports success: $jsonResp")

   return jsonResp

}

  }

})

 

 

 

Daniel Garcia June 4, 2023

Thanks, that is perfect

0 votes
Radek Janata
Contributor
September 13, 2023

I needed to disable jobs (when replicating production Jira environment to the testing one).

After the replication is done and before starting the new Jira on testing environment, I just run the following SQL update (MySQL):

UPDATE AO_4B00E6_STASH_SETTINGS SR
SET SR.SETTING = REPLACE(SR.SETTING, '"disabled":false', '"disabled":true')
WHERE SR.KEY = 'scheduled_jobs';

However, after Jira is started, all jobs seem to be disabled (greyed out) but they're still enabled. I have to go manually to Jobs, open and edit a job (just open and save) and all is OK. Not sure how to work around this.

Suggest an answer

Log in or Sign up to answer