Skip to content

Commit

Permalink
HTM-1225: implement prioritising job triggers (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
mprins authored Oct 11, 2024
2 parents da071b2 + 67a48de commit 497b032
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,9 @@ private void createPocTasks() {
"foo",
"bar",
"description",
"POC task that runs every hour")),
"POC task that runs every hour",
"priority",
10)),
/* run every hour */ "0 0 0/1 1/1 * ? *"));
} catch (SchedulerException e) {
logger.error("Error creating scheduling poc tasks", e);
Expand Down
40 changes: 38 additions & 2 deletions src/main/java/org/tailormap/api/scheduling/TMJobDataMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ public class TMJobDataMap extends HashMap<String, Object> {
public TMJobDataMap(Map<String, Object> map) {
this((String) map.get("type"), (String) map.get("description"));
this.putAll(map);
// validate the priority
this.setPriority((Integer) map.getOrDefault("priority", Trigger.DEFAULT_PRIORITY));
}

/**
* Create a new instance of TMJobDataMap with a status of {@code Trigger.TriggerState.NONE}.
* Create a new instance of TMJobDataMap with a status of {@code Trigger.TriggerState.NONE} and a
* default priority.
*
* @param type the type of the job
* @param description a description of the job
Expand All @@ -36,14 +39,30 @@ public TMJobDataMap(@NotNull String type, @NotNull String description) {
}

/**
* Create a new instance of TMJobDataMap.
* Create a new instance of TMJobDataMap with default priority.
*
* @param type the type of the job
* @param description a description of the job
* @param status the status of the job
*/
public TMJobDataMap(
@NotNull String type, @NotNull String description, @NotNull Trigger.TriggerState status) {
this(type, description, status, Trigger.DEFAULT_PRIORITY);
}

/**
* Create a new instance of TMJobDataMap.
*
* @param type the type of the job
* @param description a description of the job
* @param status the status of the job
* @param priority the priority of the job, an integer value equal or greater than 0
*/
public TMJobDataMap(
@NotNull String type,
@NotNull String description,
@NotNull Trigger.TriggerState status,
int priority) {
super();
// Check if the map contains the required parameters
Assert.notNull(type, "type must not be null");
Expand All @@ -52,6 +71,7 @@ public TMJobDataMap(
super.put("type", type);
super.put("description", description);
super.put("status", status);
setPriority(priority);
}

@NotNull
Expand All @@ -75,4 +95,20 @@ public void setStatus(Trigger.TriggerState status) {
}
super.put("status", status);
}

/**
* Set the priority of the job.
*
* @param priority the priority of the job, an integer value equal or greater than 0
*/
public void setPriority(int priority) {
if (priority < 0) {
priority = 0;
}
super.put("priority", priority);
}

public int getPriority() {
return (int) super.get("priority");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public String createTask(
TriggerBuilder.newTrigger()
.withIdentity(jobDetail.getKey().getName(), jobDetail.getKey().getGroup())
.startAt(DateBuilder.futureDate(30, DateBuilder.IntervalUnit.SECOND))
.withPriority(jobData.getPriority())
.withSchedule(
CronScheduleBuilder.cronSchedule(cronExpression)
.withMisfireHandlingInstructionFireAndProceed())
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/org/tailormap/api/scheduling/TMJobDataMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ void testInvalidMap() {
assertThrows(IllegalArgumentException.class, () -> new TMJobDataMap(Map.of("type", "test")));
}

/** Test the creation using a map with missing required parameters. */
/** Test the creation using a map with all required parameters. */
@Test
void testMap() {
TMJobDataMap jobDataMap = new TMJobDataMap(Map.of("type", "test", "description", "test"));
assertNotNull(jobDataMap);
assertEquals("NONE", jobDataMap.getStatus().name());
assertEquals(5, jobDataMap.getPriority());
}

/** Test the creation using a map with required parameters and negative priority. */
@Test
void testMapWithPriority() {
TMJobDataMap jobDataMap =
new TMJobDataMap(Map.of("type", "test", "description", "test", "priority", -1));
assertNotNull(jobDataMap);
assertEquals("NONE", jobDataMap.getStatus().name());
assertEquals(0, jobDataMap.getPriority());
}
}

0 comments on commit 497b032

Please sign in to comment.