Пример #1
0
  private void expire() {
    if (expire == null) {
      clear();
    } else {
      if (expireTime > 0 && GungnirUtils.currentTimeMillis() >= expireTime) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("expire {} >= {}", GungnirUtils.currentTimeMillis(), expireTime);
        }
        clear();

        if (expire.getType() == IntervalType.CRON) {
          try {
            CronExpression cronExpr = new CronExpression(expire.getSchedulingPattern());
            expireTime =
                cronExpr
                    .getNextValidTimeAfter(new Date(GungnirUtils.currentTimeMillis()))
                    .getTime();
          } catch (ParseException e) {
            expireTime = 0;
            LOG.error("Failed to parse pattern", e);
          }
        } else {
          long expireMs = expire.getPeriod().getTimeUnit().toMillis(expire.getPeriod().getTime());
          expireTime += ((GungnirUtils.currentTimeMillis() - expireTime) / expireMs + 1) * expireMs;
        }
      }
    }
  }
Пример #2
0
 public Date[] testCronExpression(String expression) throws ParseException {
   Date[] dates = null;
   try {
     S_LOGGER.debug("Entering Method  CI.testCronExpression(String expression)");
     S_LOGGER.debug("testCronExpression() Expression = " + expression);
     final CronExpression cronExpression = new CronExpression(expression);
     final Date nextValidDate1 = cronExpression.getNextValidTimeAfter(new Date());
     final Date nextValidDate2 = cronExpression.getNextValidTimeAfter(nextValidDate1);
     final Date nextValidDate3 = cronExpression.getNextValidTimeAfter(nextValidDate2);
     final Date nextValidDate4 = cronExpression.getNextValidTimeAfter(nextValidDate3);
     dates = new Date[] {nextValidDate1, nextValidDate2, nextValidDate3, nextValidDate4};
   } catch (Exception e) {
     S_LOGGER.error(
         "Entered into catch block of CI.testCronExpression()"
             + FrameworkUtil.getStackTraceAsString(e));
   }
   return dates;
 }
Пример #3
0
  public String check() throws Exception {

    CronExpression cronExpression = new CronExpression(expression);

    Date date = new Date();

    for (int i = 0; i < 15; i++) {
      date = cronExpression.getNextValidTimeAfter(date);
      dateList.add(date);
    }
    return "success";
  }
  /** @see AbstractReportsTask#execute() */
  @Override
  public void execute() {

    // Retrieve the time at which this task was scheduled to execute, ignoring seconds
    Calendar currentCal = Calendar.getInstance();
    currentCal.setTimeInMillis(scheduledExecutionTime());
    currentCal.set(Calendar.SECOND, 0);
    Date currentTime = currentCal.getTime();

    ReportService rs = Context.getService(ReportService.class);

    log.debug("Executing the Queue Scheduled Reports Task");

    // First, identify if there are any scheduled report requests that should be run at this moment
    // If there are, clone the request and move it to the REQUESTED status.  If this is the last
    // time this scheduled report can run, move it into the COMPLETED status.
    for (ReportRequest scheduledReport : rs.getReportRequests(null, null, null, Status.SCHEDULED)) {
      try {
        String cronSchedule = scheduledReport.getSchedule();
        CronExpression cron = new CronExpression(cronSchedule);
        if (cron.isSatisfiedBy(currentTime)) {
          log.info(
              "Running scheduled report at "
                  + currentTime
                  + " which matches the schedule: "
                  + scheduledReport.getSchedule());
          ReportRequest newRequest = new ReportRequest();
          newRequest.setStatus(Status.REQUESTED);
          newRequest.setReportDefinition(scheduledReport.getReportDefinition());
          newRequest.setBaseCohort(scheduledReport.getBaseCohort());
          newRequest.setRenderingMode(scheduledReport.getRenderingMode());
          newRequest.setPriority(scheduledReport.getPriority());
          newRequest.setDescription(scheduledReport.getDescription());
          newRequest.setProcessAutomatically(true);
          rs.saveReportRequest(newRequest);
        }

        Date nextInvalidTime = cron.getNextInvalidTimeAfter(currentTime);
        Date nextValidTime = cron.getNextValidTimeAfter(nextInvalidTime);

        if (nextValidTime == null) {
          scheduledReport.setStatus(Status.SCHEDULE_COMPLETED);
          rs.saveReportRequest(scheduledReport);
        }
      } catch (Throwable t) {
        log.error("Failed to request scheduled report", t);
        scheduledReport.setStatus(Status.FAILED);
        rs.saveReportRequest(scheduledReport);
      }
    }
  }
