private JobConfig(JobRunnerKey jobRunnerKey, @Nullable RunMode runMode, @Nullable Schedule schedule, Map<String, Serializable> parameters) { this.jobRunnerKey = jobRunnerKey; this.runMode = (runMode != null) ? runMode : RUN_ONCE_PER_CLUSTER; this.schedule = (schedule != null) ? schedule : Schedule.runOnce(null); // handled by withParameters and would be redundant elsewhere //noinspection AssignmentToCollectionOrArrayFieldFromParameter this.parameters = parameters; }
/** * Creates a new job configuration for the specified job runner key. * <p> * By default, the job configuration will assume: * </p> * <ul> * <li><code>{@link #withRunMode(RunMode) withRunMode}({@link RunMode#RUN_ONCE_PER_CLUSTER})</code></li> * <li><code>{@link #withSchedule(Schedule) withSchedule}({@link Schedule#runOnce(Date) Schedule.runOnce}(new Date()))</code></li> * <li><code>{@link #withParameters(Map) withParameters}(null)</code></li> * </ul> * <p> * Any of which may be overridden by calling the appropriate method. Note that chaining the * methods is recommended, as these methods return an altered copy rather than modifiying the * original {@code JobConfig} in place. For example, use: * </p> * <pre><code> * JobConfig config = JobConfig.forJobRunnerKey("myJobToDoImportantThings") * .withSchedule(Schedule.forInterval(Date, long)) * .withRunMode(RunMode.RUN_LOCALLY); * </code></pre> * * @param jobRunnerKey the unique identifier used to * {@link SchedulerService#registerJobRunner(JobRunnerKey, JobRunner) register} * the {@link JobRunner} * @return a job configuration for the specified job runner key that will use the default settings */ public static JobConfig forJobRunnerKey(JobRunnerKey jobRunnerKey) { requireNonNull(jobRunnerKey, "jobRunnerKey"); return new JobConfig(jobRunnerKey, RUN_ONCE_PER_CLUSTER, Schedule.runOnce(null), NO_PARAMETERS); }
private void queueDelayedEvent(final JiraHomeChangeEvent event) { delayedFileReplicationQueue.add(event); JobConfig jobConfig = JobConfig.forJobRunnerKey(DELAYED_REPLICATION_KEY) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.runOnce(DateTime.now().plusSeconds(IDLE_SECONDS).toDate())); try { schedulerService.scheduleJob(DELAYED_REPLICATION_ID, jobConfig); } catch (SchedulerServiceException e) { log.error("Failed to schedule delayed replication", e); } }
@Override public void triggerPoll(final long directoryID, final SynchronisationMode synchronisationMode) { JobConfig config = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withRunMode(RUN_ONCE_PER_CLUSTER) .withSchedule(Schedule.runOnce(null)) .withParameters(ImmutableMap.<String, Serializable>of("DIRECTORY_ID", directoryID)); try { schedulerService.scheduleJobWithGeneratedId(config); } catch (SchedulerServiceException e) { throw new RuntimeException(e); } }
@Override @Nonnull public Status scheduleDelayedUpgrades(final int delayMins, final boolean isAllowReindex) { final Schedule schedule = Schedule.runOnce(DateTime.now().plusMinutes(delayMins).toDate()); JobConfig jobConfig = JobConfig.forJobRunnerKey(JOB_RUNNER_KEY) .withSchedule(schedule) .withParameters(ImmutableMap.<String, Serializable>of(REINDEX_ALLOWED_PROPERTY, isAllowReindex)); try { schedulerService.scheduleJob(JOB_ID, jobConfig); } catch (SchedulerServiceException e) { log.error( "Unable to schedule upgrade tasks for future execution :" + e, e); final Collection<String> errors = ImmutableList.of( "Unable to schedule upgrade tasks for future execution: " + e.getMessage() + '\n' + ExceptionUtils.getStackTrace(e)); return new Status(errors); } return Status.OK; }
/** * Request a full replication of files in the home directories that are configured for replication. */ public void replicateJiraHome() throws SchedulerServiceException { JobConfig jobConfig = JobConfig.forJobRunnerKey(FULL_REPLICATION_KEY) .withRunMode(RunMode.RUN_ONCE_PER_CLUSTER) .withSchedule(Schedule.runOnce(null)); schedulerService.scheduleJob(FULL_REPLICATION_ID, jobConfig); }
@Test public void testSchedulerServiceJob() throws Exception { final CountDownLatch latch = new CountDownLatch(1); schedulerService.registerJobRunner(jobRunnerKey, new JobRunner() { @Nullable @Override public JobRunnerResponse runJob(JobRunnerRequest jobRunnerRequest) { latch.countDown(); return null; } }); final JobConfig jobConfig = JobConfig.forJobRunnerKey(jobRunnerKey) .withRunMode(RunMode.RUN_LOCALLY) .withSchedule(Schedule.runOnce(null)); try { schedulerService.scheduleJobWithGeneratedId(jobConfig); } catch (SchedulerServiceException sse) { throw new SchedulerRuntimeException(sse.getMessage(), sse); } assertThat(latch.await(30, TimeUnit.SECONDS), is(true)); }
@Override public void runNow(final long serviceId) throws Exception { JiraServiceContainer jiraServiceContainer = getServiceWithId(serviceId); if (jiraServiceContainer == null) { throw new ServiceException("Service with id '" + serviceId + "' was not found"); } JobConfig config = JobConfig.forJobRunnerKey(SERVICE_JOB_KEY) .withSchedule(Schedule.runOnce(null)) .withRunMode(RunMode.RUN_ONCE_PER_CLUSTER) .withParameters(ImmutableMap.<String, Serializable>of(SERVICE_ID_KEY, serviceId)); JobId jobId = schedulerService.scheduleJobWithGeneratedId(config); LOG.debug("JIRA Service '" + jiraServiceContainer.getName() + "' scheduled for immediate execution with job id '" + jobId + '\''); }