예제 #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 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);
      }
    }
  }
  /**
   * Register cron job REST API
   *
   * @param message - JSON with cron expressions.
   * @return JSON with status.OK
   * @throws IOException, IllegalArgumentException
   */
  @POST
  @Path("cron/{notebookId}")
  @ZeppelinApi
  public Response registerCronJob(@PathParam("notebookId") String notebookId, String message)
      throws IOException, IllegalArgumentException {
    LOG.info("Register cron job note={} request cron msg={}", notebookId, message);

    CronRequest request = gson.fromJson(message, CronRequest.class);

    Note note = notebook.getNote(notebookId);
    if (note == null) {
      return new JsonResponse<>(Status.NOT_FOUND, "note not found.").build();
    }

    if (!CronExpression.isValidExpression(request.getCronString())) {
      return new JsonResponse<>(Status.BAD_REQUEST, "wrong cron expressions.").build();
    }

    Map<String, Object> config = note.getConfig();
    config.put("cron", request.getCronString());
    note.setConfig(config);
    notebook.refreshCron(note.id());

    return new JsonResponse<>(Status.OK).build();
  }
예제 #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());
        }
      }
    }
  }
예제 #6
0
파일: CI.java 프로젝트: ab-k/phresco
 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;
 }
 private void showCronFireTimes(CronExpression cron, Date startTime, int count) {
   logger.info("cron=" + cron);
   Date nextDate = startTime;
   int i = 0;
   while (i++ < count) {
     Date fireTime = cron.getTimeAfter(nextDate);
     logger.info("Next fireTime " + fireTime);
     nextDate = fireTime;
   }
 }
예제 #8
0
 /**
  * [used by Spring]
  *
  * @param cronExpression
  */
 public void setCronExpression(String cronExpression) {
   if (CronExpression.isValidExpression(cronExpression)) {
     this.cronExpression = cronExpression;
   } else {
     if (StringHelper.containsNonWhitespace(cronExpression)) {
       // was not empty, so someone tried to set someting here, let user know that it was garbage
       log.warn(
           "Configured cron expression is not valid::"
               + cronExpression
               + " check your search.indexing.cronjob.expression property",
           null);
     }
     this.cronExpression = null;
   }
 }
예제 #9
0
  public String getCron() {
    if (cronExpression != null) {
      return cronExpression;
    }

    String cron = null;
    if (tomcatId >= 980) {
      int shift = 360 + ((tomcatId - 980) * 2);
      long hours = TimeUnit.MINUTES.toHours(shift);
      long remainMinute = shift - TimeUnit.HOURS.toMinutes(hours);
      cron = "0 " + remainMinute + " " + hours + " * * ? *";
    } else {
      int shift = 120 + (tomcatId * 2);
      long hours = TimeUnit.MINUTES.toHours(shift);
      long remainMinute = shift - TimeUnit.HOURS.toMinutes(hours);
      cron = "0 " + remainMinute + " " + hours + " * * ? *";
    }
    if (CronExpression.isValidExpression(cron)) {
      return cron;
    }
    return "0 10 5 * * ?";
  }
  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);
  }