package de.jeha.kame.crawler.scheduler.services;

import de.jeha.kame.crawler.scheduler.model.CrawlJob;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;

/**
 * @author jenshadlich@googlemail.com
 */
public class DefaultSchedulerService implements SchedulerService, SchedulerLifecycle {

    private final Scheduler scheduler;

    public DefaultSchedulerService(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    @Override
    public void addJob(CrawlJob crawlJob) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob()
                .ofType(QuartzCrawlJob.class)
                .withIdentity(crawlJob.getId())
                .usingJobData(QuartzCrawlJob.NAME, crawlJob.getName())
                .usingJobData(QuartzCrawlJob.SEED_URL, crawlJob.getSeedUrl())
                .storeDurably(false)
                .requestRecovery(false)
                .build();

        try {
            scheduler.addJob(jobDetail, false);
        } catch (org.quartz.SchedulerException e) {
            throw new SchedulerException(e);
        }
    }

    @Override
    public void start() throws SchedulerException {
        try {
            scheduler.start();
        } catch (org.quartz.SchedulerException e) {
            throw new SchedulerException(e);
        }
    }

    @Override
    public void suspend() throws SchedulerException {
        try {
            scheduler.standby();
        } catch (org.quartz.SchedulerException e) {
            throw new SchedulerException(e);
        }
    }

}

@@ -156,16 +156,42 @@
 Detail;%0A
+import org.quartz.JobKey;%0A
 import o
@@ -211,16 +211,16 @@
 eduler;%0A
-
 %0A/**%0A * 
@@ -563,32 +563,86 @@
 ulerException %7B%0A
+        JobKey jobKey = new JobKey(crawlJob.getId());%0A
         JobDetai
@@ -751,32 +751,22 @@
 dentity(
-crawlJob.getId()
+jobKey
 )%0A      
@@ -1018,32 +1018,32 @@
 %0A%0A        try %7B%0A
-
             sche
@@ -1071,16 +1071,64 @@
 l, false
+, true);%0A            scheduler.triggerJob(jobKey
 );%0A     

add key and trigger job directly after it was added