diff --git a/daily_notifier.nim b/daily_notifier.nim index 6f4b728..05b6688 100644 --- a/daily_notifier.nim +++ b/daily_notifier.nim @@ -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 +import strutils except toUpper +from unicode import toUpper type ParseState = enum BeforeHeading, ReadingPlans, AfterPlans @@ -82,23 +84,23 @@ proc loadConfig(s: cint): void {. exportc, noconv .} = try: jsonCfg = parseFile(rcFilename) except: jsonCfg = newJObject() - var cfg = CombinedConfig(docopt: args, json: jsonCfg) + var ccfg = CombinedConfig(docopt: args, json: jsonCfg) cfg = ( - cfg.getVal("plans-directory", "plans"), - cfg.getVal("date-format", "yyyy-MM-dd"), - cfg.getVal("pidfile", "/tmp/daily-plans.pid"), - cfg.getVal("logfile", "/tmp/daily-plans.log"), - cfg.getVal("errfile", "/tmp/daily-plans.error.log"), - parseInt(cfg.getVal("notification-seconds", "600")) + ccfg.getVal("plans-directory", "plans"), + ccfg.getVal("date-format", "yyyy-MM-dd"), + ccfg.getVal("pidfile", "/tmp/daily-plans.pid"), + ccfg.getVal("logfile", "/tmp/daily-plans.log"), + ccfg.getVal("errfile", "/tmp/daily-plans.error.log"), + parseInt(ccfg.getVal("notification-seconds", "600")) ) - if not existsDir(result.planDir): - quit("daily_notifier: plan directory does not exist: '" & result.planDir & "'", 1) + if not existsDir(cfg.planDir): + quit("daily_notifier: plan directory does not exist: '" & cfg.planDir & "'", 1) proc mainLoop(args: Table[string, Value]): void = - loadConfig(args) + loadConfig(0) signal(SIGHUP, loadConfig) var curDay: TimeInfo = getLocalTime(fromSeconds(0)) var todaysItems: seq[PlanItem] = @[] @@ -164,11 +166,20 @@ Options: echo doc quit() + loadConfig(0) + if args["start"]: # Start our daemon process (if needed) daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile): 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)