Search in sources :

Example 1 with SchedulerException

use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method createTrigger.

@Override
public AbstractTaskTrigger createTrigger(String taskId, AbstractTaskTrigger trigger, boolean dryRun) {
    Assert.notNull(taskId);
    Assert.notNull(trigger);
    // 
    // task has to support dry run mode
    Task task = getTask(taskId);
    Assert.notNull(task);
    if (dryRun && !task.isSupportsDryRun()) {
        throw new DryRunNotSupportedException(task.getTaskType().getCanonicalName());
    }
    // 
    String triggerId = Key.createUniqueName(taskId);
    trigger.setId(triggerId);
    trigger.setTaskId(taskId);
    // TODO use of visitor pattern may be good
    if (trigger instanceof CronTaskTrigger) {
        createTriggerInternal(taskId, (CronTaskTrigger) trigger, dryRun);
    } else if (trigger instanceof SimpleTaskTrigger) {
        createTriggerInternal(taskId, (SimpleTaskTrigger) trigger, dryRun);
    } else if (trigger instanceof DependentTaskTrigger) {
        createTriggerInternal(taskId, (DependentTaskTrigger) trigger);
    } else {
        throw new SchedulerException(CoreResultCode.SCHEDULER_UNSUPPORTED_TASK_TRIGGER, ImmutableMap.of("trigger", trigger.getClass()));
    }
    return trigger;
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) DryRunNotSupportedException(eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)

Example 2 with SchedulerException

use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method createTask.

@Override
public Task createTask(Task task) {
    Assert.notNull(task);
    Assert.notNull(task.getInstanceId());
    Assert.notNull(task.getTaskType());
    // 
    try {
        // task id
        String taskId = Key.createUniqueName(DEFAULT_GROUP_NAME);
        // description
        String description = task.getDescription();
        if (StringUtils.isEmpty(description)) {
            description = AutowireHelper.getBeanDescription(task.getTaskType());
        }
        // job properties
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID, task.getInstanceId());
        task.getParameters().entrySet().forEach(entry -> {
            jobDataMap.put(entry.getKey(), entry.getValue());
        });
        // validate init method
        try {
            LongRunningTaskExecutor<?> taskExecutor = AutowireHelper.createBean(task.getTaskType());
            taskExecutor.init(jobDataMap);
        } catch (ResultCodeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", taskId, "taskType", task.getTaskType().getSimpleName(), "instanceId", task.getInstanceId()), ex);
        }
        // create job detail - job definition
        JobDetail jobDetail = JobBuilder.newJob().withIdentity(taskId, DEFAULT_GROUP_NAME).withDescription(description).ofType(task.getTaskType()).usingJobData(jobDataMap).storeDurably().build();
        // add job
        scheduler.addJob(jobDetail, false);
        // 
        LOG.debug("Job '{}' ({}) was created and registered", taskId, task.getTaskType());
        // 
        return getTask(taskId);
    } catch (org.quartz.SchedulerException ex) {
        throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TASK_FAILED, ex);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) InvalidCronExpressionException(eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) DryRunNotSupportedException(eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)

Example 3 with SchedulerException

use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.

the class DefaultIdmScheduledTaskService method findByQuartzTaskName.

@Transactional(readOnly = true)
@Override
public IdmScheduledTaskDto findByQuartzTaskName(String taskName) {
    IdmScheduledTaskFilter filter = new IdmScheduledTaskFilter();
    filter.setQuartzTaskName(taskName);
    Page<IdmScheduledTaskDto> results = find(filter, new PageRequest(0, 1));
    if (results.getTotalElements() == 0) {
        return null;
    } else if (results.getTotalElements() == 1) {
        return results.iterator().next();
    }
    throw new SchedulerException(CoreResultCode.SEARCH_ERROR, ImmutableMap.of("reason", "Multiple tasks found for " + getClass().getSimpleName() + " task."));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) IdmScheduledTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmScheduledTaskFilter) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with SchedulerException

use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method createTriggerInternal.

private void createTriggerInternal(String taskId, CronTaskTrigger trigger, boolean dryRun) {
    CronTaskTrigger cronTaskTrigger = (CronTaskTrigger) trigger;
    CronScheduleBuilder cronBuilder;
    try {
        cronBuilder = CronScheduleBuilder.cronSchedule(cronTaskTrigger.getCron()).withMisfireHandlingInstructionDoNothing().inTimeZone(TimeZone.getDefault());
    } catch (RuntimeException ex) {
        throw new InvalidCronExpressionException(cronTaskTrigger.getCron(), ex);
    }
    // 
    try {
        scheduler.scheduleJob(TriggerBuilder.newTrigger().withIdentity(trigger.getId(), taskId).forJob(getKey(taskId)).withDescription(cronTaskTrigger.getDescription()).withSchedule(cronBuilder).usingJobData(SchedulableTaskExecutor.PARAMETER_DRY_RUN, dryRun).startNow().build());
    } catch (org.quartz.SchedulerException ex) {
        throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TRIGGER_FAILED, ex);
    }
}
Also used : CronScheduleBuilder(org.quartz.CronScheduleBuilder) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) InvalidCronExpressionException(eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)

Example 5 with SchedulerException

use of eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method updateTask.

@Override
public Task updateTask(String taskId, Task newTask) {
    Assert.notNull(taskId);
    Task task = getTask(taskId);
    String description = newTask.getDescription();
    if (StringUtils.isEmpty(description)) {
        description = AutowireHelper.getBeanDescription(task.getTaskType());
    }
    try {
        // job properties
        JobDataMap jobDataMap = new JobDataMap();
        newTask.getParameters().entrySet().forEach(entry -> {
            jobDataMap.put(entry.getKey(), entry.getValue());
        });
        jobDataMap.put(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID, newTask.getInstanceId());
        // validate init method
        try {
            LongRunningTaskExecutor<?> taskExecutor = AutowireHelper.createBean(task.getTaskType());
            taskExecutor.init(jobDataMap);
        } catch (ResultCodeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", taskId, "taskType", task.getTaskType().getSimpleName(), "instanceId", task.getInstanceId()), ex);
        }
        // create job detail - job definition
        JobDetail jobDetail = JobBuilder.newJob().withIdentity(task.getId(), DEFAULT_GROUP_NAME).withDescription(description).ofType(task.getTaskType()).usingJobData(jobDataMap).storeDurably().build();
        // add job
        scheduler.addJob(jobDetail, true);
        // 
        LOG.debug("Job '{}' ({}) was updated and registered", task.getId(), task.getTaskType());
    // 
    } catch (org.quartz.SchedulerException ex) {
        throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TASK_FAILED, ex);
    }
    return getTask(task.getId());
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) InvalidCronExpressionException(eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) DryRunNotSupportedException(eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)

Aggregations

SchedulerException (eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException)5 DryRunNotSupportedException (eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)3 InvalidCronExpressionException (eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException)3 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)2 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)2 JobDataMap (org.quartz.JobDataMap)2 JobDetail (org.quartz.JobDetail)2 DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)1 IdmScheduledTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto)1 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)1 IdmScheduledTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmScheduledTaskFilter)1 IdmDependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger)1 CronScheduleBuilder (org.quartz.CronScheduleBuilder)1 PageRequest (org.springframework.data.domain.PageRequest)1 Transactional (org.springframework.transaction.annotation.Transactional)1