public static CronTabList create(@Nonnull String format, Hash hash) throws ANTLRException { Vector<CronTab> r = new Vector<>(); int lineNumber = 0; String timezone = null; for (String line : format.split("\\r?\\n")) { lineNumber++; line = line.trim(); if(lineNumber == 1 && line.startsWith("TZ=")) { timezone = getValidTimezone(line.replace("TZ=","")); if(timezone != null) { LOGGER.log(Level.CONFIG, "CRON with timezone {0}", timezone); } else { throw new ANTLRException("Invalid or unsupported timezone '" + timezone + "'"); } continue; } if(line.length()==0 || line.startsWith("#")) continue; // ignorable line try { r.add(new CronTab(line,lineNumber,hash,timezone)); } catch (ANTLRException e) { throw new ANTLRException(Messages.CronTabList_InvalidInput(line,e.toString()),e); } } return new CronTabList(r); }
/** * Computes the nearest future timestamp that matches this cron tab. * <p> * More precisely, given the time 't', computes another smallest time x such that: * * <ul> * <li>x ≥ t (inclusive) * <li>x matches this crontab * </ul> * * <p> * Note that if t already matches this cron, it's returned as is. */ public Calendar ceil(long t) { Calendar cal = new GregorianCalendar(Locale.US); cal.setTimeInMillis(t); return ceil(cal); }
/** * Checks if this crontab entry looks reasonable, * and if not, return an warning message. * * <p> * The point of this method is to catch syntactically correct * but semantically suspicious combinations, like * "* 0 * * *" */ public String checkSanity() { for (CronTab tab : tabs) { String s = tab.checkSanity(); if(s!=null) return s; } return null; }
@Restricted(NoExternalUse.class) // just for form validation public @CheckForNull Calendar previous() { Calendar nearest = null; for (CronTab tab : tabs) { Calendar scheduled = tab.floor(tab.getTimeZone() == null ? Calendar.getInstance() : Calendar.getInstance(tab.getTimeZone())); if (nearest == null || nearest.before(scheduled)) { nearest = scheduled; } } return nearest; }
@Restricted(NoExternalUse.class) // just for form validation public @CheckForNull Calendar next() { Calendar nearest = null; for (CronTab tab : tabs) { Calendar scheduled = tab.ceil(tab.getTimeZone() == null ? Calendar.getInstance() : Calendar.getInstance(tab.getTimeZone())); if (nearest == null || nearest.after(scheduled)) { nearest = scheduled; } } return nearest; }
long bitMask = (i<4)?bits[i]:(long)dayOfWeek; for( int j=BaseParser.LOWER_BOUNDS[i]; j<=BaseParser.UPPER_BOUNDS[i]; j++ ) { if(!checkBits(bitMask,j)) { if (checkBits(bits[2], i)) { daysOfMonth++; String hashified = hashify(spec); if (hashified != null) { return Messages.CronTab_spread_load_evenly_by_using_rather_than_(hashified, spec);
private void set(String format, int line, Hash hash) throws ANTLRException { set(format, line, hash, null); }
long calculateDelay(final long currentTime, final BackupType backupType, final String cron) { CronTab cronTab; try { if (StringUtils.isEmpty(cron)) { return -1; } cronTab = new CronTab(cron); final Calendar nextExecution = cronTab.ceil(currentTime); final long delay = nextExecution.getTimeInMillis() - currentTime; LOGGER.fine(MessageFormat.format("Current time: {0,date,medium} {0,time,long}. Next execution ({3}) in {2} seconds which is {1,date,medium} {1,time,long}", new Date(currentTime), nextExecution.getTime(), TimeUnit.MILLISECONDS.toSeconds(delay), backupType)); if (delay < 0) { final String msg = "Delay is a negative number, which means the next execution is in the past! This happens for Hudson/Jenkins installations with version 1.395 or below. Please upgrade to fix this."; LOGGER.severe(msg); throw new IllegalStateException(msg); } return delay; } catch (final ANTLRException e) { LOGGER.warning(MessageFormat.format( "Cannot parse the specified ''Backup schedule for {0} backups''. Check cron notation.", backupType.toString())); return -1; } }
public FormValidation doCheckBackupSchedule(final StaplerRequest res, final StaplerResponse rsp, @QueryParameter("value") final String schedule) { if ((schedule != null) && !schedule.isEmpty()) { String message; try { message = new CronTab(schedule).checkSanity(); } catch (final ANTLRException e) { return FormValidation.error("Invalid cron schedule. " + e.getMessage()); } if (message != null) { return FormValidation.warning("Cron schedule warning: " + message); } else { return FormValidation.ok(); } } else { return FormValidation.ok(); } }
/** * Computes the nearest past timestamp that matched this cron tab. * <p> * More precisely, given the time 't', computes another smallest time x such that: * * <ul> * <li>x <= t (inclusive) * <li>x matches this crontab * </ul> * * <p> * Note that if t already matches this cron, it's returned as is. */ public Calendar floor(long t) { Calendar cal = new GregorianCalendar(Locale.US); cal.setTimeInMillis(t); return floor(cal); }
/** * Returns true if the given calendar matches */ boolean check(Calendar cal) { Calendar checkCal = cal; if(specTimezone != null && !specTimezone.isEmpty()) { Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone(specTimezone)); tzCal.setTime(cal.getTime()); checkCal = tzCal; } if(!checkBits(bits[0],checkCal.get(MINUTE))) return false; if(!checkBits(bits[1],checkCal.get(HOUR_OF_DAY))) return false; if(!checkBits(bits[2],checkCal.get(DAY_OF_MONTH))) return false; if(!checkBits(bits[3],checkCal.get(MONTH)+1)) return false; if(!checkBits(dayOfWeek,checkCal.get(Calendar.DAY_OF_WEEK)-1)) return false; return true; }
/** * Returns true if the given calendar matches */ public synchronized boolean check(Calendar cal) { for (CronTab tab : tabs) { if(tab.check(cal)) return true; } return false; }
void set(String format, Hash hash) throws ANTLRException { set(format,1,hash); }
int next = f.floor(this,cur); if (cur==next) continue; // this field is already in a good shape. move on to next
/** * Returns true if the given calendar matches */ boolean check(Calendar cal) { if(!checkBits(bits[0],cal.get(MINUTE))) return false; if(!checkBits(bits[1],cal.get(HOUR_OF_DAY))) return false; if(!checkBits(bits[2],cal.get(DAY_OF_MONTH))) return false; if(!checkBits(bits[3],cal.get(MONTH)+1)) return false; if(!checkBits(dayOfWeek,cal.get(Calendar.DAY_OF_WEEK)-1)) return false; return true; }
@Restricted(NoExternalUse.class) // just for form validation public @CheckForNull Calendar next() { Calendar nearest = null; for (CronTab tab : tabs) { Calendar scheduled = tab.ceil(tab.getTimeZone() == null ? Calendar.getInstance() : Calendar.getInstance(tab.getTimeZone())); if (nearest == null || nearest.after(scheduled)) { nearest = scheduled; } } return nearest; }
/** * Returns true if the given calendar matches */ public synchronized boolean check(Calendar cal) { for (CronTab tab : tabs) { if(tab.check(cal)) return true; } return false; }
@Restricted(NoExternalUse.class) // just for form validation public @CheckForNull Calendar previous() { Calendar nearest = null; for (CronTab tab : tabs) { Calendar scheduled = tab.floor(tab.getTimeZone() == null ? Calendar.getInstance() : Calendar.getInstance(tab.getTimeZone())); if (nearest == null || nearest.before(scheduled)) { nearest = scheduled; } } return nearest; }