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,362,610
Community Members
 
Community Events
168
Community Groups

Troubleshooting Running a Groovy Script as a Service (Scriptrunner)

Edited

I'm trying to figure out what I'm doing wrong to get a Scriptrunner groovy script running as a service, after reviewing posts such as these:

https://community.atlassian.com/t5/Answers-Developer-Questions/Unable-to-run-a-groovy-script-as-a-service-using-script-runner/qaq-p/503808 

https://scriptrunner.adaptavist.com/5.5.5/jira/services.html

For context, I'm running Jira Server 7.10.2 on a Linux box with the Scriptrunner version 5.5.5 plugin.

I've created a simple sanity-check script as follows, ensuring to set the logged in user to an admin user (obfuscated):

/*
This script is here for sanity-checking setting up a Jira service to execute
a groovy script
*/

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.*;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("******");
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user);

Logger log = Logger.getLogger("com.foo.groovyScripts");
message = "This is a test confirming groovy scripts run in Jira as a Service."
log.debug(message)
log.info(message)
log.warn(message)
log.error(message)

I can copy-paste this script and run it successfully from the Script Console, and I can see the output in the logs when I tail the atlassian-jira logs.

So, I've created a script in the scripts directory and have made sure it has both read and execute permissions, and that it is owned by the service account and that the file actually contains the script:

[foo@ip-123-456-789-321 scripts]# pwd
/path/to/jira/home/scripts

[foo@ip-123-456-789-321 scripts]# ls -la
...
-rwxr-xr-x 1 jira jira 620 Jun 10 12:18 test-service.groovy

[foo@ip-123-456-789-321 scripts]# cat test-service.groovy
/*
This script is here for sanity-checking setting up a Jira service to execute
a groovy script
*/

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.*;

ApplicationUser user = ComponentAccessor.getUserManager().getUserByName("*****");
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user);

Logger log = Logger.getLogger("com.foo.groovyScripts");
message = "This is a test confirming groovy scripts run in Jira as a Service."
log.debug(message)
log.info(message)
log.warn(message)
log.error(message)

I've also setup the script in Jira with the class:

com.onresolve.jira.groovy.GroovyService 

and cron expression (should run every 5 seconds):

 0/5 0/1 0 ? * * * 

And finally I've tried both the absolute and relative-to-script-roots paths:

/path/to/jira/home/scripts/test-service.groovy

test-service.groovy

However, the script just won't run. (I don't see anything in the logs)

In addition to my own package (com.foo.groovyScripts), I've also tried enabling logging on com.atlassian.jira.service, com.atlassian.jira.services, and com.onresolve.jira.groovy to see if I could find any information at the DEBUG level. I can see that some beans are being instantiated and script registered, however I'm seeing no error information.

2019-06-10 ... /secure/admin/EditService.jspa [c.o.jira.groovy.GroovyService] groovyService.init : input-file=/path/to/jira/home/scripts/test-service.groovy
2019-06-10 ... /secure/admin/ViewServices!default.jspa [c.o.j.groovy.groovyrunner.spring] BeforeInstantiation [bean=com.onresolve.jira.groovy.GroovyService, type=com.onresolve.jira.groovy.GroovyService]
2019-06-10 ... /secure/admin/ViewServices!default.jspa [c.o.j.groovy.groovyrunner.spring] AfterInitialisation [bean=com.onresolve.jira.groovy.GroovyService, type=com.onresolve.jira.groovy.GroovyService]

Is there anything else I can do to figure out why this is not running?

I'm not sure if there is something simple I'm missing (I've checked permissions on the files, user executing the script, existence of the file, content of the file, log levels, and service settings), or if this is a degrade/bug.

1 answer

1 accepted

0 votes
Answer accepted

Can you include screenshots of your service configuration screens?

Hover over the edit link... and grab the id in the url

Then check the scheduler details screen. Switch to Jobs tab and search for the id.

Then check if it's even registered. What the last run duration is and click show more to view the details and include them here if available.

We can go from there.

Based on what you're explaining, you shouldn't even need to set the logged in user, that really just depends on the tasks you need to do.

Thanks for your help, Peter! I forgot that tab existed, it was helpful in verifying that the script was in-fact registered and...running.

 

But I did find the problem with some help from ScriptRunner support, to my embarrassment: the cron expression. 

 

I was using https://www.freeformatter.com/cron-expression-generator-quartz.html and the expression it generated (0 * 0 ? * * *) wasn't correct. I was able to confirm the script was running every minute--but at hour 0 only (midnight). 

I recommend not using that one...

The person that helped me pointed me to http://www.cronmaker.com/, which actually provided the correct expression to run once per minute (0 0/1 * 1/1 * ? *).

Like Matthew Clark likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events