Permalink
Browse files

Tasks refactored. Begining to make sense.

  • Loading branch information...
1 parent 68581b2 commit 1dbf37d47ed6e83098baa9b449b5cb142d56be49 Damian committed Apr 16, 2012
View
51 src/main/java/uk/ac/bristol/dundry/dao/Repository.java
@@ -6,6 +6,7 @@
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableMap;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
@@ -25,6 +26,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import uk.ac.bristol.dundry.model.ResourceCollection;
import uk.ac.bristol.dundry.tasks.CopyTask;
+import uk.ac.bristol.dundry.tasks.JobBase;
/**
*
@@ -40,24 +42,10 @@
@Autowired private TaskManager taskManager;
private final FileRepository fileRepo;
private final MetadataStore mdStore;
- private final List<Class<? extends Job>> jobs;
- public Repository(FileRepository fileRepo, MetadataStore mdStore, List<String> jobsClasses) {
+ public Repository(FileRepository fileRepo, MetadataStore mdStore) {
this.fileRepo = fileRepo;
this.mdStore = mdStore;
-
- // Load up job classes
- jobs = new ArrayList<>();
- for (String jobClassName: jobsClasses) {
- // Try to load the class. Check it is a Job.
- try {
- Class<?> job = Repository.class.getClassLoader().loadClass(jobClassName);
- if (Job.class.isAssignableFrom(job)) jobs.add((Class<? extends Job>) job);
- else log.error("Class <{}> is not a Job. Ignoring.", jobClassName);
- } catch (ClassNotFoundException ex) {
- log.error("Job class <{}> not found. Ignoring.", jobClassName);
- }
- }
}
public ResourceCollection getIds() {
@@ -111,30 +99,19 @@ public String create(Path source, String creator, Resource subject) throws IOExc
mdStore.create(toInternalId(id), subject.getModel());
- startTasks(id, CopyTask.class, CopyTask.FROM, source, CopyTask.TO, repoDir);
+ // Start the post-deposit tasks
+ // Starting with the context for execution
+ ImmutableMap<String, ? extends Object> context =
+ ImmutableMap.of(
+ CopyTask.FROM, source,
+ JobBase.PATH, repoDir,
+ JobBase.REPOSITORY, this,
+ JobBase.ID, id);
- return id;
- }
-
- /**
- * Begin the post-creation tasks.
- *
- * @param loadJob First task to get get data into the repository
- * @param settings
- */
- private void startTasks(String id, Class<? extends Job> loadJob, Object... settings) throws SchedulerException {
- Map<String, Object> context = new HashMap<>();
-
- for (int i = 0; i < settings.length; i++) {
- context.put((String) settings[i], settings[i+1]);
- }
+ // Begin tasks, starting with copying
+ taskManager.startTasks(id, context, CopyTask.class);
- List<Class<? extends Job>> allJobs = new ArrayList<>();
-
- allJobs.add(loadJob);
- allJobs.addAll(jobs);
-
- taskManager.startJobs(id, allJobs, context);
+ return id;
}
public Resource getMetadata(String id) {
View
64 src/main/java/uk/ac/bristol/dundry/dao/TaskManager.java
@@ -1,6 +1,8 @@
package uk.ac.bristol.dundry.dao;
+import com.google.common.collect.Iterables;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.quartz.*;
@@ -14,18 +16,37 @@
import org.quartz.listeners.JobChainingJobListener;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
*
* @author Damian Steer <d.steer@bris.ac.uk>
*/
@Component
public class TaskManager {
+
+ static final Logger log = LoggerFactory.getLogger(TaskManager.class);
+
private final Scheduler scheduler;
+ private final ArrayList<Class<? extends Job>> defaultJobs;
- public TaskManager() throws SchedulerException {
+ public TaskManager(List<String> jobsClasses) throws SchedulerException {
this.scheduler = StdSchedulerFactory.getDefaultScheduler();
this.scheduler.start();
+
+ // Load up job classes
+ defaultJobs = new ArrayList<>();
+ for (String jobClassName: jobsClasses) {
+ // Try to load the class. Check it is a Job.
+ try {
+ Class<?> job = Repository.class.getClassLoader().loadClass(jobClassName);
+ if (Job.class.isAssignableFrom(job)) defaultJobs.add((Class<? extends Job>) job);
+ else log.error("Class <{}> is not a Job. Ignoring.", jobClassName);
+ } catch (ClassNotFoundException ex) {
+ log.error("Job class <{}> not found. Ignoring.", jobClassName);
+ }
+ }
}
public /*List<JobExecutionContext>*/ List<Object> listAllTasks() throws SchedulerException {
@@ -60,14 +81,14 @@ public void startJob(String id) throws SchedulerException {
.build();
jobs.add(job);
}
- startJobsInOrder(id, jobs);
+ executeJobsInOrder(id, jobs);
}
public void startJobs(String id, List<Class<? extends Job>> jobs, Map<String, Object> context)
throws SchedulerException {
List<JobDetail> jobDetails = new ArrayList<>();
- // Create context for these jobs
+ // Create context for these defaultJobs
JobDataMap jobData = new JobDataMap();
jobData.putAll(context);
@@ -82,18 +103,18 @@ public void startJobs(String id, List<Class<? extends Job>> jobs, Map<String, Ob
jobDetails.add(jobDetail);
}
- startJobsInOrder(id, jobDetails);
+ executeJobsInOrder(id, jobDetails);
}
- public void startJobsInOrder(String id, List<JobDetail> jobs) throws SchedulerException {
+ public void executeJobsInOrder(String id, List<JobDetail> jobs) throws SchedulerException {
if (jobs.isEmpty()) {
return;
}
- // Add all jobs to scheduler
+ // Add all defaultJobs to scheduler
for (JobDetail jd: jobs) scheduler.addJob(jd, true);
- // Make a chain of these jobs
+ // Make a chain of these defaultJobs
JobChainingJobListener jcl = new JobChainingJobListener("chain-" + id);
for (int i = 0; i < jobs.size() - 1; i++) {
jcl.addJobChainLink(jobs.get(i).getKey(), jobs.get(i + 1).getKey());
@@ -106,6 +127,35 @@ public void startJobsInOrder(String id, List<JobDetail> jobs) throws SchedulerEx
scheduler.triggerJob(jobs.get(0).getKey());
}
+ /**
+ * Begin a series of tasks. If provided it will start with the provided tasks,
+ * then run the standard tasks configured at startup.
+ * @param id The deposit id associated with this task
+ * @param context An execution context for the tasks
+ * @param jobs Jobs to run before default jobs.
+ */
+ public void startTasks(String id, Map<String, ? extends Object> context,
+ Class<? extends Job>... jobs) throws SchedulerException {
+
+ List<JobDetail> jobDetails = new ArrayList<>();
+
+ // Create context for these jobs
+ JobDataMap jobData = new JobDataMap();
+ jobData.putAll(context);
+
+ // Iterate through provided jobs, then defaults
+ for (Class<? extends Job> job: Iterables.concat(Arrays.asList(jobs), defaultJobs)) {
+ JobDetail jobDetail = newJob(job)
+ .withIdentity(job.getName(), id)
+ .usingJobData(jobData)
+ .build();
+
+ jobDetails.add(jobDetail);
+ }
+
+ executeJobsInOrder(id, jobDetails);
+ }
+
public static class HelloJob implements Job {
@Override
View
19 src/main/java/uk/ac/bristol/dundry/tasks/CopyTask.java
@@ -3,12 +3,10 @@
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
-import java.util.logging.Level;
import org.quartz.JobDataMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import uk.ac.bristol.dundry.Util;
-import uk.ac.bristol.dundry.dao.MetadataStore;
+import uk.ac.bristol.dundry.dao.Repository;
/**
*
@@ -20,29 +18,18 @@
public final static String FROM = "copy-task-from";
- public final static String TO = "copy-task-to";
@Override
- public void execute(MetadataStore store, String id, Path root, JobDataMap jobData) {
+ public void execute(Repository store, String id, Path root, JobDataMap jobData) {
Path from = (Path) jobData.get(FROM);
- Path to = (Path) jobData.get(TO);
+ Path to = root;
try {
copyDirectory(from, to);
} catch (IOException ex) {
throw new RuntimeException("Error copying", ex);
}
}
-
- @Override
- public String getName() {
- return "Copy durectory";
- }
-
- @Override
- public String getDescription() {
- return "Recursively copy a directory.";
- }
/**
* Recursively copy a directory into another directory.
View
17 src/main/java/uk/ac/bristol/dundry/tasks/JobBase.java
@@ -5,26 +5,27 @@
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
-import uk.ac.bristol.dundry.dao.MetadataStore;
+import uk.ac.bristol.dundry.dao.Repository;
/**
*
* @author Damian Steer <d.steer@bris.ac.uk>
*/
public abstract class JobBase implements Job {
-
+
+ public final static String REPOSITORY = "jobs-base-repository";
+ public final static String ID = "jobs-base-id";
+ public final static String PATH = "jobs-base-id";
+
@Override
final public void execute(JobExecutionContext jec) throws JobExecutionException {
JobDataMap jobData = jec.getMergedJobDataMap();
- execute((MetadataStore) jobData.get("store"),
- jobData.getString("id"), (Path) jobData.get("path"), jobData);
+ execute((Repository) jobData.get(REPOSITORY),
+ jobData.getString(ID), (Path) jobData.get(PATH), jobData);
// TODO: mark this as 'completed'
}
// Common case: task will go through file system, and put result in store
// TODO: maybe it should just return a model? or augment a resource?
- abstract public void execute(MetadataStore store, String id, Path root, JobDataMap jobData);
-
- abstract public String getName();
- abstract public String getDescription();
+ abstract public void execute(Repository store, String id, Path root, JobDataMap jobData);
}
View
5 src/main/java/uk/ac/bristol/dundry/webresources/Deposit.java
@@ -8,6 +8,7 @@
import java.net.URI;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
+import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,7 +47,7 @@ public Response list() {
public Response create(
@FormParam("source") String source,
@FormParam("title") String title,
- @FormParam("description") String description) throws IOException {
+ @FormParam("description") String description) throws IOException, SchedulerException {
log.info("Create deposit: {} title: {} desc: {}",
new String[]{ source, title, description });
@@ -62,7 +63,7 @@ public Response create(
@POST
@Consumes("application/json")
- public Response create(Resource record) throws IOException {
+ public Response create(Resource record) throws IOException, SchedulerException {
log.info("Create deposit: {}", record.getModel());

0 comments on commit 1dbf37d

Please sign in to comment.