/** * 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(); }
/** * [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; } }
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 * * ?"; }