Skip to content

Commit

Permalink
#10 make it possible to create cron jobs with site.globals.js or module
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Aug 21, 2024
1 parent b0251d4 commit 2081f51
Show file tree
Hide file tree
Showing 23 changed files with 715 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.thmarx.cms.api.feature.features;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.feature.Feature;
import com.github.thmarx.cms.api.scheduler.CronJobScheduler;

/**
*
* @author t.marx
*/
public record CronJobSchedulerFeature(CronJobScheduler cronJobScheduler) implements Feature {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
*/
public interface CronJobScheduler {

void schedule (String cronExpression, CronJob job);
void schedule (String cronExpression, String name, CronJob job);

void remove (String name);

boolean exists (String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,44 @@
*/

import com.github.thmarx.cms.api.scheduler.CronJob;
import com.github.thmarx.cms.api.scheduler.CronJobScheduler;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import java.util.logging.Level;
import java.util.logging.Logger;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
*
* @author t.marx
*/
@RequiredArgsConstructor
@Slf4j
public class CMSCronJobScheduler implements CronJobScheduler, AutoCloseable {
public class AbstractCronJobScheduler {

Scheduler scheduler;
private final Scheduler scheduler;
private final CronJobContext context;

@Override
public void schedule(String cronExpression, CronJob job) {
protected void schedule(
String cronExpression,
String name,
CronJob job,
Class<? extends Job> jobClass) {
JobDataMap data = new JobDataMap();
data.put("cronJob", job);
data.put(SingleCronJobRunner.DATA_CRONJOB, job);
data.put(SingleCronJobRunner.DATA_CONTEXT, context);
JobDetail jobDetail = JobBuilder
.newJob(CronJobRunner.class)
.newJob(jobClass)
.withIdentity(name)
.usingJobData(data)
.build();

Expand All @@ -67,22 +77,23 @@ public void schedule(String cronExpression, CronJob job) {
throw new RuntimeException(ex);
}
}

public void open () {
protected boolean exists (String name) {
try {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.getScheduler();
scheduler.start();
return scheduler.checkExists(JobKey.jobKey(name));
} catch (SchedulerException ex) {
log.error(null, ex);
log.error("", ex);
throw new RuntimeException(ex);
}
}


@Override
public void close() throws Exception {
if (scheduler != null) {
scheduler.shutdown();
protected void remove(String name) {
try {
scheduler.deleteJob(JobKey.jobKey(name));
} catch (SchedulerException ex) {
log.error("", ex);
throw new RuntimeException(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.thmarx.cms.core.scheduler;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/


import com.github.thmarx.cms.api.scheduler.CronJob;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import static com.github.thmarx.cms.core.scheduler.SingleCronJobRunner.DATA_CONTEXT;
import static com.github.thmarx.cms.core.scheduler.SingleCronJobRunner.DATA_CRONJOB;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
*
* @author t.marx
*/
public class DefaultCronJobRunner implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if (context.getJobDetail().getJobDataMap().get(DATA_CRONJOB) != null) {
CronJobContext jobContext = (CronJobContext) context.getJobDetail().getJobDataMap().get(DATA_CONTEXT);
((CronJob)context.getJobDetail().getJobDataMap().get(DATA_CRONJOB)).accept(jobContext);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.thmarx.cms.core.scheduler;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/


import com.github.thmarx.cms.api.scheduler.CronJob;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
*
* @author t.marx
*/
public class ScriptCronJobRunner implements Job {

public static final String DATA_CRONJOB = "cronjob";
public static final String DATA_CONTEXT = "context";

private static final Lock LOCK = new ReentrantLock(true);

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if (context.getJobDetail().getJobDataMap().get(DATA_CRONJOB) != null) {
CronJobContext jobContext = (CronJobContext) context.getJobDetail().getJobDataMap().get(DATA_CONTEXT);

LOCK.lock();
try {
((CronJob)context.getJobDetail().getJobDataMap().get(DATA_CRONJOB)).accept(jobContext);
} finally {
LOCK.unlock();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import com.github.thmarx.cms.api.scheduler.CronJob;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
Expand All @@ -33,16 +35,24 @@
*
* @author t.marx
*/
public class CronJobRunner implements Job {
public class SingleCronJobRunner implements Job {

public static final String DATA_CRONJOB = "cronjob";
public static final String DATA_CONTEXT = "context";

private static Lock lock = new ReentrantLock(true);

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if (context.get(DATA_CRONJOB) != null) {
CronJobContext jobContext = (CronJobContext) context.get(DATA_CONTEXT);
((CronJob)context.get(DATA_CRONJOB)).accept(null);
if (context.getJobDetail().getJobDataMap().get(DATA_CRONJOB) != null) {
CronJobContext jobContext = (CronJobContext) context.getJobDetail().getJobDataMap().get(DATA_CONTEXT);

lock.lock();
try {
((CronJob)context.getJobDetail().getJobDataMap().get(DATA_CRONJOB)).accept(jobContext);
} finally {
lock.unlock();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.thmarx.cms.core.scheduler;

/*-
* #%L
* cms-core
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.scheduler.CronJob;
import com.github.thmarx.cms.api.scheduler.CronJobContext;
import com.github.thmarx.cms.api.scheduler.CronJobScheduler;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;

/**
*
* @author t.marx
*/
@Slf4j
public class SingleCronJobScheduler extends AbstractCronJobScheduler implements CronJobScheduler {

public SingleCronJobScheduler(Scheduler scheduler, CronJobContext context) {
super(scheduler, context);
}

@Override
public void schedule(String cronExpression, String name, CronJob job) {
super.schedule(cronExpression, name, job, SingleCronJobRunner.class);
}

@Override
public void remove(String name) {
super.remove(name);
}

@Override
public boolean exists(String name) {
return super.exists(name);
}
}
Loading

0 comments on commit 2081f51

Please sign in to comment.