Permalink
Browse files

add

  • Loading branch information...
1 parent 5eb09e1 commit f7c0710e09930ba9dea80ca0ded01fda2f5dc21d Felipe Oliveira committed Apr 28, 2011
Showing with 236 additions and 32 deletions.
  1. +5 −3 app/controllers/PlayJobsDashboard.java
  2. +17 −1 app/views/PlayJobsDashboard/index.html
  3. BIN eclipse/classes/controllers/PlayJobsDashboard.class
  4. BIN lib/play-jobs.jar
  5. +17 −2 public-play-jobs/stylesheets/style.css
  6. +47 −3 src/play/modules/jobs/PlayJobs.java
  7. +1 −1 src/play/modules/jobs/PlayJobsPlugin.java
  8. +71 −22 src/play/modules/jobs/PlayJobsService.java
  9. +78 −0 src/play/modules/jobs/util/Exceptions.java
  10. BIN tmp/bytecode/DEV/DocViewerPlugin
  11. BIN tmp/bytecode/DEV/_app_views_PlayJobsDashboard_index.html
  12. BIN tmp/bytecode/DEV/_app_views_PlayJobsDashboard_main.html
  13. BIN tmp/bytecode/DEV/_play__framework_templates_errors_404.html
  14. BIN tmp/bytecode/DEV/_play__framework_templates_errors_500.html
  15. BIN tmp/bytecode/DEV/_play__framework_templates_tags_404.html
  16. BIN tmp/bytecode/DEV/_play__framework_templates_tags_500.html
  17. BIN tmp/bytecode/DEV/controllers.PlayDocumentation
  18. BIN tmp/bytecode/DEV/controllers.PlayJobsDashboard
  19. BIN tmp/bytecode/DEV/controllers.RESTEasyController
  20. BIN tmp/bytecode/DEV/play.modules.jobs.PlayJobs
  21. BIN tmp/bytecode/DEV/play.modules.jobs.PlayJobs$JobEntry
  22. BIN tmp/bytecode/DEV/play.modules.jobs.PlayJobsPlugin
  23. BIN tmp/bytecode/DEV/play.modules.jobs.PlayJobsService
  24. BIN tmp/bytecode/DEV/play.modules.jobs.sample.AppStartSampleJob
  25. BIN tmp/bytecode/DEV/play.modules.jobs.sample.BaseSampleJob
  26. BIN tmp/bytecode/DEV/play.modules.jobs.sample.CronSampleJob
  27. BIN tmp/bytecode/DEV/play.modules.jobs.sample.EverySoOftenSampleJob
  28. BIN tmp/bytecode/DEV/play.modules.jobs.sample.NoTriggerSampleJob
  29. BIN tmp/bytecode/DEV/play.modules.jobs.util.Exceptions
  30. BIN tmp/classes/DocViewerPlugin.class
  31. BIN tmp/classes/controllers/PlayDocumentation.class
  32. BIN tmp/classes/controllers/PlayJobsDashboard.class
  33. BIN tmp/classes/controllers/RESTEasyController.class
  34. BIN tmp/classes/play/modules/jobs/PlayJobs$JobEntry.class
  35. BIN tmp/classes/play/modules/jobs/PlayJobs.class
  36. BIN tmp/classes/play/modules/jobs/PlayJobsPlugin.class
  37. BIN tmp/classes/play/modules/jobs/PlayJobsService.class
  38. BIN tmp/classes/play/modules/jobs/sample/AppStartSampleJob.class
  39. BIN tmp/classes/play/modules/jobs/sample/BaseSampleJob.class
  40. BIN tmp/classes/play/modules/jobs/sample/CronSampleJob.class
  41. BIN tmp/classes/play/modules/jobs/sample/EverySoOftenSampleJob.class
  42. BIN tmp/classes/play/modules/jobs/sample/NoTriggerSampleJob.class
  43. BIN tmp/classes/play/modules/jobs/util/Exceptions.class
