forked from DiscordBots/lilJudd
feat: added cronjob system
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
parent
187da815e2
commit
ef28b08214
3 changed files with 168 additions and 0 deletions
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.lilJudd.jobs.component
|
||||
|
||||
enum class CronjobType {
|
||||
INFINITE,
|
||||
ONCE,
|
||||
WHILE
|
||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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()
|
||||
|
||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<ICronjob> = 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue