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;
}
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;
}
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;
}
}
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());
}
Aggregations