Search in sources :

Example 1 with DependentTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger 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 DependentTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskExecution.

@Test
@SuppressWarnings("unchecked")
public void testDependentTaskExecution() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        String resultValue = "dependent-task-initiator";
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createTask("dependent-task-initiator");
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId());
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        // execute first task
        LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
        Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
        // 
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
        // 
        longRunningTaskManager.cancel(createdLrts.get(0).getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 3 with DependentTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskInDryModeExecution.

@Test
@SuppressWarnings("unchecked")
public void testDependentTaskInDryModeExecution() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        String resultValue = "dependent-task-initiator-dry-run";
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableDryRunTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createDryRunTask(resultValue);
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId(), true);
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        Assert.assertTrue(createdLrts.get(0).isDryRun());
        UUID firstTaskId = createdLrts.get(0).getId();
        // execute first task
        LongRunningFutureTask<String> futureTask = (LongRunningFutureTask<String>) longRunningTaskManager.processCreated(createdLrts.get(0).getId());
        Assert.assertEquals(resultValue, futureTask.getFutureTask().get());
        // 
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, longRunningTaskService.find(filter, null).getTotalElements());
        Assert.assertTrue(createdLrts.get(0).isDryRun());
        Assert.assertNotEquals(firstTaskId, createdLrts.get(0).getId());
        // 
        longRunningTaskManager.cancel(createdLrts.get(0).getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) UUID(java.util.UUID) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 4 with DependentTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger 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 5 with DependentTaskTrigger

use of eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskNoExecutionAfterInitiatorFails.

@Test
public void testDependentTaskNoExecutionAfterInitiatorFails() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        LongRunningTaskExecuteDependentProcessor processor = context.getBean(LongRunningTaskExecuteDependentProcessor.class);
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createTask("dependent-task-initiator");
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId());
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        IdmLongRunningTaskDto lrt = createdLrts.get(0);
        lrt.setResult(new OperationResult(OperationState.EXCEPTION));
        // not executed
        EventResult<IdmLongRunningTaskDto> result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.BLOCKED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.NOT_EXECUTED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CANCELED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CREATED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.RUNNING));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        // 
        longRunningTaskManager.cancel(lrt.getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) LongRunningTaskExecuteDependentProcessor(eu.bcvsolutions.idm.core.scheduler.event.processor.LongRunningTaskExecuteDependentProcessor) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

DependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger)5 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)5 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)3 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)3 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)3 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)3 FutureTask (java.util.concurrent.FutureTask)3 DateTime (org.joda.time.DateTime)3 Test (org.junit.Test)3 CronTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger)2 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)2 IdmDependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger)2 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)1 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)1 AbstractTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger)1 TaskTriggerState (eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState)1 LongRunningTaskEvent (eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent)1 DryRunNotSupportedException (eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)1 SchedulableTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)1 LongRunningTaskExecuteDependentProcessor (eu.bcvsolutions.idm.core.scheduler.event.processor.LongRunningTaskExecuteDependentProcessor)1