|
| 1 | +''' |
| 2 | + CrontabService.py |
| 3 | + Written By Kyle Chen |
| 4 | + Version 20170628v1 |
| 5 | +''' |
| 6 | + |
| 7 | +##import buildin pkgs |
| 8 | +import sys |
| 9 | +import re |
| 10 | +import os |
| 11 | +import time |
| 12 | +import logging |
| 13 | +from logging.handlers import RotatingFileHandler |
| 14 | + |
| 15 | +##get workpath |
| 16 | +workpath=""; |
| 17 | +pathlst=re.split(r"\/", sys.path[0]); |
| 18 | +max_index=len(pathlst)-1; |
| 19 | +i=0; |
| 20 | + |
| 21 | +while i < max_index-1: |
| 22 | + workpath+=pathlst[i] + "/"; |
| 23 | + i+=1; |
| 24 | + |
| 25 | +workpath+=pathlst[i] |
| 26 | + |
| 27 | +##append workpath to path |
| 28 | +sys.path.append("%s/lib" % ( workpath )) |
| 29 | + |
| 30 | +##import priviate pkgs |
| 31 | +from Config import Config |
| 32 | +from Crontab import Crontab |
| 33 | +from Lock import Lock |
| 34 | + |
| 35 | +##CrontabService Class |
| 36 | +class CrontabService: |
| 37 | + |
| 38 | + ##initial function |
| 39 | + def __init__(self): |
| 40 | + |
| 41 | + ##set priviate values |
| 42 | + self.config = Config(workpath) |
| 43 | + self.pid = os.getpid() |
| 44 | + self.pname = 'CrontabService.py' |
| 45 | + |
| 46 | + ##logger initial |
| 47 | + #self.logger_init(self.config.LOG_LEVEL, self.config.LOG_MAX_SIZE, self.config.LOG_BACKUP_COUNT, self.config.LOG_FILE) |
| 48 | + self.logger_init() |
| 49 | + |
| 50 | + ##lock initial |
| 51 | + self.lockObj = Lock(self.pname, self.pid, self.config.LOCK_DIR, self.config.LOCK_FILE, self.logger) |
| 52 | + |
| 53 | + ##debug output |
| 54 | + self.logger.debug('Crontab Initial') |
| 55 | + self.logger.debug('[SERVICE_INTERVAL][%s]' % (self.config.SERVICE_INTERVAL)) |
| 56 | + self.logger.debug('[CRONTAB_CFG_DIR][%s]' % (self.config.CRONTAB_CFG_DIR)) |
| 57 | + self.logger.debug('[CRONTAB_CFG_FILE][%s]' % (self.config.CRONTAB_CFG_FILE)) |
| 58 | + self.logger.debug('[MAX_THREADS][%s]' % (self.config.MAX_THREADS)) |
| 59 | + self.logger.debug('[THREAD_TIMEOUT][%s]' % (self.config.THREAD_TIMEOUT)) |
| 60 | + self.logger.debug('[LOCK_DIR][%s]' % (self.config.LOCK_DIR)) |
| 61 | + self.logger.debug('[LOCK_FILE][%s]' % (self.config.LOCK_FILE)) |
| 62 | + self.logger.debug('[LOG_DIR][%s]' % (self.config.LOG_DIR)) |
| 63 | + self.logger.debug('[LOG_FILE][%s]' % (self.config.LOG_FILE)) |
| 64 | + self.logger.debug('[LOG_LEVEL][%s]' % (self.config.LOG_LEVEL)) |
| 65 | + self.logger.debug('[LOG_MAX_SIZE][%s]' %(self.config.LOG_MAX_SIZE)) |
| 66 | + self.logger.debug('[LOG_BACKUP_COUNT][%s]' %(self.config.LOG_BACKUP_COUNT)) |
| 67 | + |
| 68 | + ##crontab initial |
| 69 | + #self.crontabObj = Crontab(self.config.CRONTAB_CFG_FILE, self.logger, self.config.MAX_THREADS, self.config.THREAD_TIMEOUT) |
| 70 | + |
| 71 | + return(None) |
| 72 | + |
| 73 | + ##initial logger |
| 74 | + def logger_init(self): |
| 75 | + |
| 76 | + self.logger = logging.getLogger("SACheck") |
| 77 | + |
| 78 | + try: |
| 79 | + log_level = getattr(logging, self.config.LOG_LEVEL) |
| 80 | + except: |
| 81 | + log_level = logging.NOTSET |
| 82 | + |
| 83 | + self.logger.setLevel(log_level) |
| 84 | + |
| 85 | + fh = RotatingFileHandler(self.config.LOG_FILE, mode='a', maxBytes=self.config.LOG_MAX_SIZE, backupCount=self.config.LOG_BACKUP_COUNT) |
| 86 | + fh.setLevel(log_level) |
| 87 | + |
| 88 | + ch = logging.StreamHandler() |
| 89 | + ch.setLevel(log_level) |
| 90 | + |
| 91 | + formatter = logging.Formatter('[%(asctime)s][%(name)s][%(levelname)s] %(message)s') |
| 92 | + fh.setFormatter(formatter) |
| 93 | + ch.setFormatter(formatter) |
| 94 | + |
| 95 | + self.logger.addHandler(fh) |
| 96 | + self.logger.addHandler(ch) |
| 97 | + |
| 98 | + return(True) |
| 99 | + |
| 100 | + ##run crontab function |
| 101 | + def run(self): |
| 102 | + |
| 103 | + while True: |
| 104 | + |
| 105 | + ##crontab initial |
| 106 | + self.crontabObj = Crontab(self.config.CRONTAB_CFG_FILE, self.logger, self.config.MAX_THREADS, self.config.THREAD_TIMEOUT, self.config.SUBPROC_LIMITS, self.config.MAX_RETRY, self.config.THREAD_DELAY) |
| 107 | + self.crontabObj.run() |
| 108 | + time.sleep(self.config.SERVICE_INTERVAL) |
| 109 | + |
| 110 | + return(True) |
| 111 | + |
| 112 | + ##destructor function |
| 113 | + def __del__(self): |
| 114 | + |
| 115 | + ##lock release |
| 116 | + try: |
| 117 | + self.lockObj.lock_release(self.config.LOCK_FILE) |
| 118 | + except Exception, e: |
| 119 | + pass |
| 120 | + |
| 121 | + return(None) |
| 122 | + |
| 123 | +##New CrontabServiceObj |
| 124 | +crontabServiceObj = CrontabService() |
| 125 | +crontabServiceObj.run() |
0 commit comments