I have a groovy script which runs as a service after every 1 minute. The groovy service runs properly.
In this groovy script we have to date-time fields named- Shutdown Start Time and Shutdown End Time for a ticket. I want to trigger an email after ever 120 mins (2hrs) of the Shutdown Start Time until current time is less than Shutdown End Time, which is the future date.
Below is the groovy script to get the elapsed minutes between the current time and the Shutdown Time. However, I am not able to figure out how can I check if the the elapsed minutes are in multiples 120 mins.
import java.util.Date
import groovy.time.*
import java.util.*
import java.util.concurrent.TimeUnit
import java.util.Calendar
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomField downTimeStart = customFieldManager.getCustomFieldObjectByName("Shutdown Start Time")
CustomField downTimeEnd = customFieldManager.getCustomFieldObjectByName("Shutdown End Time")
Date downTimeStartCFDate = (Date)issue.getCustomFieldValue(downTimeStart) //Get the Shutdown Start Time
Date downTimeEndCFDate = (Date)issue.getCustomFieldValue(downTimeEnd) //Get the Shutdown End Time
//Get elapsed time between the current time and the Shutdown Start Time
TimeDuration startDuration = TimeCategory.minus(new Date(), downTimeStartCFDate)
long downtimeStartElapsedMinutes =TimeUnit.MILLISECONDS.toMinutes(startDuration.toMilliseconds())
//Get elapsed time between the current time and the Shutdown End Time
TimeDuration endDuration = TimeCategory.minus(downTimeEndCFDate, downTimeStartCFDate)
long downtimeEndRemaniningMinutes = TimeUnit.MILLISECONDS.toMinutes(endDuration.toMilliseconds())
//This sample 'if' condition checks if 12 minutes are elapsed.
if(downtimeStartElapsedMinutes % 3 == 0 && downtimeStartElapsedMinutes % 4 == 0)
{
//Send an email
}
The above groovy script is implemented as a service which runs after every minute. In this groovy script I just need to check if the elapsed time between the current time and the Shutdown Start Time is 120 mins (2 hrs) until the future Shutdown End Time date arrives.
That is, let's say -
Downtime Start - 2014-10-06 23:00:00.0
Downtime End - 2014-10-07 18:51:00.0
Then, The groovy service which will run after every 1 minute should trigger emails after every 2 hrs from the date of Downtime Start till the Downtime End datetime arrives -
Downtime Start - 2014-10-06 23:00:00.0
1st Email Sent - 2014-10-07 01:00:00.0
2nd Email Sent - 2014-10-07 03:00:00.0
3rd Email Sent - 2014-10-07 05:00:00.0
4th Email Sent - 2014-10-07 07:00:00.0
5th Email Sent - 2014-10-07 09:00:00.0
6th Email Sent - 2014-10-07 11:00:00.0
7th Email Sent - 2014-10-07 13:00:00.0
8th Email Sent - 2014-10-07 15:00:00.0
9th Email Sent - 2014-10-07 17:00:00.0
Downtime End - 2014-10-07 18:51:00.0
It would be of very help if anyone can share inputs on this as I am not familiar with the groovy scripting.