Hi, We have many scripts executing per day & few of them are failing due to some errors . We tried to create "Execution failure notifier" listener 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) }
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.
HI @PD Sheehan : Thank you soo much for your help. I will test your script and will update you :)
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @PD Sheehan Thank you :)
Your script is working as expected. Now the mail is triggering only when 5 failure counts happened.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.