ddd refactoring ScheduledJobs.kt

This commit is contained in:
ansgarz 2024-12-20 13:02:26 +01:00
parent 62fa879fce
commit d50f61a4d6
3 changed files with 30 additions and 19 deletions

View file

@ -1,9 +1,7 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.domain
import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure.createCronJob import org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure.createCronJob
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
/** /**
@ -11,14 +9,6 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.check
* ATTENTION: Use with care!! System will be shut down, restart might not succeed in all cases. * ATTENTION: Use with care!! System will be shut down, restart might not succeed in all cases.
*/ */
fun Prov.scheduleMonthlyReboot() = task { fun Prov.scheduleMonthlyReboot() = task {
// use controlled "shutdown" instead of direct "reboot" // reboot each first Tuesday in a month at 3:00
val shutdown = "/sbin/shutdown" createCronJob("50_monthly_reboot", "0 2 1-7 * 2", "/sbin/shutdown", "-r now", "root")
// ensure shutdown command exists
if (checkFile(shutdown, sudo = true)) {
// reboot each first Tuesday in a month at 3:00
createCronJob("50_monthly_reboot", "0 2 1-7 * 2", "$shutdown -r now", "root")
} else {
addResultToEval(ProvResult(false, err = "$shutdown not found."))
}
} }

View file

@ -1,6 +1,8 @@
package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure package org.domaindrivenarchitecture.provs.framework.ubuntu.scheduledjobs.infrastructure
import org.domaindrivenarchitecture.provs.framework.core.Prov import org.domaindrivenarchitecture.provs.framework.core.Prov
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.checkFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createDirs
import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile import org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base.createFile
import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami
@ -13,10 +15,15 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.user.base.whoami
* @param command the executed command * @param command the executed command
* @param user the user with whom the command will be executed, if null the current user is used * @param user the user with whom the command will be executed, if null the current user is used
*/ */
fun Prov.createCronJob(cronFilename: String, schedule: String, command: String, user: String? = null) = task { fun Prov.createCronJob(cronFilename: String, schedule: String, command: String, parameter: String, user: String? = null) = task {
val cronUser = user ?: whoami() val cronUser = user ?: whoami()
val cronLine = "$schedule $cronUser $command\n"
createDirs("/etc/cron.d/", sudo = true) // ensure command exists
createFile("/etc/cron.d/$cronFilename", cronLine, "644", sudo = true, overwriteIfExisting = true) if (checkFile(command, sudo = true)) {
val cronLine = "$schedule $cronUser $command $parameter\n"
createDirs("/etc/cron.d/", sudo = true)
createFile("/etc/cron.d/$cronFilename", cronLine, "644", sudo = true, overwriteIfExisting = true)
} else {
addResultToEval(ProvResult(false, err = "$command not found."))
}
} }

View file

@ -19,7 +19,7 @@ class CronJobsKtTest {
val cronFilename = "50_test_cron" val cronFilename = "50_test_cron"
// when // when
val result = prov.createCronJob(cronFilename, "0 * * * *", "echo hello > /dev/null 2>&1") val result = prov.createCronJob(cronFilename, "0 * * * *", "/usr/bin/echo", "hello > /dev/null 2>&1")
// then // then
assertTrue(result.success) assertTrue(result.success)
@ -27,7 +27,21 @@ class CronJobsKtTest {
assertTrue(prov.checkFile(fqFilename), "") assertTrue(prov.checkFile(fqFilename), "")
val actualFileContent = prov.fileContent(fqFilename, sudo = true) val actualFileContent = prov.fileContent(fqFilename, sudo = true)
val expectedUser = prov.whoami() val expectedUser = prov.whoami()
assertEquals("0 * * * * $expectedUser echo hello > /dev/null 2>&1\n", actualFileContent) assertEquals("0 * * * * $expectedUser /usr/bin/echo hello > /dev/null 2>&1\n", actualFileContent)
}
@ContainerTest
fun createCronJob_is_not_created_due_to_command_not_found() {
// given
val prov = defaultTestContainer()
val cronFilename = "88_test_cron"
// when
val result = prov.createCronJob(cronFilename, "0 * * * *", "/usr/bin/dontexist", "")
// then
assertTrue(!result.success)
assertTrue(!prov.checkFile(cronFilename), "File should not exist: $cronFilename")
} }
@ -56,7 +70,7 @@ class CronJobsKtTest {
val result = prov.createCronJob( val result = prov.createCronJob(
cronFilename, cronFilename,
"*/1 * * * *", "*/1 * * * *",
"echo \"xxx\" > /home/$user/tmp/\$(/usr/bin/date +\\%Y_\\%m_\\%d-\\%H_\\%M)" "/usr/bin/echo", "\"xxx\" > /home/$user/tmp/\$(/usr/bin/date +\\%Y_\\%m_\\%d-\\%H_\\%M)"
) )
// then // then