18 from typing
import List
20 from ...utils
import command
21 from ...configuration
import CONFIG_DIR
24 def getExisting() -> List[str]:
25 _, output, error = command([
"crontab",
"-l"], ignoreStdout =
True, ignoreStderr =
True, check =
False)
26 if error
is not None and "no crontab for" in error:
28 if output
is not None:
29 return [line.strip()
for line
in output.split(
"\n")
if line.strip()]
31 raise ValueError(
"\"crontab\" is not installed. To enable automatic updates please install \"crontab\"")
34 def jobExists(script: str) -> bool:
35 existingLines = getExisting()
36 return any(script
in line
for line
in existingLines)
39 def scheduleJob(scriptName: str) ->
None:
40 existingLines = getExisting()
41 cronJob = f
"*/30 * * * * {CONFIG_DIR / scriptName} >> {CONFIG_DIR}/logs/ctx_autoupdate.log 2>&1\n"
42 existingLines.append(cronJob)
44 tempCronFilePath = CONFIG_DIR /
"temp.cron"
45 with tempCronFilePath.open(
"w")
as tempCronFile:
46 tempCronFile.write(
"\n".join(existingLines))
48 command([
"crontab", str(tempCronFilePath)])
49 tempCronFilePath.unlink()