wisp-task¶
This module contains code to assist with running tasks.
RepeatedTask runs a repeated task at the user controlled rate based on the kotlin-retry library. For convenience a RepeatedTaskManager is available to help manage the RepeatedTasks.
It’s possible to specify your own retry policy for the task. See kotlin-retry library for more details and examples. The default retry policy instructions are to retry on all exceptions with a Binary Exponential backoff delay - which itself is configured from the supplied RepeatedTaskConfig .
Usage¶
Create a repeated task and start it running.
val repeatedTaskConfig = RepeatedTaskConfig(
timeBetweenRunsMs = 10000L // 10 sec delay between task runs or retries
)
val manager = RepeatedTaskManager()
val newTask = manager.createTask(
name = "taskName",
repeatedTaskConfig = repeatedTaskConfig,
) { name: String, taskConfig: TaskConfig ->
// do task stuff
// ...
// if the task completes correctly, return OK.
Status.OK
}
newTask.startUp()
You can shutdown a task or the manager which will shutdown all tasks. Using the above code:
// shut down the repeated task.
newTask.shutDown()
// shut down all repeated tasks known by the manager.
manager.shutDown()
The task to be run takes a TaskConfig (which could be loaded using wisp-config).
class MyTaskConfig(
val foo: String,
val allResults: MutableList<String> = mutableListOf()
): TaskConfig()
val taskName = "myTask"
val anotherTask = manager.createTask(
name = taskName,
repeatedTaskConfig = repeatedTaskConfig,
taskConfig = MyTaskConfig("fooString")
) { name: String, taskConfig: TaskConfig ->
val config = taskConfig as MyTaskConfig
// access the taskConfig
println("Foo is ${config.foo}")
// do task stuff
// ...
config.allResults.add("another result")
// if the task completes correctly, return OK.
Status.OK
}