Skip to content

Commit

Permalink
Revert to string time representation and DateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
grkvlt committed Apr 6, 2017
1 parent c910c06 commit de72251
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.brooklyn.policy.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -56,6 +59,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp

private static final Logger LOG = LoggerFactory.getLogger(AbstractScheduledEffectorPolicy.class);

private static final String TIME_FORMAT = "HH:mm:ss";
private static final DateFormat FORMATTER = SimpleDateFormat.getTimeInstance();

public static final ConfigKey<String> EFFECTOR = ConfigKeys.builder(String.class)
.name("effector")
.description("The effector to be executed by this policy")
Expand All @@ -69,9 +75,9 @@ public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy imp
.defaultValue(ImmutableMap.<String, Object>of())
.build();

public static final ConfigKey<Date> TIME = ConfigKeys.builder(Date.class)
public static final ConfigKey<String> TIME = ConfigKeys.builder(String.class)
.name("time")
.description("An optional time when this policy should be first executed")
.description("An optional time when this policy should be first executed, formatted as HH:mm:ss")
.build();

public static final ConfigKey<Duration> WAIT = ConfigKeys.builder(Duration.class)
Expand Down Expand Up @@ -113,15 +119,22 @@ protected Effector<?> getEffector() {
return effector.get();
}

protected Duration getWaitUntil(Date time) {
Calendar now = Calendar.getInstance();
Calendar when = Calendar.getInstance();
when.setTime(time);
when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
if (when.before(now)) {
when.add(Calendar.DATE, 1);
protected Duration getWaitUntil(String time) {
try {
Calendar now = Calendar.getInstance();
Calendar when = Calendar.getInstance();
boolean formatted = time.contains(":"); // FIXME deprecated TimeDuration coercion
Date parsed = formatted ? FORMATTER.parse(time) : new Date(Long.parseLong(time) * 1000);
when.setTime(parsed);
when.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
if (when.before(now)) {
when.add(Calendar.DATE, 1);
}
return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis()));
} catch (ParseException | NumberFormatException e) {
LOG.warn("{}: Time should be formatted as {}: {}", new Object[] { this, TIME_FORMAT, e.getMessage() });
throw Exceptions.propagate(e);
}
return Duration.millis(Math.max(0, when.getTimeInMillis() - now.getTimeInMillis()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void onEvent(SensorEvent<Object> event) {
Boolean start = (Boolean) event.getValue();
if (start && running.compareAndSet(false, true)) {
Duration period = Preconditions.checkNotNull(config().get(PERIOD), "The period must be configured for this policy");
Date time = config().get(TIME);
String time = config().get(TIME);
Duration wait = config().get(WAIT);
if (time != null) {
wait = getWaitUntil(time);
Expand All @@ -103,7 +103,6 @@ public void onEvent(SensorEvent<Object> event) {
LOG.debug("{}: Scheduling {} every {} in {}", new Object[] { PeriodicEffectorPolicy.this, effector.getName(),
Time.fromDurationToTimeStringRounded().apply(period), Time.fromDurationToTimeStringRounded().apply(wait) });
executor.scheduleAtFixedRate(PeriodicEffectorPolicy.this, wait.toMilliseconds(), period.toMilliseconds(), TimeUnit.MILLISECONDS);
LOG.debug("{}: Scheduled", PeriodicEffectorPolicy.this);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setEntity(final EntityLocal entity) {
subscriptions().subscribe(entity, INVOKE_IMMEDIATELY, handler);
subscriptions().subscribe(entity, INVOKE_AT, handler);

Date time = config().get(TIME);
String time = config().get(TIME);
Duration wait = config().get(WAIT);
if (time != null) {
scheduleAt(time);
Expand All @@ -78,7 +78,7 @@ public void setEntity(final EntityLocal entity) {
}
}

protected void scheduleAt(Date time) {
protected void scheduleAt(String time) {
Duration wait = getWaitUntil(time);
LOG.debug("{}: Scheduling {} at {} (in {})", new Object[] { this, effector.getName(), time, Time.fromDurationToTimeStringRounded().apply(wait) });
executor.schedule(this, wait.toMilliseconds(), TimeUnit.MILLISECONDS);
Expand All @@ -90,7 +90,7 @@ public void onEvent(SensorEvent<Object> event) {
synchronized (mutex) {
LOG.debug("{}: Got event {}", ScheduledEffectorPolicy.this, event);
if (event.getSensor().getName().equals(INVOKE_AT.getName())) {
Date time = (Date) event.getValue();
String time = (String) event.getValue();
if (time != null) {
scheduleAt(time);
}
Expand Down

0 comments on commit de72251

Please sign in to comment.