Skip to content

Commit

Permalink
use db-scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
MajdT51 committed Aug 1, 2022
1 parent 377c13a commit 5ec3651
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ dependencies {
implementation("io.bkbn:kompendium-core:$kompendium_version")
implementation("io.bkbn:kompendium-auth:$kompendium_version")

implementation("com.github.kibertoad:ktor-scheduler:1.0.4")
implementation("org.jobrunr:jobrunr:5.1.6")
implementation("org.postgresql:postgresql:42.4.0")
implementation("org.xerial:sqlite-jdbc:3.36.0.3")

Expand All @@ -69,6 +67,9 @@ dependencies {

implementation("decentralized-identity:did-common-java:1.0.0")

// https://mvnrepository.com/artifact/com.github.kagkarlsson/db-scheduler
implementation("com.github.kagkarlsson:db-scheduler:11.2")

testImplementation("io.ktor:ktor-server-tests:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
testImplementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/********************************************************************************
* Copyright (c) 2021,2022 Contributors to the CatenaX (ng) GitHub Organisation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package org.eclipse.tractusx.managedidentitywallets.persistence.entities

import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.*

object SchedulerTasks : Table("scheduled_tasks") {
val taskName = text("task_name")
val taskInstance = text("task_instance")
val taskData = binary("task_data").nullable()
// It should be timestampWithTimeZone (timestampez), but jetbrains exposed does not support it yet
val executionTime = timestamp("execution_time")
val picked = bool("picked")
val pickedBy = text("picked_by").nullable()
val lastSuccess = timestamp("last_success").nullable()
val lastFailure = timestamp("last_failure").nullable()
val consecutiveFailures = integer("consecutive_failures").nullable()
val lastHeartbeat = timestamp("last_heartbeat").nullable()
val version = long("version")
override val primaryKey = PrimaryKey(taskName, taskInstance, name = "PK_Scheduler_Tasks")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.eclipse.tractusx.managedidentitywallets.plugins

import io.ktor.application.*
import org.eclipse.tractusx.managedidentitywallets.persistence.entities.SchedulerTasks

import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
Expand All @@ -33,7 +34,7 @@ fun Application.configurePersistence() {
Database.connect(jdbcUrl, driver = jdbcDriver)
transaction {
// Create missing tables
SchemaUtils.createMissingTablesAndColumns(Wallets, VerifiableCredentials)
SchemaUtils.createMissingTablesAndColumns(Wallets, VerifiableCredentials, SchedulerTasks)
commit()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,40 @@

package org.eclipse.tractusx.managedidentitywallets.plugins

import com.github.kagkarlsson.scheduler.Scheduler
import com.github.kagkarlsson.scheduler.task.helper.RecurringTask
import com.github.kagkarlsson.scheduler.task.helper.Tasks
import com.github.kagkarlsson.scheduler.task.schedule.Schedules
import io.ktor.application.*
import kotlinx.coroutines.runBlocking
import net.kiberion.ktor_scheduler.Scheduler
import net.kiberion.ktor_scheduler.recurringJob
import net.kiberion.ktor_scheduler.schedule
import org.eclipse.tractusx.managedidentitywallets.Services
import org.jobrunr.scheduling.cron.Cron
import org.jobrunr.storage.AbstractStorageProvider
import org.jobrunr.storage.sql.common.SqlStorageProviderFactory
import org.postgresql.ds.PGSimpleDataSource
import org.sqlite.SQLiteDataSource
import java.sql.DriverManager
import java.time.Duration
import javax.sql.DataSource

fun Application.configureJobs() {

val jdbcUrl = environment.config.property("db.jdbcUrl").getString()
val pullDataAtHour = environment.config.property("bpdm.pullDataAtHour").getString().toInt()

install(Scheduler) {
storageProvider = SqlStorageProviderFactory.using(initDatabase(jdbcUrl)) as AbstractStorageProvider
threads = 5
}

schedule {
recurringJob("bpdm-update", Cron.daily(pullDataAtHour)) {
val bpdmUpdate: RecurringTask<Void> = Tasks.recurring("bpdm-update",
// Spring Scheduled tasks (second, minute, hour, day of month, month, day(s) of week)
Schedules.cron("0 0 $pullDataAtHour * * *"))
.execute { inst, ctx ->
runJobPayload()
}
}

val scheduler: Scheduler = Scheduler
.create(initDatabase(jdbcUrl))
.startTasks(bpdmUpdate)
.pollingInterval(Duration.ofHours(1))
.registerShutdownHook()
.threads(3)
.build()

scheduler.start()
}

fun initDatabase(jdbcUrl: String): DataSource {
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
<logger name="Exposed" level="${LOG_LEVEL_EXPOSED}"> </logger>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="io.netty" level="INFO"/>
<logger name="org.jobrunr" level="INFO"/>
<logger name="com.zaxxer" level="INFO"/>
</configuration>

0 comments on commit 5ec3651

Please sign in to comment.