Пример #5
0
  @Override
  protected void prepare() {
    if (interval.getType() == IntervalType.COUNT) {
      counter = 0;
    } else {
      if (snapshotJob == null) {
        snapshotJob = new SnapshotJob();
        try {
          if (interval.getType() == IntervalType.CRON) {
            getContext()
                .getComponent()
                .getShapshotTimer()
                .cronSchedule(interval.getSchedulingPattern(), snapshotJob);
          } else {
            getContext()
                .getComponent()
                .getShapshotTimer()
                .periodSchedule(interval.getPeriod(), snapshotJob);
          }
        } catch (SchedulerException e) {
          LOG.error("Failed to add schedule", e);
        }
      }

      snapshotJob.addTask(new CommitTask());

      if (expire != null) {
        if (interval.getType() == IntervalType.CRON) {
          CronExpression cronExpr;
          try {
            cronExpr = new CronExpression(expire.getSchedulingPattern());
            expireTime =
                cronExpr
                    .getNextValidTimeAfter(new Date(GungnirUtils.currentTimeMillis()))
                    .getTime();
          } catch (ParseException e) {
            LOG.error("Failed to parse pattern", e);
          }
        } else {
          expireTime =
              GungnirUtils.currentTimeMillis()
                  + expire.getPeriod().getTimeUnit().toMillis(expire.getPeriod().getTime());
        }
      }
    }
  }
  private Optional<Long> getNextRunAt(
      SingularityRequest request,
      RequestState state,
      SingularityDeployStatistics deployStatistics,
      PendingType pendingType) {
    final long now = System.currentTimeMillis();

    long nextRunAt = now;

    if (request.isScheduled()) {
      if (pendingType == PendingType.IMMEDIATE || pendingType == PendingType.RETRY) {
        LOG.info("Scheduling requested immediate run of {}", request.getId());
      } else {
        try {
          Date scheduleFrom = new Date(now);

          CronExpression cronExpression = new CronExpression(request.getQuartzScheduleSafe());

          final Date nextRunAtDate = cronExpression.getNextValidTimeAfter(scheduleFrom);

          if (nextRunAtDate == null) {
            return Optional.absent();
          }

          LOG.trace(
              "Calculating nextRunAtDate for {} (schedule: {}): {} (from: {})",
              request.getId(),
              request.getSchedule(),
              nextRunAtDate,
              scheduleFrom);

          nextRunAt =
              Math.max(
                  nextRunAtDate.getTime(),
                  now); // don't create a schedule that is overdue as this is used to indicate that
                        // singularity is not fulfilling requests.

          LOG.trace(
              "Scheduling next run of {} (schedule: {}) at {} (from: {})",
              request.getId(),
              request.getSchedule(),
              nextRunAtDate,
              scheduleFrom);
        } catch (ParseException pe) {
          throw Throwables.propagate(pe);
        }
      }
    }

    if (pendingType == PendingType.TASK_DONE
        && request.getWaitAtLeastMillisAfterTaskFinishesForReschedule().or(0L) > 0) {
      nextRunAt =
          Math.max(
              nextRunAt, now + request.getWaitAtLeastMillisAfterTaskFinishesForReschedule().get());

      LOG.trace(
          "Adjusted next run of {} to {} (by {}) due to waitAtLeastMillisAfterTaskFinishesForReschedule",
          request.getId(),
          nextRunAt,
          JavaUtils.durationFromMillis(
              request.getWaitAtLeastMillisAfterTaskFinishesForReschedule().get()));
    }

    if (state == RequestState.SYSTEM_COOLDOWN && pendingType != PendingType.NEW_DEPLOY) {
      final long prevNextRunAt = nextRunAt;
      nextRunAt =
          Math.max(
              nextRunAt,
              now + TimeUnit.SECONDS.toMillis(configuration.getCooldownMinScheduleSeconds()));
      LOG.trace(
          "Adjusted next run of {} to {} (from: {}) due to cooldown",
          request.getId(),
          nextRunAt,
          prevNextRunAt);
    }

    return Optional.of(nextRunAt);
  }