Search in sources :

Example 1 with SchedulableTaskExecutor

use of eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor in project CzechIdMng by bcvsolutions.

the class ContractSynchronizationExecutor method executeHrProcess.

/**
 * Start HR process. Find quartz task and LRT. If some LRT for this task type
 * exists, then is used. If not exists, then is created new. Task is execute
 * synchronously.
 *
 * @param log
 * @param executor
 * @return
 */
private SysSyncLogDto executeHrProcess(SysSyncLogDto log, SchedulableTaskExecutor<?> executor) {
    @SuppressWarnings("unchecked") Class<? extends SchedulableTaskExecutor<?>> taskType = (Class<? extends SchedulableTaskExecutor<?>>) executor.getClass();
    IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
    filter.setOperationState(OperationState.CREATED);
    filter.setTaskType(taskType.getCanonicalName());
    List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
    IdmLongRunningTaskDto lrt = null;
    String simpleName = taskType.getSimpleName();
    if (createdLrts.isEmpty()) {
        // We do not have LRT for this task, we will create him
        Task processTask = findTask(taskType);
        if (processTask == null) {
            addToItemLog(log, MessageFormat.format("Warning - HR process [{0}] cannot be executed, because task for this type was not found!", simpleName));
            log = synchronizationLogService.save(log);
            return log;
        }
        IdmScheduledTaskDto scheduledTask = scheduledTaskService.findByQuartzTaskName(processTask.getId());
        if (scheduledTask == null) {
            addToItemLog(log, MessageFormat.format("Warning - HR process [{0}] cannot be executed, because scheduled task for this type was not found!", simpleName));
            log = synchronizationLogService.save(log);
            return log;
        }
        lrt = longRunningTaskService.create(scheduledTask, executor, configurationService.getInstanceId());
    } else {
        lrt = createdLrts.get(0);
    }
    if (lrt != null) {
        log.addToLog(MessageFormat.format("After success sync have to be run HR task [{1}]. We start him (synchronously) now [{0}]. LRT ID: [{2}]", LocalDateTime.now(), simpleName, lrt.getId()));
        log = synchronizationLogService.save(log);
        executor.setLongRunningTaskId(lrt.getId());
        longRunningTaskManager.executeSync(executor);
        log.addToLog(MessageFormat.format("HR task [{1}] ended in [{0}].", LocalDateTime.now(), simpleName));
        log = synchronizationLogService.save(log);
    }
    return log;
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) SchedulableTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)

Example 2 with SchedulableTaskExecutor

use of eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor in project CzechIdMng by bcvsolutions.

the class AbstractScheduledTaskInitializer method typeToTask.

/**
 * Transform {@link IdmScheduledTaskType} to {@link Task}.
 *
 * @param type
 * @return
 * @throws ClassNotFoundException
 */
private Task typeToTask(IdmScheduledTaskType type) throws ClassNotFoundException {
    Task task = new Task();
    task.setDescription(type.getDescription());
    // TODO: module attribute not working in Task
    task.setModule(getModule());
    task.setInstanceId(type.getInstanceId());
    @SuppressWarnings("unchecked") Class<? extends SchedulableTaskExecutor<?>> classType = (Class<? extends SchedulableTaskExecutor<?>>) Class.forName(type.getTaskType());
    task.setTaskType(classType);
    // parameters
    if (type.getParameters() != null && type.getParameters().getParameters() != null) {
        Map<String, String> parameters = new HashMap<>();
        for (IdmScheduledTaskParameterType param : type.getParameters().getParameters()) {
            parameters.put(param.getKey(), param.getValue());
        }
        if (!parameters.isEmpty()) {
            task.setParameters(parameters);
        }
    }
    return task;
}
Also used : IdmScheduledTaskParameterType(eu.bcvsolutions.idm.core.scheduler.jaxb.IdmScheduledTaskParameterType) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) HashMap(java.util.HashMap) SchedulableTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)

Example 3 with SchedulableTaskExecutor

use of eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method getTask.

/**
 * Returns task by given key
 *
 * @param jobKey
 * @return
 */
