EN · DE · RU · FR · ES

#1019: SystemSanityCheckJob.kt

projectforge-business/src/main/kotlin/org/projectforge/business/jobs/SystemSanityCheckJob.kt Type: Kotlin · Role: Component · Source: projectforge-business/src/main/kotlin/org/projectforge/business/jobs/SystemSanityCheckJob.kt 82 lines · 55 code · 23 comments · 4 blank
Scheduled/system job for SystemSanityCheck. Implements periodic maintenance or sanity-check logic executed by the PF job scheduler.

Code Structure

Classes: SystemSanityCheckJob

Supertype(s): AbstractJob

Functions (2): executeJob, asString

Properties (7): taskDao, tasks, taskMap, rootTask, abandonedTasks, ancestor, rootTaskFound

Imports: 5 packages

Package: org.projectforge.business.jobs

Source Code (abridged)

package org.projectforge.business.jobs

import org.projectforge.business.task.TaskDO
import org.projectforge.business.task.TaskDao
import org.projectforge.business.task.TaskNode
import org.projectforge.jobs.AbstractJob
import org.projectforge.jobs.JobExecutionContext

class SystemSanityCheckJob(val taskDao: TaskDao) : AbstractJob("System integrity check") {
    override fun executeJob() {
        jobExecutionContext.addMessage("Task integrity (abandoned tasks)")
        val tasks: List<TaskDO> = taskDao.selectAll(false)
        jobExecutionContext.addMessage("Found ${tasks.size} tasks.")
        val taskMap: MutableMap<Long?, TaskDO> = HashMap()
        for (task in tasks) {
            taskMap[task.id] = task
        }
        var rootTask = false
        var abandonedTasks = false
        for (task in tasks) {
            if (task.parentTask == null) {
                if (rootTask) {
                    jobExecutionContext.addError("Found another root task: ${asString(task)}")
                } else {
                    jobExecutionContext.addMessage("Found root task: ${asString(task)}")
                    rootTask = true
                }
            } else {
                var ancestor = taskMap[task.parentTaskId]
                var rootTaskFound = false
                for (i in 0..49) { // Max. depth of 50, otherwise cyclic task!
                    if (ancestor == null) {
                        break
                    }
                    if (ancestor.parentTaskId == null) {
                        // Root task found, OK.
                        rootTaskFound = true
                        break
                    }
                    ancestor = taskMap[ancestor.parentTaskId]
                }
                if (!rootTaskFound) {
                    jobExecutionContext.addError("Found abandoned task (cyclic tasks without path to root): ${asString(task)}")
                    abandonedTasks = true
                }
            }
            taskMap[task.id] = task
        }
        if (!abandonedTasks) {
            jobExecutionContext.addMessage("Test OK, no abandoned tasks detected.")
        } else {
            jobExecutionContext.addError("Test FAILED, abandoned tasks detected.")
        }
    }

    private fun asString(task: TaskDO): String {
        return "TaskNode[id=[${task.id}], created=[${task.created}] title=[${task.title}]]"
    }
}

Git History

868d6abb7 2025 -> 2026
034eebd5d Html handling and job handling.
63081666f Source file headers: 2024-> 2025.
619985f48 Release 8.1 snapshot (#247)