Gopal S Akshintala
06/27/2022, 2:09 PMsimon.vergauwen
06/27/2022, 2:12 PMIs there an example, that uses persistence to suspend and resume a thread/job?Do you mean like a persisted cronjob using Scheduler for Kotlin? Something like that doesn't exist, but it should be possible to build something like this.
Gopal S Akshintala
06/27/2022, 3:13 PMsimon.vergauwen
06/27/2022, 6:37 PMIf Continuation can be serialized and stored in db to start, restart, retry and resume the job, this solves my use-case.This should be do-able, but given this issue I doubt it's possible yet atm 😞 https://github.com/Kotlin/kotlinx.coroutines/issues/76
Satyam Agarwal
06/29/2022, 5:43 AMSatyam Agarwal
06/29/2022, 5:43 AMsimon.vergauwen
06/29/2022, 8:24 AMfun main(): Unit = runBlocking(Dispatchers.Default) {
Schedule.spaced(7.days).repeat {
crawlWebsitesAndStore()
}
}
Satyam Agarwal
06/29/2022, 8:36 AMSo instead you want to “serialise” the current state, and restart it when the JVM starts again.By serialising the current state, we actually mean to write/persist the current state of the job somewhere when the instance dies so that it can be picked up next time new/old instance starts ?
simon.vergauwen
06/29/2022, 10:08 AMContinuation
/ Coroutine implementations that are found in the Kotlin Std, and KotlinX Coroutines.
I.e. if it can correctly serialise the state of suspend fun delay(x: Long)
it should even be able to keep track of how long it slept, and continue from there.Satyam Agarwal
06/30/2022, 7:21 AMSatyam Agarwal
06/30/2022, 7:22 AMGopal S Akshintala
06/30/2022, 7:42 AMSatyam Agarwal
06/30/2022, 8:00 AMSatyam Agarwal
06/30/2022, 8:01 AMSatyam Agarwal
06/30/2022, 8:01 AMSatyam Agarwal
06/30/2022, 8:02 AMSatyam Agarwal
06/30/2022, 8:04 AMsuspend fun scheduleRequestCacheEviction() {
val jobToExecute: Runnable = Runnable {
shedLockExecutor.executeWithLock(
Runnable { runBlocking { myService.evictFromRequestCache() } },
LockConfiguration(Instant.now(), "RequestCacheEviction", durationLockMax, durationLockMin)
)
}
scheduledExecutorService
.scheduleAtFixedRate(jobToExecute, jobDelayBeforeFirstRunInSeconds(), Duration.ofHours(6).seconds, SECONDS)
}
Satyam Agarwal
06/30/2022, 8:07 AMval cacheEvictionJob: CacheEvictionJob = object : CacheEvictionJob {
override val myService: MyService = myService
override val coroutineContext: CoroutineContext = IO
override val scheduledExecutorService: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
override val shedLockExecutor: LockingTaskExecutor = DefaultLockingTaskExecutor(lockProvider())
}
and I call this on app startup :
cacheEvictionJob.scheduleRequestCacheEviction()
Satyam Agarwal
06/30/2022, 8:07 AMCacheEvictionJob
extends CoroutineScope
Satyam Agarwal
06/30/2022, 8:09 AMSatyam Agarwal
06/30/2022, 8:10 AMSatyam Agarwal
06/30/2022, 8:16 AMGopal S Akshintala
06/30/2022, 8:17 AMSatyam Agarwal
06/30/2022, 8:25 AMSatyam Agarwal
06/30/2022, 8:26 AMsimon.vergauwen
06/30/2022, 8:37 AMSatyam Agarwal
06/30/2022, 8:41 AMGopal S Akshintala
06/30/2022, 8:59 AMIts really hard to make time to write code after my job hoursThanks @Satyam Agarwal I can totally relate 😄 and so I feel so grateful to OSS authors
I can try to set something up in the weekendA simple prototype can help me get started quickly!
simon.vergauwen
06/30/2022, 9:50 AMSo I thought Continuation implements Serializable from Java, and then persisting the whole object somewhere touse it later.
I wasn't able to comprehend it. It got me so excited 😅That is actually exactly what it does, and the goal of the linked ticket. It allows for building some extremely powerful things. I remember watching a talk about this on the very first KotlinConf but I cannot seem to recall which talk it was and cannot spot it on YouTube.
Satyam Agarwal
06/30/2022, 10:01 AM