Search in sources :

Example 1 with JobContext

use of org.craftercms.engine.util.quartz.JobContext in project engine by craftercms.

the class SchedulingUtils method createJobContext.

public static JobContext createJobContext(SiteContext siteContext, String scriptUrl, String cronExpression, ServletContext servletContext) {
    String jobName = siteContext.getSiteName() + ":" + scriptUrl;
    JobDetail detail = SchedulingUtils.createScriptJob(siteContext, jobName, scriptUrl, servletContext);
    Trigger trigger = SchedulingUtils.createCronTrigger("trigger for " + jobName, cronExpression);
    String description = "Job{url='" + scriptUrl + "', cron='" + cronExpression + "'}";
    return new JobContext(detail, trigger, description);
}
Also used : JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) JobContext(org.craftercms.engine.util.quartz.JobContext)

Example 2 with JobContext

use of org.craftercms.engine.util.quartz.JobContext in project engine by craftercms.

the class ConfigurationScriptJobResolver method resolveJobs.

@Override
@SuppressWarnings("unchecked")
public List<JobContext> resolveJobs(SiteContext siteContext) throws SchedulingException {
    HierarchicalConfiguration config = siteContext.getConfig();
    List<JobContext> jobContexts = new ArrayList<>();
    if (config != null) {
        List<HierarchicalConfiguration> jobFoldersConfig = config.configurationsAt(JOB_FOLDER_KEY);
        if (CollectionUtils.isNotEmpty(jobFoldersConfig)) {
            for (HierarchicalConfiguration jobFolderConfig : jobFoldersConfig) {
                List<JobContext> folderJobContexts = getJobsUnderFolder(siteContext, jobFolderConfig);
                if (CollectionUtils.isNotEmpty(folderJobContexts)) {
                    jobContexts.addAll(folderJobContexts);
                }
            }
        }
        List<HierarchicalConfiguration> jobsConfig = config.configurationsAt(JOB_KEY);
        if (CollectionUtils.isNotEmpty(jobsConfig)) {
            for (HierarchicalConfiguration jobConfig : jobsConfig) {
                JobContext jobContext = getJob(siteContext, jobConfig);
                if (jobContext != null) {
                    jobContexts.add(jobContext);
                }
            }
        }
    }
    return jobContexts;
}
Also used : ArrayList(java.util.ArrayList) JobContext(org.craftercms.engine.util.quartz.JobContext) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration)

Example 3 with JobContext

use of org.craftercms.engine.util.quartz.JobContext in project engine by craftercms.

the class ConfigurationScriptJobResolver method getJobsUnderFolder.

protected List<JobContext> getJobsUnderFolder(SiteContext siteContext, HierarchicalConfiguration jobFolderConfig) {
    List<JobContext> jobContexts = null;
    String folderPath = jobFolderConfig.getString(PATH_KEY);
    String cronExpression = jobFolderConfig.getString(CRON_EXPRESSION_KEY);
    ContentStoreService storeService = siteContext.getStoreService();
    Context context = siteContext.getContext();
    if (StringUtils.isNotEmpty(folderPath) && StringUtils.isNotEmpty(cronExpression)) {
        List<String> scriptPaths = ContentStoreUtils.findChildrenUrl(storeService, context, folderPath);
        if (CollectionUtils.isNotEmpty(scriptPaths)) {
            for (String scriptPath : scriptPaths) {
                if (scriptPath.endsWith(scriptSuffix)) {
                    if (jobContexts == null) {
                        jobContexts = new ArrayList<>();
                    }
                    jobContexts.add(SchedulingUtils.createJobContext(siteContext, scriptPath, cronExpression, disableVariableRestrictions ? servletContext : null));
                }
            }
        }
    }
    return jobContexts;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ServletContext(javax.servlet.ServletContext) ContentStoreService(org.craftercms.core.service.ContentStoreService) JobContext(org.craftercms.engine.util.quartz.JobContext)

Example 4 with JobContext

use of org.craftercms.engine.util.quartz.JobContext in project engine by craftercms.

the class ConfigurationScriptJobResolverTest method testResolveJobs.

@Test
public void testResolveJobs() throws Exception {
    List<JobContext> jobContexts = resolver.resolveJobs(siteContext);
    assertNotNull(jobContexts);
    assertEquals(2, jobContexts.size());
    JobDetailImpl jobDetail = (JobDetailImpl) jobContexts.get(0).getDetail();
    CronTrigger trigger = (CronTrigger) jobContexts.get(0).getTrigger();
    assertEquals(ScriptJob.class, jobDetail.getJobClass());
    assertEquals("/scripts/jobs/morejobs/testJob2.groovy", jobDetail.getJobDataMap().getString(ScriptJob.SCRIPT_URL_DATA_KEY));
    assertEquals("0 0/15 * * * ?", trigger.getCronExpression());
    jobDetail = (JobDetailImpl) jobContexts.get(1).getDetail();
    trigger = (CronTrigger) jobContexts.get(1).getTrigger();
    assertEquals(ScriptJob.class, jobDetail.getJobClass());
    assertEquals("/scripts/jobs/testJob.groovy", jobDetail.getJobDataMap().getString(ScriptJob.SCRIPT_URL_DATA_KEY));
    assertEquals("0 0/15 * * * ?", trigger.getCronExpression());
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetailImpl(org.quartz.impl.JobDetailImpl) JobContext(org.craftercms.engine.util.quartz.JobContext) Test(org.junit.Test)

Example 5 with JobContext

use of org.craftercms.engine.util.quartz.JobContext in project engine by craftercms.

the class FolderBasedScriptJobResolverTest method testResolveJobs.

@Test
public void testResolveJobs() throws Exception {
    List<JobContext> jobContexts = resolver.resolveJobs(siteContext);
    assertNotNull(jobContexts);
    assertEquals(1, jobContexts.size());
    JobDetailImpl jobDetail = (JobDetailImpl) jobContexts.get(0).getDetail();
    CronTrigger trigger = (CronTrigger) jobContexts.get(0).getTrigger();
    assertEquals(ScriptJob.class, jobDetail.getJobClass());
    assertEquals("/scripts/jobs/testJob.groovy", jobDetail.getJobDataMap().getString(ScriptJob.SCRIPT_URL_DATA_KEY));
    assertEquals(HOURLY_CRON_EXPRESSION, trigger.getCronExpression());
}
Also used : CronTrigger(org.quartz.CronTrigger) JobDetailImpl(org.quartz.impl.JobDetailImpl) JobContext(org.craftercms.engine.util.quartz.JobContext) Test(org.junit.Test)

Aggregations

JobContext (org.craftercms.engine.util.quartz.JobContext)6 Test (org.junit.Test)2 CronTrigger (org.quartz.CronTrigger)2 JobDetailImpl (org.quartz.impl.JobDetailImpl)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 ConfigurationException (org.craftercms.commons.config.ConfigurationException)1 ContentStoreService (org.craftercms.core.service.ContentStoreService)1 Context (org.craftercms.core.service.Context)1 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)1 ScriptJobResolver (org.craftercms.engine.scripting.ScriptJobResolver)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 JobDetail (org.quartz.JobDetail)1 Scheduler (org.quartz.Scheduler)1 Trigger (org.quartz.Trigger)1 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)1 BeansException (org.springframework.beans.BeansException)1