-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af255d9
commit adef59e
Showing
2 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/valensas/simplyquartz/ExecutionAwareTimedJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.valensas.simplyquartz | ||
|
||
import org.quartz.JobExecutionContext | ||
import org.quartz.PersistJobDataAfterExecution | ||
import org.slf4j.LoggerFactory | ||
import java.time.Instant | ||
import java.util.* | ||
|
||
@PersistJobDataAfterExecution | ||
abstract class ExecutionAwareTimedJob : TimedJob() { | ||
|
||
|
||
private val logger = LoggerFactory.getLogger(javaClass) | ||
|
||
override fun executeTimed(context: JobExecutionContext) { | ||
|
||
var success = true | ||
var lastErrorMessage = "" | ||
|
||
try { | ||
executeAwareTimed(context) | ||
} catch (e: Exception) { | ||
|
||
logger.error("Error in job", e) | ||
|
||
success = false | ||
lastErrorMessage = e.message ?: "Unknown error" | ||
} | ||
|
||
val lastFireTime = context.fireTime | ||
val lastFinishTime = Date.from(Instant.now()) | ||
val lastDuration = lastFinishTime.time - lastFireTime?.time!! | ||
|
||
context.jobDetail.jobDataMap["lastFireTime"] = lastFireTime | ||
context.jobDetail.jobDataMap["lastFinishTime"] = lastFinishTime | ||
context.jobDetail.jobDataMap["lastDuration"] = lastDuration | ||
context.jobDetail.jobDataMap["lastSuccess"] = success | ||
context.jobDetail.jobDataMap["lastErrorMessage"] = lastErrorMessage | ||
context.jobDetail.jobDataMap["executionId"] = UUID.randomUUID().toString() | ||
|
||
} | ||
|
||
abstract fun executeAwareTimed(context: JobExecutionContext) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters