Search in sources :

Example 1 with TriggerState

use of org.quartz.Trigger.TriggerState in project jaffa-framework by jaffa-projects.

the class QuartzSchedulerHelper method jobAndTriggerToTask.

/**
 * Molds the input Job and Trigger into a Task.
 */
private ScheduledTask jobAndTriggerToTask(final JobDetail jobDetail, final List<? extends Trigger> triggers) throws FrameworkException {
    // Obtain the Task from the JobDataMap of the Job
    ScheduledTask task = jobDataMapToTask(jobDetail.getJobDataMap());
    // Obtain the Trigger Details
    if (triggers != null && 0 < triggers.size()) {
        final Trigger trigger = triggers.get(0);
        task.setNextDue(trigger.getNextFireTime() != null ? new DateTime(trigger.getNextFireTime()) : null);
        task.setLastRunOn(trigger.getPreviousFireTime() != null ? new DateTime(trigger.getPreviousFireTime()) : null);
    }
    // Determine the Status
    ScheduledTask.TaskStatusEnumeration status;
    try {
        final TriggerState triggerState = scheduler.getTriggerState(TriggerKey.triggerKey(task.getScheduledTaskId(), JOB_GROUP));
        switch(triggerState) {
            case BLOCKED:
                status = ScheduledTask.TaskStatusEnumeration.BLOCKED;
                break;
            case COMPLETE:
                status = ScheduledTask.TaskStatusEnumeration.COMPLETE;
                break;
            case ERROR:
                status = ScheduledTask.TaskStatusEnumeration.ERROR;
                break;
            case NORMAL:
                status = ScheduledTask.TaskStatusEnumeration.NORMAL;
                break;
            case PAUSED:
                status = ScheduledTask.TaskStatusEnumeration.PAUSED;
                break;
            default:
                status = ScheduledTask.TaskStatusEnumeration.UNKNOWN;
                break;
        }
    } catch (SchedulerException e) {
        LOGGER.error("Error in determing the trigger state. Will assume UNKNOWN status for " + task.getScheduledTaskId(), e);
        status = ScheduledTask.TaskStatusEnumeration.UNKNOWN;
    }
    task.setStatus(status);
    return task;
}
Also used : TriggerState(org.quartz.Trigger.TriggerState) DateTime(org.jaffa.datatypes.DateTime)

Example 2 with TriggerState

use of org.quartz.Trigger.TriggerState in project xxl-job by xuxueli.

the class XxlJobDynamicScheduler method fillJobInfo.

// ---------------------- schedule util ----------------------
/**
 * fill job info
 *
 * @param jobInfo
 */
public static void fillJobInfo(XxlJobInfo jobInfo) {
    // TriggerKey : name + group
    String group = String.valueOf(jobInfo.getJobGroup());
    String name = String.valueOf(jobInfo.getId());
    TriggerKey triggerKey = TriggerKey.triggerKey(name, group);
    try {
        Trigger trigger = scheduler.getTrigger(triggerKey);
        TriggerState triggerState = scheduler.getTriggerState(triggerKey);
        // parse params
        if (trigger != null && trigger instanceof CronTriggerImpl) {
            String cronExpression = ((CronTriggerImpl) trigger).getCronExpression();
            jobInfo.setJobCron(cronExpression);
        }
        if (triggerState != null) {
            jobInfo.setJobStatus(triggerState.name());
        }
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : TriggerState(org.quartz.Trigger.TriggerState) CronTriggerImpl(org.quartz.impl.triggers.CronTriggerImpl)

Example 3 with TriggerState

use of org.quartz.Trigger.TriggerState in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method getTask.

/**
 * Returns task by given key
 *
 * @param jobKey
 * @return task dto
 */
@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());
        // app context is needed here
        SchedulableTaskExecutor<?> taskExecutor = (SchedulableTaskExecutor<?>) context.getAutowireCapableBeanFactory().createBean(jobDetail.getJobClass());
        task.setTaskType((Class<? extends SchedulableTaskExecutor<?>>) AutowireHelper.getTargetClass(taskExecutor));
        task.setDescription(jobDetail.getDescription());
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        task.setInstanceId(jobDataMap.getString(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID));
        task.setModified((ZonedDateTime) jobDataMap.get(SchedulableTaskExecutor.PARAMETER_MODIFIED));
        task.setTriggers(new ArrayList<>());
        // task properties
        for (Entry<String, Object> entry : jobDataMap.entrySet()) {
            task.getParameters().put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().toString());
        }
        task.setDisabled(taskExecutor.isDisabled());
        if (!task.isDisabled()) {
            task.setSupportsDryRun(taskExecutor.supportsDryRun());
            task.setFormDefinition(taskExecutor.getFormDefinition());
            task.setRecoverable(taskExecutor.isRecoverable());
        } else {
            LOG.warn("Task [{}] is disabled and cannot be executed, remove schedule for this task to hide this warning.", task.getTaskType().getSimpleName());
        }
        // 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 (BeansException | 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) JobDataMap(org.quartz.JobDataMap) CronTrigger(org.quartz.CronTrigger) TaskTriggerState(eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState) TriggerState(org.quartz.Trigger.TriggerState) JobDetail(org.quartz.JobDetail) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) CronTrigger(org.quartz.CronTrigger) AbstractTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.AbstractTaskTrigger) CronTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.CronTaskTrigger) 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) 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) BeansException(org.springframework.beans.BeansException)

Aggregations

TriggerState (org.quartz.Trigger.TriggerState)3 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 SimpleTaskTrigger (eu.bcvsolutions.idm.core.scheduler.api.dto.SimpleTaskTrigger)1 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)1 TaskTriggerState (eu.bcvsolutions.idm.core.scheduler.api.dto.TaskTriggerState)1 SchedulableTaskExecutor (eu.bcvsolutions.idm.core.scheduler.api.service.SchedulableTaskExecutor)1 IdmDependentTaskTrigger (eu.bcvsolutions.idm.core.scheduler.entity.IdmDependentTaskTrigger)1 DateTime (org.jaffa.datatypes.DateTime)1 CronTrigger (org.quartz.CronTrigger)1 JobDataMap (org.quartz.JobDataMap)1 JobDetail (org.quartz.JobDetail)1 SimpleTrigger (org.quartz.SimpleTrigger)1 Trigger (org.quartz.Trigger)1 CronTriggerImpl (org.quartz.impl.triggers.CronTriggerImpl)1 BeansException (org.springframework.beans.BeansException)1