Search in sources :

Example 11 with SchedulerException

use of org.quartz.SchedulerException in project zeppelin by apache.

the class Notebook method refreshCron.

public void refreshCron(String id) {
    removeCron(id);
    synchronized (notes) {
        Note note = notes.get(id);
        if (note == null) {
            return;
        }
        Map<String, Object> config = note.getConfig();
        if (config == null) {
            return;
        }
        String cronExpr = (String) note.getConfig().get("cron");
        if (cronExpr == null || cronExpr.trim().length() == 0) {
            return;
        }
        JobDetail newJob = JobBuilder.newJob(CronJob.class).withIdentity(id, "note").usingJobData("noteId", id).build();
        Map<String, Object> info = note.getInfo();
        info.put("cron", null);
        CronTrigger trigger = null;
        try {
            trigger = TriggerBuilder.newTrigger().withIdentity("trigger_" + id, "note").withSchedule(CronScheduleBuilder.cronSchedule(cronExpr)).forJob(id, "note").build();
        } catch (Exception e) {
            logger.error("Error", e);
            info.put("cron", e.getMessage());
        }
        try {
            if (trigger != null) {
                quartzSched.scheduleJob(newJob, trigger);
            }
        } catch (SchedulerException e) {
            logger.error("Error", e);
            info.put("cron", "Scheduler Exception");
        }
    }
}
Also used : JobDetail(org.quartz.JobDetail) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) AngularObject(org.apache.zeppelin.display.AngularObject) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException)

Example 12 with SchedulerException

use of org.quartz.SchedulerException in project pinot by linkedin.

the class AlertJobScheduler method scheduleJob.

private void scheduleJob(JobContext jobContext, EmailConfigurationDTO alertConfig) {
    LOG.info("Starting {}", jobContext.getJobName());
    String triggerKey = String.format("alert_scheduler_trigger_%d", alertConfig.getId());
    CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(alertConfig.getCron())).build();
    String jobKey = jobContext.getJobName();
    JobDetail job = JobBuilder.newJob(AlertJobRunner.class).withIdentity(jobKey).build();
    job.getJobDataMap().put(AlertJobRunner.ALERT_JOB_CONTEXT, jobContext);
    try {
        quartzScheduler.scheduleJob(job, trigger);
    } catch (SchedulerException e) {
        LOG.error("Exception while scheduling alert job", e);
    }
    LOG.info("Started {}: {}", jobKey, alertConfig);
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException)

Example 13 with SchedulerException

use of org.quartz.SchedulerException in project pinot by linkedin.

the class AlertJobScheduler method run.

