Skip to content

Latest commit

 

History

History

ktor-server-task-scheduling

Task Scheduler Plugin for Ktor Server

koverage


Manage scheduled tasks across instances of your distributed ktor server, using various strategies and a kotlin flavoured cron tab


Features:

  • Various Implementations: Can use Redis( JVM/Native), JDBC (JVM) or MongoDB (JVM) for lock management, or add your own implementation by extending Core

  • Multiple managers: Define multiple tasks and assign each to a manager of your choice

  • Kron Schedule builder: Utilizes krontab for building schedules using a convenient kotlin DSL

How to Use:

  • Add a dependency for your chosen task managers or just add core and implement yourself:
    implementation("io.github.flaxoos:ktor-server-task-scheduling-${redis/jdbc/mongodb/core}:$ktor_plugins_version")
  • Install the plugin and define one or more task managers:
install(TaskScheduling){
    redis{ //<-- this will be the default manager
        ...
    }
    jdbc("my jdbc manager"){
        ...
    }
}
  • Configure some tasks and assign them to the managers
install(TaskScheduling) {
    ...
    task { // if no taskManagerName is provided, the task would be assigned to the default manager
        name = "My task"
        task = { taskExecutionTime ->
            log.info("My task is running: $taskExecutionTime")
        }
        kronSchedule = {
            hours {
                from 0 every 12
            }
            minutes {
                from 15 every 30
            }
        }
        concurrency = 2
    }
    
    task(taskManagerName = "my jdbc manager") {
        name = "My Jdbc task"
        ...
    }
}

Important Notes:

  • Ensure you have distinct names for task and task manager.