/** * When triggered, records an inactive task state change. * * @param change Event when a task changes state. */ @Subscribe public void recordStateChange(TaskStateChange change) { if (Tasks.isTerminated(change.getNewState())) { long timeoutBasis = change.isTransition() ? clock.nowMillis() : Iterables.getLast(change.getTask().getTaskEvents()).getTimestamp(); registerInactiveTask( Tasks.getJob(change.getTask()), change.getTaskId(), calculateTimeout(timeoutBasis)); } }
@VisibleForTesting long calculateTimeout(long taskEventTimestampMillis) { return Math.max( settings.minRetentionThresholdMillis, settings.pruneThresholdMillis - Math.max(0, clock.nowMillis() - taskEventTimestampMillis)); }
@Override public boolean apply(IScheduledTask task) { return Tasks.getLatestEvent(task).getTimestamp() <= clock.nowMillis() - settings.minRetentionThresholdMillis; }