I have a @Scheduled job that has a runBlocking { …...
# spring
p
I have a @Scheduled job that has a runBlocking { … } inside and performs cpu intensive work for multiple minutes. Due to this load, the actuator health endpoint becomes slow too, making the service seemingly unhealthy and thus triggers the cloud provider to restart the container. I tried isolating @Scheduled logic coroutine execution into a separate thread, but this does not help:
Copy code
val pollingContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
withContext(pollingContext) {
...
}
Anyone any ideas? Thanks!
a
You don't specify the cloud provider, so my answer is a bit generic. I've solved this issue using several methods. 1.) This is at best a kludge: put a
Thread.sleep(100)
(or similar) into your processing loop. Then call that task from an ExecutorPool. This will allow the vCPU some cycles with which to respond to health checks 2.) Completely remove that task to a different container and kick it off from the main service with an asynch message (or simply move the entire @Scheduled) there. Then you can adjust the health check for this long-running process as needed. I've moved such loads to dedicated EC2 instances (in AWS) and monitored them separately from the Kubernetes cluster (in the instance the process might run for 18-24 hours) 3.) If you are hosted in Kubernetes then by far the bast alternative is a Job/CronJob mechanism https://kubernetes.io/docs/concepts/workloads/controllers/job/ https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ Kubernetes deals with those differently (TTL vs "liveness" check) so they are better suited to the problem
👀 1