#1208: JobHandler.kt

projectforge-business/src/main/kotlin/org/projectforge/framework/jobs/JobHandler.kt Spring Service, projectforge-business/src/main/kotlin/org/projectforge/framework/jobs/JobHandler.kt 290 lines · 228 code · 40 comments · 22 blank
Central job scheduler and lifecycle manager for AbstractJob. Handles job registration, queue strategy evaluation, coroutine-based execution, timeout enforcement, cleanup of terminated jobs, and health-check monitoring via email alerts. Implements ShutdownListener to cancel running jobs gracefully on application shutdown.

Architecture

Class Overview

Job Lifecycle Management

MethodDescription
addJob(job)Registers job, spawns a coroutine via runBlocking { launch { ... } } that waits for queue clearance then calls job.start()
tidyUp()Called every minute by JobHandlerScheduler; cancels timed-out jobs, removes old terminated jobs
cancelJob(job)Cancels job if user has write access; calls job.cancel()
shutdown()Cancels and joins all running jobs on application shutdown

Queue Strategy Evaluation

The addJob method spawns a coroutine that:

  1. Iterates up to 10,000 times (paranoia counter, max 10,000 seconds wait)
  2. Checks all existing jobs for blocking via other.isBlocking(job)
  3. If blocked with REFUSE strategy, marks job as REFUSED (errorCode: REFUSED_BY_ANOTHER_RUNNING_JOB)
  4. If blocked with PER_QUEUE strategy, sets status to WAITING and delays 1000ms
  5. If no blocks found, sets status to RUNNING and starts execution
  6. If loop exhausts, marks job as FAILED (errorCode: TIMEOUT_WHILE_WAITING)

Health Monitoring

Coroutine Context Propagation

When launching the job coroutine, the following contexts are propagated:

Cleanup Constants

Git History

868d6abb7 2025 -> 2026
00ec94e68 New version of fr-opensagres-xdocreport-poi-xwpf-converter-pdf, DataTransferBridge, ThreadLocalUserContext for coroutines.
63081666f Source file headers: 2024-> 2025.
5f9fbbbd3 Fix typos in projectforge-business directory
f8961fd66 Shutdown handling improved: ShutdownService shuts down some services before database.
67805f2fc ThreadLocalUserContext.user -> ThreadLocalUserContext.loggedInUser
77bade6df javax.* -> jakarta.*
b6092df09 Copyright 2023 -> 2024
a060d94b0 JobHandler was not scheduled. Fixed.
bdcc7ed73 Report failure of Spring scheduler as mail (to be tested).
ab45d51fa Copyright 2001-2022 -> 2001-2023.
163492453 WIP: Jobs and Bank account records.
9f6ae7a0f WIP: Jobs.
232a91c5a BaseUserGroupRightUtils implemented. WIP BankingPlugin and jobs-handling.
7bc51ed17 WIP: Jobs.
38bec971a ThreadLocal -> Kotlin
f99714d0a Wrong log import fixed.
fcc4c67f6 WIP: Jobmonitor.
d93f61491 WIP: Banking plugin (and new import module).
cd69cc40f WIP: Banking plugin (and new import module).
67eed70e4 WIP: Banking plugin (and new import module). JobHandler/monitor started.