Script runner execution failure's

RichardA February 26, 2024

Hi, We have many scripts executing per day & few of them are failing due to some errors . We tried to create "Execution failure notifierlistener to trigger mail when ever listener got failed for continuous 5 times.

The problem is, mail is triggering on every event failed but my requirement is to trigger an email when script fails for 5 times. 

We are using below script:

import com.atlassian.mail.queue.SingleMailQueueItem
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email


// Initialize a counter for failures
def failureCount = ComponentAccessor.getApplicationProperties().getString("failureCount") as Integer ?: 0

// Maximum number of failures to track
def maxFailures = 5

// Event listener for script failures
//def onScriptFailure(event) {
    
    failureCount++
    ComponentAccessor.getApplicationProperties().setString("count", failureCount.toString())
    if (failureCount <= maxFailures) {
        // Send email notification
         ComponentAccessor.getApplicationProperties().setString("count", "0")
        def email = new Email('sonamsh9909@gmail.com')
        email.setSubject("Script Failure (${failureCount} of ${maxFailures})")
        //log.warn(" feature name 2 "+event.getFeatureName().findAll())
        email.setBody("""
            Notes: ${event.notes}.
            Something has gone wrong in ${event.featureName}.
        
            To edit this, click here: ${event.url}
            Error: ${event.stackTrace}
        """)


        SingleMailQueueItem item = new SingleMailQueueItem(email)
        ComponentAccessor.getMailQueue().addItem(item)
    }
Can some one help me how to make this script to trigger only when 5 failure counts.
Thanks!

1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
February 27, 2024

You have a few errors in there:

This line

if (failureCount <= maxFailures)

means that you'll only send the first 5 failures then stop.

In  your initial getApplicationProperty, you are fetching 'failureCount', but then storing just 'count'

This script will count global failures. If you have multiple configurations, it would be best to count failures on a per-config basis.

Try something like this:

import com.atlassian.mail.queue.SingleMailQueueItem
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.mail.Email

def configId = event.configId
def propKey = "scriptrunner.${configId}.failureCount"
def simpleFeatureName = event.featureName.tokenize('.').last()
// Initialize a counter for failures
def failureCount = ComponentAccessor.getApplicationProperties().getString(propKey) as Integer ?: 0

// Interval at which to send emails (send email every n failures)
def failureInterval = 5

failureCount++
ComponentAccessor.getApplicationProperties().setString(propKey, failureCount.toString())
if (failureCount == failureInterval) {
//reset the failure count to 0
ComponentAccessor.getApplicationProperties().setString(propKey, "0")

// Send email notification
def email = new Email('sonamsh9909@gmail.com')
email.setSubject("Script Failure $simpleFeatureName wih ID=$configId failed ${failureCount} times")
//log.warn(" feature name 2 "+event.getFeatureName().findAll())
email.setBody("""
Notes: ${event.notes}.
Something has gone wrong in ${event.featureName}.
ConfigId: $configId

To edit this, click here: ${event.url}
Error: ${event.stackTrace}
""")


SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}

 

You'll see that I'm defining "propKey" at the top and using that throughout the script. When you have literal strings, it's a good idea to list them only once when you can.

So now, each configId will have its own count.

I changed the maxFailures to failureInterval to reflect the different purpose.

And adjusted the if block to fire the email only when the failureCount matched the failureInterval.

Also, the email subject will now include the last part of the feature name and the configid. That should make your emails appear in threads (depending on your email client) based on each separate configurations.

RichardA February 28, 2024

HI @PD Sheehan : Thank you soo much for your help. I will test your script and will update you :)

 

Thanks!

RichardA March 5, 2024

Hi @PD Sheehan Thank you :)
Your script is working as expected. Now the mail is triggering only when 5 failure counts happened.

Suggest an answer

Log in or Sign up to answer