Skip to content

Commit

Permalink
Add deleteAll method (#109)
Browse files Browse the repository at this point in the history
* Add deleteAll method

Removes all running jobs, schedules, and
clears the scheduler, without resorting
to shutting down.

* Update README.md with mass operation reference
  • Loading branch information
amdelamar committed Nov 7, 2021
1 parent 6c99b97 commit bb62ae4
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Expand Up @@ -520,3 +520,48 @@ try {
}
```

### Mass operations suspendAll, resumeAll, deleteAll

These mass operations let you manage many `JobSchedule`s at once.
*For working examples please check test section:*
*"The Quartz Scheduling Extension with Dynamic mass methods"*
*in* `com.typesafe.akka.extension.quartz.QuartzSchedulerFunctionalSpec`

#### Suspend All Schedules

Suspends (pauses) all jobs in the scheduler.

```scala
try {
scheduler.suspendAll()
} catch {
case e: SchedulerException =>
// Take action in case an exception is thrown
}
```

#### Resume All Schedules

Unpauses all paused jobs in the scheduler.

```scala
try {
scheduler.resumeAll()
} catch {
case e: SchedulerException =>
// Take action in case an exception is thrown
}
```

#### Delete All Schedules

Deletes all jobs in the scheduler without shutting down the scheduler.

```scala
try {
scheduler.deleteAll()
} catch {
case e: SchedulerException =>
// Take action in case an exception is thrown
}
```
10 changes: 10 additions & 0 deletions src/main/scala/QuartzSchedulerExtension.scala
Expand Up @@ -237,6 +237,16 @@ class QuartzSchedulerExtension(system: ActorSystem) extends Extension {
* @return Success or Failure in a Boolean
*/
def deleteJobSchedule(name: String): Boolean = unscheduleJob(name)

/**
* Deletes all jobs in the scheduler
*/
def deleteAll(): Unit = {
log.info("Deleting all Quartz jobs.")
runningJobs.clear()
schedules.clear()
scheduler.clear()
}

/**
* Unschedule an existing schedule
Expand Down
39 changes: 39 additions & 0 deletions src/test/scala/QuartzSchedulerFunctionalSpec.scala
Expand Up @@ -355,6 +355,45 @@ class QuartzSchedulerFunctionalSpec(_system: ActorSystem) extends TestKit(_syste

}

/**
* Mass operations {suspendAll, resumeAll, deleteAll} combine existing
* QuartzSchedulerExtension {pauseAll, resumeAll} and adds deleteAll
* (suspend synonym for pause for consistency).
*/
"The Quartz Scheduling Extension with Dynamic mass methods" should {

"Suspend all jobs" ignore {/* TODO implement */}

"Resume all jobs" ignore {/* TODO implement */}

"Delete all jobs" in {
val receiver = _system.actorOf(Props(new ScheduleTestReceiver))
val probe = TestProbe()
receiver ! NewProbe(probe.ref)
val toBeDeletedAllJobName1 = "toBeDeletedAll_1"
val toBeDeletedAllJobName2 = "toBeDeletedAll_2"
val jobDt1 = QuartzSchedulerExtension(_system).createJobSchedule(toBeDeletedAllJobName1, receiver, Tick, Some("Creating new dynamic schedule for deleteAll test"), "*/7 * * ? * *")
val jobDt2 = QuartzSchedulerExtension(_system).createJobSchedule(toBeDeletedAllJobName2, receiver, Tick, Some("Creating new dynamic schedule for deleteAll test"), "*/7 * * ? * *")

noException should be thrownBy {
// Deleting all scheduled jobs
QuartzSchedulerExtension(_system).deleteAll()

// Create a new schedule job reusing former toBeDeletedAllJobName1. This will fail if deleteAll is not effective.
val newJobDt1 = QuartzSchedulerExtension(_system).createJobSchedule(toBeDeletedAllJobName1, receiver, Tick, Some("Creating new dynamic schedule after deleteAll success"), "8 * * ? * *")
val jobCalender1 = Calendar.getInstance()
jobCalender1.setTime(newJobDt1)
jobCalender1.get(Calendar.SECOND) mustEqual 8

// Create a new schedule job reusing former toBeDeletedAllJobName2. This will fail if deleteAll is not effective.
val newJobDt2 = QuartzSchedulerExtension(_system).createJobSchedule(toBeDeletedAllJobName2, receiver, Tick, Some("Creating new dynamic schedule after deleteAll success"), "9 * * ? * *")
val jobCalender2 = Calendar.getInstance()
jobCalender2.setTime(newJobDt2)
jobCalender2.get(Calendar.SECOND) mustEqual 9
}
}
}

case class NewProbe(probe: ActorRef)
case object Tick
case object Tock
Expand Down

0 comments on commit bb62ae4

Please sign in to comment.