kagkarlsson / db-scheduler

Persistent cluster-friendly scheduler for Java
Apache License 2.0
1.23k stars 188 forks source link

Minimum FixedDelay for Recurring Task 10 seconds? #513

Closed driessamyn closed 1 month ago

driessamyn commented 2 months ago

Prerequisites

Expected Behavior

Using the example code below, I was expecting a task execution every 500ms.

Current Behavior

Using the example code below I can see a task execution every 10 seconds (see logs).

Steps to Reproduce the bug

  1. Run the code below

Context

Logs

[2024-07-09T13:22:18.965567Z] Executed!
[2024-07-09T13:22:28.968643Z] Executed!
[2024-07-09T13:22:38.979854Z] Executed!
[2024-07-09T13:22:48.990616Z] Executed!
[2024-07-09T13:22:59.001506Z] Executed!
[2024-07-09T13:23:09.010734Z] Executed!
[2024-07-09T13:23:19.027571Z] Executed!
[2024-07-09T13:23:29.033023Z] Executed!
[2024-07-09T13:23:39.044822Z] Executed!

Code

val shortIntervalTask = Tasks.recurring("my-task", FixedDelay.of(Duration.ofMillis(500)))
        .execute { _: TaskInstance<Void?>?, _: ExecutionContext? ->
            println("[${Instant.now()}] Executed!")
        }

val scheduler = Scheduler
    .create(dataSource)
    .startTasks<RecurringTask<Void>>(shortIntervalTask)
    .threads(5)
    .build()

scheduler.start()
nicktar commented 1 month ago

The default polling interval for new jobs is 10 seconds. If you need shorter delays, you need to configure the scheduler accordingly using .pollingInterval(Duration) with a duration that's shorter than your minimum delay. I think to reliably trigger at your interval, it needs to be no longer than half your minimum delay...

driessamyn commented 1 month ago

Thanks @nicktar.

That works perfectly 👍

For completeness, here's this is the version that works:

        val f = File("/tmp/log.txt")
        val id = UUID.randomUUID().toString()
        val shortIntervalTask = Tasks.recurring("my-task", FixedDelay.of(Duration.ofMillis(500)))
            .execute { _: TaskInstance<*>, _: ExecutionContext? ->
                "[${Instant.now()}|$id] Executed!".also {
                    f.appendText("$it\n")
                    println(it)
                }
            }

        val scheduler = Scheduler.create(dataSource)
            .pollingInterval(Duration.ofMillis(250))
            .startTasks<RecurringTask<*>>(shortIntervalTask)
            .threads(15)
            .build()

        scheduler.start()

Example log (4 concurrent instances):

[2024-07-18T13:42:48.417646Z|5dbe8c42-7edb-4dee-91d8-f6a28ffc0334] Executed!
[2024-07-18T13:42:48.927126Z|5dbe8c42-7edb-4dee-91d8-f6a28ffc0334] Executed!
[2024-07-18T13:42:49.437913Z|5dbe8c42-7edb-4dee-91d8-f6a28ffc0334] Executed!
[2024-07-18T13:42:49.948168Z|5dbe8c42-7edb-4dee-91d8-f6a28ffc0334] Executed!
[2024-07-18T13:42:50.482727Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:50.990114Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:51.499913Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:52.008399Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:52.518156Z|c709b11d-f787-4bd8-8d12-fe2c07c00063] Executed!
[2024-07-18T13:42:53.025445Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:53.535962Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:54.048378Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:54.559758Z|a4a16633-4e6d-4612-b040-d9482e24f395] Executed!
[2024-07-18T13:42:55.174521Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:55.684807Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:56.193730Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:56.707297Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:57.216028Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:57.726570Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:58.237484Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:58.747589Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:59.255246Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:42:59.761781Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!
[2024-07-18T13:43:00.270595Z|852339ed-f0ba-4505-8869-6395bf3dd16e] Executed!