@Override public void initialize(final StructrServices services, final Properties config) throws ClassNotFoundException, InstantiationException, IllegalAccessException { final String taskList = config.getProperty(TASKS, ""); if (taskList != null) { for (String task : taskList.split("[ \\t]+")) { String expression = config.getProperty(task.concat(EXPRESSION_SUFFIX)); if (expression != null) { CronEntry entry = CronEntry.parse(task, expression); if (entry != null) { logger.info("Adding cron entry {} for {}", new Object[] {entry, task}); cronEntries.add(entry); } else { logger.warn("Unable to parse cron expression for taks {}, ignoring.", task); } } else { logger.warn("No cron expression for task {}, ignoring.", task); } } } }
@Override public void run() { final Services servicesInstance = Services.getInstance(); // wait for service layer to be initialized while (!servicesInstance.isInitialized()) { try { Thread.sleep(1000); } catch (InterruptedException iex) { } } // sleep 5 seconds more try { Thread.sleep(5000); } catch (InterruptedException iex) { } while (doRun) { // sleep for some time try { Thread.sleep(GRANULARITY_UNIT.toMillis(GRANULARITY)); } catch (InterruptedException iex) { } for (CronEntry entry : cronEntries) { if (entry.getDelayToNextExecutionInMillis() < GRANULARITY_UNIT.toMillis(GRANULARITY)) { final String taskClassName = entry.getName(); final Class taskClass = instantiate(taskClassName); try { if (taskClass != null) { Task task = (Task) taskClass.newInstance(); logger.debug("Starting task {}", taskClassName); StructrApp.getInstance().processTasks(task); } else { try (final Tx tx = StructrApp.getInstance().tx()) { // check for schema method with the given name Actions.call(taskClassName, Collections.EMPTY_MAP); tx.success(); } } } catch (Throwable t) { logger.warn( "Exception while executing cron task {}: {}", taskClassName, t.getMessage()); } } } } }