-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcronifyd
executable file
·81 lines (68 loc) · 2.28 KB
/
cronifyd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import os
import sys
# import signal
import yaml
import time
import logging
import logging.handlers
import syslog
import daemon
import daemon.runner
from cronify import cronify
CFG_FILE = "/etc/cronify.yaml"
PID_FILE = "/var/run/cronify.pid"
_LOG_DIR = "/var/log/cronify"
LOG_FILE = os.path.sep.join([_LOG_DIR, "cronify.log"])
try:
os.mkdir(_LOG_DIR)
except OSError, e:
if e.errno == 13:
sys.stderr.write("No permissions to create log dir %s\n" % (_LOG_DIR,))
sys.exit(1)
def start_watcher():
"""Read config file, start watcher and return Watcher object"""
cfg_fileh = open(CFG_FILE, 'r')
data = yaml.load(cfg_fileh)
cfg_fileh.close()
syslog.syslog("Cronify daemon starting..")
return cronify.Watcher(data)
def testy():
"""Small fake function to test daemon app with"""
while 1:
syslog.syslog('Running..')
time.sleep(5)
class CronifyDaemon(object):
"""Cronify daemon application class.
Used with daemon.runner.DaemonRunner"""
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/null'
self.stderr_path = '/dev/tty'
self.pidfile_path = PID_FILE
self.pidfile_timeout = 5
self.watcher = None
def __del__(self):
"""Cleanup sanely if watchers are started"""
if self.watcher:
self.watcher.cleanup()
def setup_logger(self):
"""Instantiate our loggers"""
loggers = [logging.getLogger('cronify.cronify'), logging.getLogger('cronify.threadpool')]
_handler = logging.handlers.TimedRotatingFileHandler(LOG_FILE, when = "midnight", interval = 1, backupCount = 7)
log_format = logging.Formatter('%(name)s - %(threadName)s - %(asctime)s - %(levelname)s - %(message)s')
_handler.setFormatter(log_format)
for logger in loggers:
logger.addHandler(_handler)
logger.setLevel(logging.INFO)
def run(self):
"""Startup our watcher and sleep forever (an hour at a time..)"""
# testy()
self.setup_logger()
self.watcher = start_watcher()
while 1:
time.sleep(3600)
cronifyd = CronifyDaemon()
if __name__ == "__main__":
daemon_runner = daemon.runner.DaemonRunner(cronifyd)
daemon_runner.do_action()