@SuppressWarnings("unchecked")
private Task getTask(JobKey jobKey) {
    try {
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        if (jobDetail == null) {
            // job does not exists
            return null;
        }
        Task task = new Task();
        // task setting
        task.setId(jobKey.getName());
        // AutowireHelper is not needed here
        SchedulableTaskExecutor<?> taskExecutor = (SchedulableTaskExecutor<?>) jobDetail.getJobClass().newInstance();
        task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) taskExecutor.getClass());
        task.setSupportsDryRun(taskExecutor.supportsDryRun());
        task.setDescription(jobDetail.getDescription());
        task.setInstanceId(jobDetail.getJobDataMap().getString(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID));
        task.setTriggers(new ArrayList<>());
        // task properties
        for (Entry<String, Object> entry : jobDetail.getJobDataMap().entrySet()) {
            task.getParameters().put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
        }
        // scheduled triggers - native
        for (Trigger trigger : scheduler.getTriggersOfJob(jobKey)) {
            TriggerState state = scheduler.getTriggerState(trigger.getKey());
            if (trigger instanceof CronTrigger) {
                task.getTriggers().add(new CronTaskTrigger(task.getId(), (CronTrigger) trigger, TaskTriggerState.convert(state)));
            } else if (trigger instanceof SimpleTrigger) {
                task.getTriggers().add(new SimpleTaskTrigger(task.getId(), (SimpleTrigger) trigger, TaskTriggerState.convert(state)));
            } else {
                LOG.warn("Job '{}' ({}) has registered trigger of unsupported type {}", jobKey, jobDetail.getJobClass(), trigger);
            }
        }
        // dependent tasks
        dependentTaskTriggerRepository.findByDependentTaskId(jobKey.getName()).forEach(dependentTask -> {
            task.getTriggers().add(new DependentTaskTrigger(task.getId(), dependentTask.getId(), dependentTask.getInitiatorTaskId()));
        });
        return task;
    } catch (org.quartz.SchedulerException ex) {
        if (ex.getCause() instanceof ClassNotFoundException) {
            deleteTask(jobKey.getName());
            LOG.warn("Job [{}] inicialization failed, job class was removed, scheduled task is removed.", jobKey, ex);
            return null;
        }
        throw new CoreException(ex);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException ex) {
        deleteTask(jobKey.getName());
        LOG.warn("Job [{}] inicialization failed, scheduled task is removed", jobKey, ex);
        return null;
    }
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) CronTrigger(org.quartz.CronTrigger) TriggerState(org.quartz.Trigger.TriggerState) TaskTriggerState(eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState) JobDetail(org.quartz.JobDetail) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmDependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger) SimpleTrigger(org.quartz.SimpleTrigger) SchedulableTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) SimpleTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)

Example 4 with SchedulableTaskExecutor

use of eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method getSupportedTasks.

@Override
@SuppressWarnings({ "unchecked" })
public List<Task> getSupportedTasks() {
    return context.getBeansOfType(SchedulableTaskExecutor.class).entrySet().stream().map(entry -> {
        SchedulableTaskExecutor<?> taskExecutor = entry.getValue();
        Task task = new Task();
        task.setId(entry.getKey());
        task.setModule(taskExecutor.getModule());
        task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) taskExecutor.getClass());
        task.setDescription(AutowireHelper.getBeanDescription(entry.getKey()));
        for (String parameterName : taskExecutor.getPropertyNames()) {
            task.getParameters().put(parameterName, null);
        }
        return task;
    }).sorted(Comparator.comparing(task -> task.getTaskType().getSimpleName(), Comparator.naturalOrder())).collect(Collectors.toList());
}
Also used : Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) SchedulableTaskExecutor(eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)

Aggregations

Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)4 SchedulableTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)4 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)1 AbstractTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger)1 CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)1 DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)1 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)1 IdmScheduledTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto)1 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)1 TaskTriggerState (eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState)1 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)1 IdmDependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger)1 IdmScheduledTaskParameterType (eu.bcvsolutions.idm.core.scheduler.jaxb.IdmScheduledTaskParameterType)1 HashMap (java.util.HashMap)1 CronTrigger (org.quartz.CronTrigger)1 JobDetail (org.quartz.JobDetail)1 SimpleTrigger (org.quartz.SimpleTrigger)1 Trigger (org.quartz.Trigger)1 TriggerState (org.quartz.Trigger.TriggerState)1