Tasks

Jakon includes a built-in system for scheduling and running periodic background tasks. This system is built around the TaskRunner and allows for easy definition of custom tasks.

Defining a Task

Every task must inherit from the AbstractTask class. When defining a task, you specify the period and the time unit (TimeUnit) for its execution.

import cz.kamenitxan.jakon.core.task.AbstractTask
import java.util.concurrent.TimeUnit

class MyCustomTask extends AbstractTask(1, TimeUnit.HOURS) {
    override def start(): Unit = {
        // Task logic that will run every hour
        println("Task is running...")
    }
}

Registering a Task

For the TaskRunner to be aware of a task and manage its execution, the task must be registered. This is typically done in the taskSetup() method of your initialization class (inheriting from JakonInit).

override protected def taskSetup(): Unit = {
    super.taskSetup() // preserves default system tasks
    TaskRunner.registerTask(new MyCustomTask)
}

Once registered and the application starts, the TaskRunner automatically schedules the task according to the specified period.

Features and Management

  • Pausing Tasks: Tasks can be paused or resumed. The "paused" state is persisted in the database in the KeyValueEntity table with the key {TaskName}_disabled.
  • Admin Monitoring: Jakon provides an administration interface for task management where you can monitor:
    • Last run time.
    • Duration of the last execution.
    • Whether the last run was successful.
    • Next scheduled execution time.
    • Manual "Run Now" trigger.

Default System Tasks

Jakon includes several built-in tasks for system maintenance:

Task Default Period Description
RenderTask 10 minutes Handles static page generation.
EmailSendTask 1 minute Sends emails waiting in the queue (if email is enabled).
LogCleanerTask Varies Cleans up old log records.
FileManagerConsistencyTestTask 1 hour Verifies file consistency within the system.
ResetPasswordRequestCleanerTask 6 hours Removes expired password reset requests.

Manual Execution and Control

Beyond automatic scheduling, you can interact with tasks programmatically via the TaskRunner object:

  • TaskRunner.runSingle(task): Runs the task immediately (asynchronously).
  • TaskRunner.stop(task): Cancels the scheduled execution of the given task.
  • TaskRunner.schedule(task): Schedules periodic execution of the task (if not paused).