@Override public Query<PostEntity> posts() { QueryBuilder<PostEntity> queryBuilder = module.newQueryBuilder(PostEntity.class); return module .currentUnitOfWork() .newQuery(queryBuilder) .orderBy(templateFor(PostEntity.class).postedAt(), Order.DESCENDING); }
@Override public Schedule scheduleOnce(Task task, DateTime runAt, boolean durable) { Schedule schedule = scheduleFactory.newOnceSchedule(task, runAt, durable); dispatchForExecution(schedule); if (durable) { Schedules schedules = module.currentUnitOfWork().get(Schedules.class, getSchedulesIdentity(me)); schedules.schedules().add(schedule); } return schedule; }
@Override public Schedule scheduleCron( Task task, @CronExpression String cronExpression, DateTime start, boolean durable) { Schedule schedule = scheduleFactory.newCronSchedule(task, cronExpression, start, durable); if (durable) { Schedules schedules = module.currentUnitOfWork().get(Schedules.class, getSchedulesIdentity(me)); schedules.schedules().add(schedule); } dispatchForExecution(schedule); return schedule; }
@Override public Iterable<TimelineRecord> getNextRecords(int maxResults) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity(scheduler); Schedules schedules = uow.get(Schedules.class, schedulesName); for (Schedule schedule : schedules.schedules()) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getNextRecords(maxResults); Iterables.addAll(result, lastRecords); } return Iterables.limit(maxResults, result); }
@Override public Schedule scheduleCron( Task task, String cronExpression, long initialDelay, boolean durable) { DateTime start = new DateTime(System.currentTimeMillis() + initialDelay); Schedule schedule = scheduleFactory.newCronSchedule(task, cronExpression, start, durable); if (durable) { Schedules schedules = module.currentUnitOfWork().get(Schedules.class, getSchedulesIdentity(me)); schedules.schedules().add(schedule); } dispatchForExecution(schedule); return schedule; }
@Override public Iterable<TimelineRecord> getRecords(DateTime from, DateTime to) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity(scheduler); Schedules schedules = uow.get(Schedules.class, schedulesName); for (Schedule schedule : schedules.schedules()) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getRecords(from, to); Iterables.addAll(result, lastRecords); } return result; }
@Override public Schedule scheduleOnce(Task task, int initialSecondsDelay, boolean durable) { long now = System.currentTimeMillis(); Schedule schedule = scheduleFactory.newOnceSchedule( task, new DateTime(now + initialSecondsDelay * 1000), durable); if (durable) { Schedules schedules = module.currentUnitOfWork().get(Schedules.class, getSchedulesIdentity(me)); schedules.schedules().add(schedule); } dispatchForExecution(schedule); return schedule; }
@Override public PostEntity postByIdentity(String identity) throws NoSuchEntityException { return module.currentUnitOfWork().get(PostEntity.class, identity); }