public void run() {
    try {
        // read all alert configs
        LOG.info("Reading all alert configs..");
        List<EmailConfigurationDTO> alertConfigs = emailConfigurationDAO.findAll();
        // get active jobs
        List<String> scheduledJobs = getScheduledJobs();
        LOG.info("Scheduled jobs {}", scheduledJobs);
        for (EmailConfigurationDTO alertConfig : alertConfigs) {
            Long id = alertConfig.getId();
            String jobKey = getJobKey(id);
            boolean isActive = alertConfig.isActive();
            boolean isScheduled = scheduledJobs.contains(jobKey);
            // schedule them with quartz, as function is newly created, or newly activated
            if (isActive && !isScheduled) {
                LOG.info("Found active but not scheduled {}", id);
                startJob(alertConfig, jobKey);
            } else // remove them from quartz, as function is newly deactivated
            if (!isActive && isScheduled) {
                LOG.info("Found inactive but scheduled {}", id);
                stopJob(jobKey);
            } else // but check for cron updates
            if (isActive && isScheduled) {
                String cronInDatabase = alertConfig.getCron();
                List<Trigger> triggers = (List<Trigger>) quartzScheduler.getTriggersOfJob(JobKey.jobKey(jobKey));
                CronTrigger cronTrigger = (CronTrigger) triggers.get(0);
                String cronInSchedule = cronTrigger.getCronExpression();
                // cron expression has been updated, restart this job
                if (!cronInDatabase.equals(cronInSchedule)) {
                    LOG.info("Cron expression for config {} with jobKey {} has been changed from {}  to {}. " + "Restarting schedule", id, jobKey, cronInSchedule, cronInDatabase);
                    stopJob(jobKey);
                    startJob(alertConfig, jobKey);
                }
            }
        // for all jobs with not isActive, and not isScheduled, no change required
        }
        // stop the schedule, as function has been deleted
        for (String scheduledJobKey : scheduledJobs) {
            Long configId = getIdFromJobKey(scheduledJobKey);
            EmailConfigurationDTO alertConfigSpec = emailConfigurationDAO.findById(configId);
            if (alertConfigSpec == null) {
                LOG.info("Found scheduled, but not in database {}", configId);
                stopJob(scheduledJobKey);
            }
        }
    } catch (SchedulerException e) {
        LOG.error("Exception in reading active jobs", e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList) List(java.util.List) EmailConfigurationDTO(com.linkedin.thirdeye.datalayer.dto.EmailConfigurationDTO)

Example 14 with SchedulerException

use of org.quartz.SchedulerException in project pinot by linkedin.

the class AlertJobSchedulerV2 method scheduleJob.

private void scheduleJob(JobContext jobContext, AlertConfigDTO alertConfig) {
    LOG.info("Starting {}", jobContext.getJobName());
    String triggerKey = String.format("alert_scheduler_trigger_%d", alertConfig.getId());
    CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(CronScheduleBuilder.cronSchedule(alertConfig.getCronExpression())).build();
    String jobKey = jobContext.getJobName();
    JobDetail job = JobBuilder.newJob(AlertJobRunnerV2.class).withIdentity(jobKey).build();
    job.getJobDataMap().put(AlertJobRunnerV2.ALERT_JOB_CONTEXT_V2, jobContext);
    try {
        quartzScheduler.scheduleJob(job, trigger);
    } catch (SchedulerException e) {
        LOG.error("Exception while scheduling alert job", e);
    }
    LOG.info("Started {}: {}", jobKey, alertConfig);
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException)

Example 15 with SchedulerException

use of org.quartz.SchedulerException in project pinot by linkedin.

the class AlertJobSchedulerV2 method run.

public void run() {
    try {
        // read all alert configs
        LOG.info("Reading all alert configs..");
        List<AlertConfigDTO> alertConfigs = alertConfigDAO.findAll();
        // get active jobs
        List<String> scheduledJobs = getScheduledJobs();
        LOG.info("Scheduled jobs {}", scheduledJobs);
        for (AlertConfigDTO alertConfig : alertConfigs) {
            Long id = alertConfig.getId();
            String jobKey = getJobKey(id);
            boolean isActive = alertConfig.isActive();
            boolean isScheduled = scheduledJobs.contains(jobKey);
            if (isActive) {
                if (isScheduled) {
                    String cronInDatabase = alertConfig.getCronExpression();
                    List<Trigger> triggers = (List<Trigger>) quartzScheduler.getTriggersOfJob(JobKey.jobKey(jobKey));
                    CronTrigger cronTrigger = (CronTrigger) triggers.get(0);
                    String cronInSchedule = cronTrigger.getCronExpression();
                    // cron expression has been updated, restart this job
                    if (!cronInDatabase.equals(cronInSchedule)) {
                        LOG.info("Cron expression for config {} with jobKey {} has been changed from {}  to {}. " + "Restarting schedule", id, jobKey, cronInSchedule, cronInDatabase);
                        stopJob(jobKey);
                        startJob(alertConfig, jobKey);
                    }
                } else {
                    LOG.info("Found active but not scheduled {}", id);
                    startJob(alertConfig, jobKey);
                }
            } else {
                if (isScheduled) {
                    LOG.info("Found inactive but scheduled {}", id);
                    stopJob(jobKey);
                }
            // for all jobs with not isActive, and not isScheduled, no change required
            }
        }
        // stop the schedule, as function has been deleted
        for (String scheduledJobKey : scheduledJobs) {
            Long configId = getIdFromJobKey(scheduledJobKey);
            AlertConfigDTO alertConfigSpec = alertConfigDAO.findById(configId);
            if (alertConfigSpec == null) {
                LOG.info("Found scheduled, but not in database {}", configId);
                stopJob(scheduledJobKey);
            }
        }
    } catch (SchedulerException e) {
        LOG.error("Exception in reading active jobs", e);
    }
}
Also used : CronTrigger(org.quartz.CronTrigger) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList) List(java.util.List) AlertConfigDTO(com.linkedin.thirdeye.datalayer.dto.AlertConfigDTO)

Aggregations

SchedulerException (org.quartz.SchedulerException)133 JobDetail (org.quartz.JobDetail)59 Trigger (org.quartz.Trigger)42 Scheduler (org.quartz.Scheduler)37 JobKey (org.quartz.JobKey)33 SimpleTrigger (org.quartz.SimpleTrigger)19 JobDataMap (org.quartz.JobDataMap)18 CronTrigger (org.quartz.CronTrigger)17 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 ApplicationContext (org.springframework.context.ApplicationContext)15 ArrayList (java.util.ArrayList)12 SchedulerContext (org.quartz.SchedulerContext)12 IOException (java.io.IOException)11 TriggerKey (org.quartz.TriggerKey)10 Date (java.util.Date)9 JobExecutionException (org.quartz.JobExecutionException)9 StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)6 ParseException (java.text.ParseException)5 Command (org.openhab.core.types.Command)5 JobSystemException (com.dangdang.ddframe.job.exception.JobSystemException)4