Skip to content

Commit fde6e97

Browse files
Cleanup, added logs
1 parent 8ee53b5 commit fde6e97

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ jar {
2626
dependencies {
2727
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
2828
testCompile group: 'junit', name: 'junit', version: '4.12'
29-
implementation 'com.squareup.okhttp3:okhttp:4.7.2'
3029
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
31-
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8"
3230
implementation 'com.microsoft.sqlserver:mssql-jdbc:8.2.2.jre8'
3331
implementation 'com.typesafe:config:1.3.3'
32+
implementation "io.github.microutils:kotlin-logging:1.6.22"
3433
}
3534

3635
compileKotlin {

src/main/kotlin/fi/hsl/transitdata/dbmonitor/Main.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package fi.hsl.transitdata.dbmonitor
22

33
import fi.hsl.transitdata.dbmonitor.config.ConfigParser
4+
import mu.KotlinLogging
45
import java.io.File
56
import java.sql.Connection
67
import java.sql.DriverManager
7-
import java.sql.SQLException
88
import java.util.*
9+
import java.util.concurrent.Executors
10+
import java.util.concurrent.TimeUnit
911

12+
private val log = KotlinLogging.logger {}
1013
fun main() {
14+
startScheduler()
15+
}
16+
17+
fun startScheduler(){
18+
val scheduler = Executors.newScheduledThreadPool(1)
1119
val config = ConfigParser.createConfig()
12-
MonitorService.start(config, createPubtransConnection())
20+
scheduler.scheduleWithFixedDelay(Runnable {
21+
try{
22+
MonitorService.start(config, createPubtransConnection())
23+
log.info("Db connection check complete")
24+
}
25+
catch(e : Exception){
26+
log.error("Error while checking the databases:",e)
27+
}
28+
}, 0 ,5 , TimeUnit.MINUTES)
1329
}
1430

1531
@Throws(Exception::class)
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
package fi.hsl.transitdata.dbmonitor
22

3-
import com.fasterxml.jackson.core.JsonFactory
4-
import com.fasterxml.jackson.databind.ObjectMapper
5-
import com.fasterxml.jackson.module.kotlin.KotlinModule
63
import com.typesafe.config.Config
74
import kotlinx.coroutines.Dispatchers
85
import kotlinx.coroutines.runBlocking
96
import kotlinx.coroutines.withContext
7+
import mu.KotlinLogging
108
import java.net.URL
119
import java.sql.Connection
12-
import java.sql.DriverManager
1310
import java.sql.Statement
1411

15-
1612
object MonitorService{
17-
18-
private val f = JsonFactory()
19-
private val mapper: ObjectMapper = ObjectMapper(f).registerModule(KotlinModule())
13+
private val log = KotlinLogging.logger {}
2014

2115
fun start(config : Config, connection : Connection)= runBlocking {
2216
try{
2317
connection.use {
2418
config.getConfigList("databases").forEach {
25-
endpointToCheck -> checkIfDbIfReachable(endpointToCheck.getString("dblabel"), endpointToCheck.getString("dbname"), it)
19+
endpointToCheck -> (
20+
try {
21+
checkIfDbIfReachable(endpointToCheck.getString("dblabel"), endpointToCheck.getString("dbname"), it)
22+
}
23+
catch(e : Exception){
24+
log.error ("Failed to connect to db", e)
25+
sendErrorMessageToSlack(e, config)
26+
})
2627
}
2728
}
2829
}
@@ -31,31 +32,29 @@ object MonitorService{
3132
}
3233
}
3334

34-
private fun sendErrorMessageToSlack(e : Exception, config : Config){
35+
private fun sendErrorMessageToSlack(e : Exception, config : Config, dblabel : String? = null){
3536

3637
val url = URL(config.getString("slackbridge"))
3738
val headers : Map<String, String> = mapOf(
3839
Pair("Content-type","application/json")
3940
)
40-
4141
NetworkHelper.getResponse(url, headers, config.getString("errormessage") + " " + e.message)
42-
throw e
4342
}
4443

4544
/**
4645
* Throws an exception if can't reach the db
4746
*/
4847
private suspend fun checkIfDbIfReachable(dblabel : String, dbname : String, connection : Connection){
49-
withContext(Dispatchers.IO) {
50-
try {
51-
val stmt: Statement = connection.createStatement()
52-
stmt.execute("use [$dbname]")
53-
stmt.close()
54-
assert(connection.isValid(5000))
55-
} catch (e: Exception) {
56-
throw Exception("Connection to DB $dblabel failed")
57-
}
58-
}
48+
withContext(Dispatchers.IO){
49+
try {
50+
val stmt: Statement = connection.createStatement()
51+
stmt.execute("use [$dbname]")
52+
stmt.close()
53+
assert(connection.isValid(5000))
54+
} catch (e: Exception) {
55+
throw Exception("Connection to DB $dblabel failed", e)
56+
}
57+
}
5958
}
6059
}
6160

0 commit comments

Comments
 (0)