Skip to content

Commit 6d9081f

Browse files
authored
Handle unrecognized job type properly (#30)
1 parent d7835ce commit 6d9081f

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ libraryDependencies ++= Seq(
1414

1515
organization := "givers.moonlight"
1616
name := "play-moonlight"
17-
version := "0.16.1"
17+
version := "0.16.2"
1818
parallelExecution in Test := false
1919

2020
publishMavenStyle := true

src/main/scala/givers/moonlight/Main.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ class Work @Inject()(
204204
return
205205
}
206206

207-
val runnable = getWorker(job.jobType)
208-
209207
val startInMillis = Instant.now().toEpochMilli
210208
try {
209+
val runnable = getWorker(job.jobType)
210+
211211
await(backgroundJobService.start(job.id))
212212
logger.info(s"Started ${runnable.getClass.getSimpleName} (id=${job.id})")
213213
runnable.run(job)
@@ -217,12 +217,12 @@ class Work @Inject()(
217217
case e: InterruptedException => throw e
218218
case e: Throwable =>
219219
await(backgroundJobService.fail(job.id, e))
220-
logger.error(s"Error occurred while running ${runnable.getClass.getSimpleName} (id=${job.id}, type=${job.jobType}, params=${job.paramsInJsonString}.", e)
221-
logger.info(s"Finished ${runnable.getClass.getSimpleName} (id=${job.id}) with the above error")
220+
logger.error(s"Error occurred while running the job (id=${job.id}, type=${job.jobType}, params=${job.paramsInJsonString}, tryCount=${job.tryCount}).", e)
222221
throw e
222+
} finally {
223+
val duration = Instant.now().toEpochMilli - startInMillis
224+
logger.info(s"The job (id=${job.id}) took $duration millis")
223225
}
224-
val duration = Instant.now().toEpochMilli - startInMillis
225-
logger.info(s"The job (id=${job.id}) took $duration millis")
226226
}
227227

228228
private[moonlight] def getWorker(jobType: String): Worker[_] = {

src/test/scala/givers.moonlight/WorkSpec.scala

+16
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ object WorkSpec extends BaseSpec {
9999
verifyNoMoreInteractions(backgroundJobService)
100100
}
101101

102+
"Unrecognized job type fails the job" - {
103+
val unrecognizedJob = job.copy(id = 1234L, jobType = "ItsUnrecognizedJobType")
104+
when(backgroundJobService.getById(any())).thenReturn(Future(Some(unrecognizedJob)))
105+
when(worker.run(any())).thenAnswer(new Answer[Unit] {
106+
override def answer(invocation: InvocationOnMock) = throw new Exception("FakeError")
107+
})
108+
109+
intercept[Exception] {
110+
work.runJob(unrecognizedJob.id)
111+
}
112+
113+
verify(backgroundJobService).getById(unrecognizedJob.id)
114+
verify(backgroundJobService).fail(eq(unrecognizedJob.id), argThat { e: Throwable => e.getMessage == "Unrecognized job type 'ItsUnrecognizedJobType'." })
115+
verifyNoMoreInteractions(backgroundJobService)
116+
}
117+
102118
"InterruptedException occurs" - {
103119
when(worker.run(any())).thenAnswer(new Answer[Unit] {
104120
override def answer(invocation: InvocationOnMock) = throw new InterruptedException()

0 commit comments

Comments
 (0)