Finished initial daily_notifier implementation.

This commit is contained in:
Jonathan Bernard 2016-11-02 22:15:37 -05:00
parent a48d25200e
commit 2748a3e8ce

View File

@ -1,5 +1,7 @@
import daemonize, docopt, json, os, nre, sequtils, strutils, times, timeutils import daemonize, docopt, json, os, nre, sequtils, times, timeutils
from posix import SIGTERM, SIGHUP, signal, kill from posix import SIGTERM, SIGHUP, signal, kill
import strutils except toUpper
from unicode import toUpper
type type
ParseState = enum BeforeHeading, ReadingPlans, AfterPlans ParseState = enum BeforeHeading, ReadingPlans, AfterPlans
@ -82,23 +84,23 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
try: jsonCfg = parseFile(rcFilename) try: jsonCfg = parseFile(rcFilename)
except: jsonCfg = newJObject() except: jsonCfg = newJObject()
var cfg = CombinedConfig(docopt: args, json: jsonCfg) var ccfg = CombinedConfig(docopt: args, json: jsonCfg)
cfg = ( cfg = (
cfg.getVal("plans-directory", "plans"), ccfg.getVal("plans-directory", "plans"),
cfg.getVal("date-format", "yyyy-MM-dd"), ccfg.getVal("date-format", "yyyy-MM-dd"),
cfg.getVal("pidfile", "/tmp/daily-plans.pid"), ccfg.getVal("pidfile", "/tmp/daily-plans.pid"),
cfg.getVal("logfile", "/tmp/daily-plans.log"), ccfg.getVal("logfile", "/tmp/daily-plans.log"),
cfg.getVal("errfile", "/tmp/daily-plans.error.log"), ccfg.getVal("errfile", "/tmp/daily-plans.error.log"),
parseInt(cfg.getVal("notification-seconds", "600")) parseInt(ccfg.getVal("notification-seconds", "600"))
) )
if not existsDir(result.planDir): if not existsDir(cfg.planDir):
quit("daily_notifier: plan directory does not exist: '" & result.planDir & "'", 1) quit("daily_notifier: plan directory does not exist: '" & cfg.planDir & "'", 1)
proc mainLoop(args: Table[string, Value]): void = proc mainLoop(args: Table[string, Value]): void =
loadConfig(args) loadConfig(0)
signal(SIGHUP, loadConfig) signal(SIGHUP, loadConfig)
var curDay: TimeInfo = getLocalTime(fromSeconds(0)) var curDay: TimeInfo = getLocalTime(fromSeconds(0))
var todaysItems: seq[PlanItem] = @[] var todaysItems: seq[PlanItem] = @[]
@ -164,11 +166,20 @@ Options:
echo doc echo doc
quit() quit()
loadConfig(0)
if args["start"]: if args["start"]:
# Start our daemon process (if needed) # Start our daemon process (if needed)
daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile): daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile):
mainLoop(args) mainLoop(args)
#elif args["stop"]:
#loadConfig()
#kill( elif args["stop"] or args["reconfigure"]:
if not fileExists(cfg.pidfile):
echo "daily_notifier is not running"
quit(QuitSuccess)
let pid = parseInt(readFile(cfg.pidfile).strip)
if args["stop"]: discard kill(pid, SIGTERM)
else: discard kill(pid, SIGHUP)