generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e47aa70
commit 03acdb4
Showing
10 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/job/CronJobHelper.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,24 @@ | ||
package uk.gov.justice.digital.hmpps.job | ||
|
||
import io.opentelemetry.api.trace.Span | ||
import io.sentry.Sentry | ||
import org.springframework.boot.SpringApplication.exit | ||
import org.springframework.context.ApplicationContext | ||
import kotlin.system.exitProcess | ||
|
||
object CronJobHelper { | ||
fun ApplicationContext.runThenExit( | ||
exit: (code: Int) -> Unit = { exitProcess(exit(this, { it })) }, | ||
job: () -> Unit | ||
) { | ||
try { | ||
job.invoke() | ||
exit(0) | ||
} catch (e: Exception) { | ||
Sentry.captureException(e) | ||
Span.current().recordException(e) | ||
} finally { | ||
exit(1) | ||
} | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
libs/commons/src/main/kotlin/uk/gov/justice/digital/hmpps/logging/Logger.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,8 @@ | ||
package uk.gov.justice.digital.hmpps.logging | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
object Logger { | ||
fun <T : Any> T.logger(): Logger = LoggerFactory.getLogger(this::class.java) | ||
} |
45 changes: 45 additions & 0 deletions
45
libs/commons/src/test/kotlin/uk/gov/justice/digital/hmpps/job/CronJobHelperTest.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 uk.gov.justice.digital.hmpps.job | ||
|
||
import io.sentry.Sentry | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.mockStatic | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.never | ||
import org.mockito.kotlin.verify | ||
import org.springframework.context.ApplicationContext | ||
import uk.gov.justice.digital.hmpps.job.CronJobHelper.runThenExit | ||
|
||
@ExtendWith(MockitoExtension::class) | ||
class CronJobHelperTest { | ||
@Mock | ||
private lateinit var applicationContext: ApplicationContext | ||
|
||
@Test | ||
fun `captures exception and exits with code 1 on error`() { | ||
mockStatic(Sentry::class.java).use { sentry -> | ||
val exception = RuntimeException("error") | ||
val exitMock = mock<(code: Int) -> Unit>() | ||
|
||
applicationContext.runThenExit(exit = exitMock) { throw exception } | ||
|
||
sentry.verify { Sentry.captureException(exception) } | ||
verify(exitMock).invoke(1) | ||
} | ||
} | ||
|
||
@Test | ||
fun `runs successfully`() { | ||
mockStatic(Sentry::class.java).use { sentry -> | ||
val exitMock = mock<(code: Int) -> Unit>() | ||
|
||
applicationContext.runThenExit(exit = exitMock) { } | ||
|
||
sentry.verify({ Sentry.captureException(any()) }, never()) | ||
verify(exitMock).invoke(0) | ||
} | ||
} | ||
} |
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
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
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
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
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
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