@Override public ConstraintProgramScheduleBuilder withDurationSinceLastRun(long duration, TimeUnit unit) { constraints.add(new LastRunConstraint(duration, unit)); return this; }
LastRunConstraint lastRunConstraint = new LastRunConstraint(1, TimeUnit.HOURS); ConstraintContext constraintContext = new ConstraintContext(job, now, store); assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext)); Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName()); setStartAndRunning(store, pid1, EMPTY_MAP, systemArgs); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); store.setResume(pid1, AppFabricTestHelper.createSourceId(++sourceId), -1); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); store.setStop(pid3, nowSec - TimeUnit.MINUTES.toSeconds(1), ProgramRunStatus.KILLED, AppFabricTestHelper.createSourceId(++sourceId)); assertSatisfied(true, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext)); assertSatisfied(false, lastRunConstraint.check(schedule, constraintContext));
@Override public ConstraintResult check(ProgramSchedule schedule, ConstraintContext context) { long startTime = TimeUnit.MILLISECONDS.toSeconds(context.getCheckTimeMillis() - millisSinceLastRun); // We only need to check program runs within recent history, adding a buffer of 1 day, because the time range // is for the start time of the program. It may start before `millisSinceLastRun`, but complete after it. // Note: this will miss out on active workflow runs that started more than ~1day ago (suspended/lengthy workflows) Iterable<RunRecordMeta> runRecords = context.getProgramRuns(schedule.getProgramId(), ProgramRunStatus.ALL, startTime - TimeUnit.DAYS.toSeconds(1), Long.MAX_VALUE, 100).values(); // We can limit to 100, since just 1 program in the recent history is enough to make the constraint fail. // We want use 100 as the limit instead of 1, because we want to attempt to get the latest completed run, // instead of just the first (in order to more accurately compute a next runtime if (Iterables.isEmpty(filter(runRecords, startTime))) { return ConstraintResult.SATISFIED; } if (!waitUntilMet) { return ConstraintResult.NEVER_SATISFIED; } return new ConstraintResult(ConstraintResult.SatisfiedState.NOT_SATISFIED, context.getCheckTimeMillis() + TimeUnit.SECONDS.toMillis(10)); }
@Override public ConstraintResult check(ProgramSchedule schedule, ConstraintContext context) { long startTime = TimeUnit.MILLISECONDS.toSeconds(context.getCheckTimeMillis() - millisSinceLastRun); // We only need to check program runs within recent history, adding a buffer of 1 day, because the time range // is for the start time of the program. It may start before `millisSinceLastRun`, but complete after it. // Note: this will miss out on active workflow runs that started more than ~1day ago (suspended/lengthy workflows) Iterable<RunRecordMeta> runRecords = context.getProgramRuns(schedule.getProgramId(), ProgramRunStatus.ALL, startTime - TimeUnit.DAYS.toSeconds(1), Long.MAX_VALUE, 100).values(); // We can limit to 100, since just 1 program in the recent history is enough to make the constraint fail. // We want use 100 as the limit instead of 1, because we want to attempt to get the latest completed run, // instead of just the first (in order to more accurately compute a next runtime if (Iterables.isEmpty(filter(runRecords, startTime))) { return ConstraintResult.SATISFIED; } if (!waitUntilMet) { return ConstraintResult.NEVER_SATISFIED; } return new ConstraintResult(ConstraintResult.SatisfiedState.NOT_SATISFIED, context.getCheckTimeMillis() + TimeUnit.SECONDS.toMillis(10)); }
@Override public ConstraintProgramScheduleBuilder withDurationSinceLastRun(long duration, TimeUnit unit) { constraints.add(new LastRunConstraint(duration, unit)); return this; }
@Test public void testConstraintCodec() { testSerDeser(new ProtoConstraint.ConcurrencyConstraint(3), new ConcurrencyConstraint(3)); testSerDeser(new ProtoConstraint.DelayConstraint(300000L, TimeUnit.MILLISECONDS), new DelayConstraint(300000L, TimeUnit.MILLISECONDS)); testSerDeser(new ProtoConstraint.LastRunConstraint(3600000L, TimeUnit.MILLISECONDS), new LastRunConstraint(3600000L, TimeUnit.MILLISECONDS)); testSerDeser(new ProtoConstraint.TimeRangeConstraint("02:00", "06:00", TimeZone.getDefault()), new TimeRangeConstraint("02:00", "06:00", TimeZone.getDefault())); }