Search in sources :

Example 1 with ScheduledJob

use of org.molgenis.jobs.model.ScheduledJob in project molgenis by molgenis.

the class JobSchedulerTest method scheduleInvalidCronExpression.

@Test(expectedExceptions = MolgenisValidationException.class)
public void scheduleInvalidCronExpression() throws SchedulerException {
    ScheduledJob scheduledJob = scheduledJobFactory.create();
    scheduledJob.setId(id);
    scheduledJob.set(ScheduledJobMetadata.CRON_EXPRESSION, "XXX");
    scheduledJob.set(ScheduledJobMetadata.NAME, "name");
    scheduledJob.set(ScheduledJobMetadata.ACTIVE, false);
    scheduledJob.setType(scheduledJobType);
    jobScheduler.schedule(scheduledJob);
}
Also used : ScheduledJob(org.molgenis.jobs.model.ScheduledJob) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 2 with ScheduledJob

use of org.molgenis.jobs.model.ScheduledJob in project molgenis by molgenis.

the class JobScheduler method runNow.

/**
 * Executes a {@link ScheduledJob} immediately.
 *
 * @param scheduledJobId ID of the {@link ScheduledJob} to run
 */
public synchronized void runNow(String scheduledJobId) {
    ScheduledJob scheduledJob = getJob(scheduledJobId);
    try {
        JobKey jobKey = new JobKey(scheduledJobId, SCHEDULED_JOB_GROUP);
        if (quartzScheduler.checkExists(jobKey)) {
            // Run job now
            quartzScheduler.triggerJob(jobKey);
        } else {
            // Schedule with 'now' trigger
            Trigger trigger = newTrigger().withIdentity(scheduledJobId, SCHEDULED_JOB_GROUP).startNow().build();
            schedule(scheduledJob, trigger);
        }
    } catch (SchedulerException e) {
        LOG.error("Error runNow ScheduledJob", e);
        throw new MolgenisDataException("Error job runNow", e);
    }
}
Also used : MolgenisDataException(org.molgenis.data.MolgenisDataException) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) ScheduledJob(org.molgenis.jobs.model.ScheduledJob)

Example 3 with ScheduledJob

use of org.molgenis.jobs.model.ScheduledJob in project molgenis by molgenis.

the class JobExecutor method executeScheduledJob.

/**
 * Executes a {@link ScheduledJob} in the current thread.
 *
 * @param scheduledJobId ID of the {@link ScheduledJob} to run
 */
@RunAsSystem
public void executeScheduledJob(String scheduledJobId) {
    ScheduledJob scheduledJob = dataService.findOneById(SCHEDULED_JOB, scheduledJobId, ScheduledJob.class);
    JobExecution jobExecution = createJobExecution(scheduledJob);
    Job molgenisJob = saveExecutionAndCreateJob(jobExecution);
    try {
        runJob(jobExecution, molgenisJob);
    } catch (Exception ex) {
        LOG.error("Error creating job for JobExecution.", ex);
        jobExecution.setStatus(JobExecution.Status.FAILED);
        jobExecution.setProgressMessage(ex.getMessage());
        dataService.update(jobExecution.getEntityType().getId(), jobExecution);
        throw ex;
    }
}
Also used : JobExecution(org.molgenis.jobs.model.JobExecution) ScheduledJob(org.molgenis.jobs.model.ScheduledJob) ScheduledJob(org.molgenis.jobs.model.ScheduledJob) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 4 with ScheduledJob

use of org.molgenis.jobs.model.ScheduledJob in project molgenis by molgenis.

the class JobSchedulerTest method scheduleExisting.

@Test
public void scheduleExisting() throws SchedulerException {
    ScheduledJob scheduledJob = scheduledJobFactory.create();
    scheduledJob.setId(id);
    scheduledJob.set(ScheduledJobMetadata.CRON_EXPRESSION, "	0/20 * * * * ?");
    scheduledJob.set(ScheduledJobMetadata.NAME, "name");
    scheduledJob.set(ScheduledJobMetadata.ACTIVE, true);
    scheduledJob.setType(scheduledJobType);
    when(quartzScheduler.checkExists(jobKey)).thenReturn(true);
    when(dataService.findOneById(SCHEDULED_JOB, id, ScheduledJob.class)).thenReturn(scheduledJob);
    jobScheduler.schedule(scheduledJob);
    verify(quartzScheduler).deleteJob((jobKey));
    verify(quartzScheduler).scheduleJob(ArgumentMatchers.any(JobDetail.class), ArgumentMatchers.any(Trigger.class));
}
Also used : ScheduledJob(org.molgenis.jobs.model.ScheduledJob) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 5 with ScheduledJob

use of org.molgenis.jobs.model.ScheduledJob in project molgenis by molgenis.

the class JobSchedulerTest method schedule.

@Test
public void schedule() throws SchedulerException {
    ScheduledJob scheduledJob = scheduledJobFactory.create();
    scheduledJob.setId(id);
    scheduledJob.set(ScheduledJobMetadata.CRON_EXPRESSION, "	0/20 * * * * ?");
    scheduledJob.set(ScheduledJobMetadata.NAME, "name");
    scheduledJob.set(ScheduledJobMetadata.ACTIVE, true);
    scheduledJob.setType(scheduledJobType);
    when(quartzScheduler.checkExists(jobKey)).thenReturn(false);
    jobScheduler.schedule(scheduledJob);
    verify(quartzScheduler).scheduleJob(ArgumentMatchers.any(JobDetail.class), ArgumentMatchers.any(Trigger.class));
}
Also used : ScheduledJob(org.molgenis.jobs.model.ScheduledJob) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Aggregations

ScheduledJob (org.molgenis.jobs.model.ScheduledJob)6 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)4 Test (org.testng.annotations.Test)4 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 JobExecution (org.molgenis.jobs.model.JobExecution)1 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)1 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)1