0

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.

1
  • so you want to send email every 2 hrs until the end time?
    – evanwong
    Commented Oct 1, 2014 at 14:47

1 Answer 1

1

isn't it way too complicated?

final long TWO_HOURS_MILIS = 2 * 60 * 60 * 1000l

Date downTimeStartCFDate = (Date)issue.getCustomFieldValue(downTimeStart)
Date downTimeEndCFDate = (Date)issue.getCustomFieldValue(downTimeEnd)
long now = System.currentTimeMillis()
long delta = now - downTimeStartCFDate.time

// fire between 1:59 and 2:00
if( delta % TWO_HOURS_MILIS >= TWO_HOURS_MILLIS - 60000 && now < downTimeEndCFDate.time ){
  sendEmail()
}
3
  • Thanks @injecteer. But I believe here in the if block the email will be triggered only for the first time when 2 hrs are elapsed after the Downtime Start. This email triggering process needs to be incremental which will send email after every 2 hrs from Downtime Start until Downtime End date-time approaches.
    – Naren
    Commented Oct 6, 2014 at 5:57
  • 1
    I think your casts to (Date) are superfluous. That cast will happen automatically because the variables have static a static type of Date. Commented Oct 6, 2014 at 14:08
  • I copied the original :)
    – injecteer
    Commented Oct 6, 2014 at 14:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.