@@ -18,6 +18,7 @@
*/
package controllers;
+import play.Logger;
import play.modules.jobs.PlayJobs;
import play.modules.jobs.PlayJobsService;
import play.mvc.Controller;
@@ -32,7 +33,7 @@
*/
public static void index() {
PlayJobsService service = new PlayJobsService();
- PlayJobs jobs = service.getScheduledJobs();
+ PlayJobs jobs = service.getJobs();
render(jobs);
}
@@ -41,9 +42,10 @@ public static void index() {
*
* @para m jobClass the job class
*/
- public static void executeNow(String jobClass) {
+ public static void executeNow(String jobClass, Integer instances) {
+ Logger.info(String.format("Job Class: %s, Number of Concurrent Instances: %s", jobClass, instances));
PlayJobsService service = new PlayJobsService();
- service.triggerJob(jobClass);
+ service.triggerJob(jobClass, instances);
flash.success("Triggered Job: %s", jobClass);
index();
}
@@ -31,7 +31,23 @@
<td>${job.runOnApplicationStart}</td>
<td>${job.runOnCron}</td>
<td>${job.runEverySoOften}</td>
- <td><a href="@{executeNow(job.jobClass.name)}">Trigger Job</a></td>
+ <td>
+ <form action="@{executeNow()}" method="POST">
+ <input type="hidden" name="jobClass" value="${job.jobClass.name}">
+ <select name="instances">
+ <option value="1">1
+ <option value="5">5
+ <option value="10">10
+ <option value="25">25
+ <option value="50">50
+ <option value="100">100
+ <option value="250">250
+ <option value="500">500
+ <option value="1000">1000
+ </select><br>
+ <input type="submit" value="trigger x no. of concurrent jobs" class="triggerButton">
+ </form>
+ </td>
</tr>
#{/if}
#{/list}
Binary file not shown.
View
Binary file not shown.
@@ -218,14 +218,13 @@ td a, td input[type=submit]
font-family: "Qlassik Bold";
font-weight: bold;
xtext-shadow: none;
- padding: 0 3px 0 5px;
+ padding: 50 3px 50 5px;
color: #3E6300;
-webkit-border-radius: 10px;
-webkit-box-shadow: rgba(0,0,0,0.5) 0 1px 1px;
-moz-border-radius: 10px;
-moz-box-shadow: rgba(0,0,0,0.5) 0 1px 1px;
cursor: pointer;
- width: 16px;
height: 16px;
}
@@ -250,6 +249,21 @@ td input
width: 200px;
}
+td select
+{
+ font-family: "Qlassik Bold";
+ text-align: left;
+ font-size: 15px;
+ padding: 20;
+ border: none;
+ border-bottom: 1px solid rgba(255, 255, 0, .3);
+ background: transparent;
+ color: gold;
+ display: block;
+ margin: -3px -1px;
+ width: 200px;
+}
+
td.editing
{
padding: 0px 5px;
@@ -441,4 +455,5 @@ footer a:hover
{
-khtml-user-drag: element;
}
+
@@ -19,6 +19,7 @@
package play.modules.jobs;
import java.lang.annotation.Annotation;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -138,7 +139,7 @@ public int getQueueSize() {
/**
* The Class JobEntry.
*/
- public static class JobEntry {
+ public static class JobEntry implements Comparator<JobEntry> {
/** The job class. */
private Class jobClass;
@@ -166,6 +167,19 @@ public JobEntry() {
}
/**
+ * Instantiates a new job entry.
+ *
+ * @param jobClass
+ * the job class
+ */
+ public JobEntry(Class jobClass) {
+ this.jobClass = jobClass;
+ this.runOnApplicationStart = this.annotationCheck(OnApplicationStart.class, jobClass);
+ this.runOnCron = this.annotationCheck(On.class, jobClass);
+ this.runEverySoOften = this.annotationCheck(Every.class, jobClass);
+ }
+
+ /**
* Instantiates a new schedule job.
*
* @param job
@@ -190,10 +204,23 @@ public JobEntry(Job job) {
* @return the string
*/
private String annotationCheck(Class<? extends Annotation> clazz, Job job) {
- if (job == null) {
+ return this.annotationCheck(clazz, job.getClass());
+ }
+
+ /**
+ * Annotation check.
+ *
+ * @param clazz
+ * the clazz
+ * @param jobClass
+ * the jobClass
+ * @return the string
+ */
+ private String annotationCheck(Class<? extends Annotation> clazz, Class jobClass) {
+ if (jobClass == null) {
return "n/a";
}
- if (job.getClass().isAnnotationPresent(clazz)) {
+ if (jobClass.isAnnotationPresent(clazz)) {
return "Yes";
} else {
return "-";
@@ -254,6 +281,23 @@ public String getLastRunWasError() {
return this.lastRunWasError;
}
+ /**
+ * Compare.
+ *
+ * @param arg0
+ * the arg0
+ * @param arg1
+ * the arg1
+ * @return the int
+ */
+ @Override
+ public int compare(JobEntry arg0, JobEntry arg1) {
+ if ((arg0 != null) && (arg1 != null) && (arg0.getClass() != null) && (arg1.getClass() != null)) {
+ return arg0.getClass().getName().compareTo(arg1.getClass().getName());
+ }
+ return 0;
+ }
+
}
}
@@ -11,7 +11,7 @@
@Override
public void onApplicationStart() {
Router.addRoute("GET", "/@jobs", "PlayJobsDashboard.index");
- Router.addRoute("GET", "/@jobs/executeNow", "PlayJobsDashboard.executeNow");
+ Router.addRoute("*", "/@jobs/executeNow", "PlayJobsDashboard.executeNow");
}
}
@@ -18,16 +18,24 @@
*/
package play.modules.jobs;
+import static play.modules.jobs.util.Exceptions.logError;
+
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import org.apache.commons.lang.StringUtils;
+
import play.Logger;
import play.Play;
+import play.classloading.ApplicationClasses.ApplicationClass;
import play.exceptions.UnexpectedException;
import play.jobs.Job;
import play.jobs.JobsPlugin;
@@ -47,7 +55,7 @@
@GET
@Path("/list")
@Produces("application/json")
- public PlayJobs getScheduledJobs() {
+ public PlayJobs getJobs() {
// Get Jobs Plugin
JobsPlugin plugin = Play.plugin(JobsPlugin.class);
if (plugin == null) {
@@ -61,13 +69,14 @@ public PlayJobs getScheduledJobs() {
int queueSize = plugin.executor.getPoolSize();
// Get Scheduled Jobs
- List<Job> scheduledJobs = plugin.scheduledJobs;
-
- // Wrap Pojos
List<JobEntry> jobEntries = new ArrayList<JobEntry>();
- if (scheduledJobs != null) {
- for (Job j : scheduledJobs) {
- jobEntries.add(new JobEntry(j));
+ List<ApplicationClass> classes = Play.classes.all();
+ if (classes != null) {
+ // Wrap Pojos
+ for (Class clazz : Play.classloader.getAllClasses()) {
+ if ((clazz != null) && Job.class.isAssignableFrom(clazz)) {
+ jobEntries.add(new JobEntry(clazz));
+ }
}
}
@@ -86,27 +95,67 @@ public PlayJobs getScheduledJobs() {
* the job class
*/
@GET
- @Path("/trigger/{jobClass}")
+ @Path("/trigger/{jobClass}/${instances}")
@Produces("application/json")
- public void triggerJob(@PathParam("jobClass") String jobClass) {
- // Get Plugin
- JobsPlugin plugin = Play.plugin(JobsPlugin.class);
+ public void triggerJob(@PathParam("jobClass") String jobClass, @PathParam("numberOfInstances") Integer instances) {
+ // Jobs Plugin
+ Play.plugin(JobsPlugin.class);
+
+ // Check Job Class
+ if (StringUtils.isBlank(jobClass)) {
+ throw new RuntimeException("Invalid Job Class!");
+ }
+
+ // Check Number of Instances
+ if (instances == null) {
+ instances = 1;
+ }
+ if (instances < 1) {
+ instances = 1;
+ }
+
+ // Log Debug
+ Logger.info("Trigger Job - Class: %s, Number of Instances: %s", jobClass, instances);
+
+ // Trigger Job(s)
+ try {
+ // Define Class
+ Class clazz = Class.forName(jobClass);
+ if (clazz == null) {
+ throw new RuntimeException("Invalid Job Class: " + jobClass);
+ }
+
+ // Define Executor Service
+ ExecutorService executor = Executors.newFixedThreadPool(instances);
+
+ // Loop on number of instances needed
+ int count = 0;
+ for (int i = 0; i < instances; i++) {
+ // Add Count (I don't like to mix with "i" to avoid causing bugs
+ // when code gets refactored or something that I can't predict)
+ count = count + 1;
+
+ // Create Instance
+ Object o = clazz.newInstance();
+
+ // Check Instance Type
+ if ((o instanceof Job) == false) {
+ throw new RuntimeException("Invalid Class Instance: " + o);
+ }
- // Look for Job
- List<Job> scheduledJobs = plugin.scheduledJobs;
- for (Job job : scheduledJobs) {
- // Check Class Match
- if ((job != null) && job.getClass().getName().equals(jobClass)) {
// Log Debug
- Logger.info("Firing Job: %s", job);
+ Logger.info("Triggering Job: %s", o);
// Fire Job
- job.now();
+ Job job = (Job) o;
+ executor.submit((Callable) job);
+ Logger.info("%s) Triggered Job: %s", count, job);
}
- }
- // Job wasn't found
- throw new UnexpectedException(String.format("Couldn't find job %s on current list of scheduled jobs %s", jobClass, scheduledJobs));
+ } catch (Throwable t) {
+ // Job wasn't found
+ logError(t);
+ throw new UnexpectedException(String.format("Couldn't trigger job: %s", jobClass));
+ }
}
-
}
Oops, something went wrong.

0 comments on commit f7c0710

Please sign in to comment.