feat: make command parameter optional in set_alarm mutation

This commit is contained in:
Markus Graf
2026-05-11 15:05:29 +02:00
parent 5b5b845e41
commit ff79224560
10 changed files with 152 additions and 98 deletions
+20 -11
View File
@@ -1,6 +1,7 @@
import uuid
from crontab import CronTab
class CrontabManager:
COMMENT_PREFIX = "wecker-alarm:"
@@ -21,29 +22,37 @@ class CrontabManager:
if job.comment.startswith(self.COMMENT_PREFIX):
alarm_id = job.comment.split(self.COMMENT_PREFIX)[1].strip()
# job.slices is a valid cron slice object, str(job.slices) gives the expression
alarms.append({
"id": alarm_id,
"cron_expression": str(job.slices),
"command": job.command,
"is_enabled": job.is_enabled()
})
alarms.append(
{
"id": alarm_id,
"cron_expression": str(job.slices),
"command": job.command,
"is_enabled": job.is_enabled(),
}
)
return alarms
def set_alarm(self, alarm_id: str | None, cron_expression: str, command: str, is_enabled: bool = True):
def set_alarm(
self,
alarm_id: str | None,
cron_expression: str,
command: str,
is_enabled: bool = True,
):
if not alarm_id:
alarm_id = str(uuid.uuid4())
cron = self._get_cron()
comment = f"{self.COMMENT_PREFIX}{alarm_id}"
# Remove existing if any
cron.remove_all(comment=comment)
# Create new
job = cron.new(command=command, comment=comment)
job.setall(cron_expression)
job.enable(is_enabled)
cron.write()
return alarm_id