diff --git a/src/main/kotlin/net/moonleay/lilJudd/jobs/component/CronjobType.kt b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/CronjobType.kt new file mode 100644 index 0000000..fa34b60 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/CronjobType.kt @@ -0,0 +1,25 @@ +/* + * lilJudd + * Copyright (C) 2023 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.lilJudd.jobs.component + +enum class CronjobType { + INFINITE, + ONCE, + WHILE +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/jobs/component/ICronjob.kt b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/ICronjob.kt new file mode 100644 index 0000000..d3f6700 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/ICronjob.kt @@ -0,0 +1,47 @@ +/* + * lilJudd + * Copyright (C) 2023 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.lilJudd.jobs.component + +import dev.inmo.krontab.KronScheduler +import kotlinx.coroutines.Job + +interface ICronjob { + val jobName: String + + /* /--------------- Seconds + | /------------- Minutes + | | /----------- Hours + | | | /--------- Days of months + | | | | /------- Months + | | | | | /----- (optional) Year + | | | | | | /--- (optional) Timezone offset + | | | | | | | / (optional) Week days + * * * * * * 0o *w + */ + + val jobIncoming: String + val jobType: CronjobType + + val continueJob: Boolean + + var cronjobJob: Job + var scheduler: KronScheduler + suspend fun jobFunction() + +} diff --git a/src/main/kotlin/net/moonleay/lilJudd/jobs/component/JobManager.kt b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/JobManager.kt new file mode 100644 index 0000000..cc55256 --- /dev/null +++ b/src/main/kotlin/net/moonleay/lilJudd/jobs/component/JobManager.kt @@ -0,0 +1,96 @@ +/* + * lilJudd + * Copyright (C) 2023 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.moonleay.lilJudd.jobs.component + +import dev.inmo.krontab.buildSchedule +import dev.inmo.krontab.doInfinityTz +import dev.inmo.krontab.doOnceTz +import dev.inmo.krontab.doWhileTz +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import net.moonleay.lilJudd.util.Logger +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter + +object JobManager { + private val jobs: MutableList = mutableListOf() + + // Add a cronjob and register it + fun addJob(job: ICronjob) { + if (jobs.contains(job)) { + killJob(job) + } + registerJob(job) + jobs.add(job) + Logger.out("Registered job \"${job.jobName}\" of type \"${job.javaClass.name}\".") + } + + // Register a cronjob + private fun registerJob(job: ICronjob) { + Logger.out( + "INFO: Registering job \"${job.jobName}\"of type ${job.javaClass.name} at ${ + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + }" + ) + job.cronjobJob = CoroutineScope(Dispatchers.Default).launch { + job.scheduler = buildSchedule(job.jobIncoming) + Logger.out("Registered job \"${job.jobName}\"of type ${job.javaClass.name} to run at timer \"${job.jobIncoming}\".") + when (job.jobType) { + // Runs the job once at the specified time + CronjobType.ONCE -> { + job.scheduler.doOnceTz { + job.jobFunction() + } + } + // Runs the job whiles the variable is set to true + CronjobType.WHILE -> { + job.scheduler.doWhileTz { + job.jobFunction() + job.continueJob + } + } + // Run this job until the programm stops + CronjobType.INFINITE -> { + job.scheduler.doInfinityTz { + job.jobFunction() + } + } + } + } + } + + // Kill all cronjobs + fun killAllJobs() { + for (j in jobs) { + killJob(j) + } + } + + // Kill a cronjob + fun killJob(j: ICronjob) { + if (!jobs.contains(j)) { + Logger.out("This job does not exist.") + return + } + Logger.out("Killing job \"${j.jobName}\" of type ${j.javaClass.name}.") + j.cronjobJob.cancel() + jobs.remove(j) + } +}