class MeetingReminder(object): _logger = Logger.instance() def __init__(self, queue): self.userQueue = queue self._lock = threading.Lock() def job(self): if isWorkDay(getCurrentDate()): with self._lock: user = self.userQueue.deQueue() self.sendMessage(user) self.userQueue.enQueue(user) else: self._logger.info(getCurrentDate() + '是假日!') pass def sendMessage(self, user): try: _r2 = '--------order:%s---%s(%s)负责今天(%s)早会------------' % ( str(user.order), user.name, user.account, DateUtil.getCurrentDate()) self._logger.info(_r2) eReceivers = [] tReceivers = [] if user: eReceivers.append(user.email) tReceivers.append(user.tel) try: content = user.name + '(' + user.account + ')' + '负责今天(' + getCurrentDate( ) + ')早会' EmailUtil.sendEmail(eReceivers, u'主持早会', content.encode("utf-8")) except Exception as e: self._logger.error("邮件失败," + str(e)) pass try: msg = "【主持早会】" + user.name + '(' + user.account + ')' + '负责今天(' + getCurrentDate( ) + ')早会' SMSUtil.sendSMS(tReceivers, msg.encode("utf-8")) except Exception as e: self._logger.error("短信失败," + str(e)) pass except Exception as e: print(e) def setSchdeule(self, job): # schedule.every().day.at("7:00").do(job) schedule.every(3).seconds.do(job) def scheduleCheck(self): schedule.run_pending()
class MISReminder(): _logger = Logger.instance() def __init__(self, queue): self.userQueue = queue self._lock = threading.Lock() def job(self): # if isWorkDay(getCurrentDate()): # with self._lock: # user = self.userQueue.getHead(); # self.sendMessage(user); # else: # today = datetime.now().weekday(); # if today == 6: # with self._lock: # user = self.userQueue.deQueue(); # self.userQueue.enQueue(user); # self._logger.info(getCurrentDate() + '是假日!'); if isWorkDay(getCurrentDate()): with self._lock: user = self.userQueue.deQueue() self.sendMessage(user) self.userQueue.enQueue(user) else: self._logger.info(getCurrentDate() + '是假日!') pass def sendMessage(self, user): _r = '--------order:%s---%s(%s)负责巡检------------' % (str(user.order), user.name, user.account,) self._logger.info(_r) eReceivers = []; tReceivers = []; if user: _r2 = '--------email:%s---tel:%s------------' % (str(user.email), user.tel) self._logger.info(_r2) eReceivers.append(user.email); eReceivers.append('*****@*****.**') tReceivers.append(user.tel); try: content = user.name + '(' + user.account + ')' + '负责巡检,访问地址:<a href="http://union.vip.58.com/bsp/index">http://union.vip.58.com/bsp/index</a>,并查看《HBG业绩加和校验结果通知》邮件'; content += '<br/>' content += email_content EmailUtil.sendEmail(eReceivers, '巡检轮班', content.encode("utf-8")) except Exception as e: self._logger.error("邮件失败," + str(e)) try: msg = "【巡检轮班】" + user.name + '(' + user.account + ')' + '负责巡检,并查看《HBG业绩加和校验结果通知》邮件' SMSUtil.sendSMS(tReceivers, msg.encode("utf-8")) except Exception as e: self._logger.error("短信失败," + str(e)) pass def setSchdeule(self, job): schedule.every().day.at("9:30").do(job) # schedule.every().day.at("14:00").do(job) # schedule.every(5).seconds.do(job) def scheduleCheck(self): schedule.run_pending();
class Manager(object): __meetingReminderThread = None __meetingReminderFlag = True __misReminderThread = None __misReminderFlag = True __userdb = SqliteUserDB() _logger = Logger.instance() _meetingUserQueue = None def startMeetingReminder(self, account): _firstUser = self.__userdb.query_meetinguser_by_account(account)[0] if _firstUser is None: return False, u"用户%s不存在!" % account if _firstUser.isValid == 0: return False, u"用户%s不可用!" % account _meetingUsers = self.__userdb.get_meetinguser_all_valid() _meetingUsers = self.__rSortUsers(_firstUser, _meetingUsers) self._meetingUserQueue = self.__getUserQueue(_meetingUsers) if self.__meetingReminderThread is None or self.__meetingReminderThread.is_alive( ) == False: self.__meetingReminderThread = None self.__meetingReminderThread = threading.Thread( name="meeting-thread", target=self._startMeetingReminderServer, args=(self._meetingUserQueue, )) self.__meetingReminderThread.start() return True, u"Meeting Reminder Start" else: return False, u"Meeting Reminder is running" def startMisReminder(self, account): _firstUser = self.__userdb.query_misuser_by_account(account)[0] if _firstUser is None: return False, u"用户%s不存在!" % account if _firstUser.isValid == 0: return False, u"用户%s不可用!" % account _misUsers = self.__userdb.get_misuser_all_valid() _misUsers = self.__rSortUsers(_firstUser, _misUsers) _misUserQueue = self.__getUserQueue(_misUsers) if self.__misReminderThread is None or self.__misReminderThread.is_alive( ) == False: self.__misReminderThread = None self.__misReminderThread = threading.Thread( name="mis-thread", target=self._startMisReminderServer, args=(_misUserQueue, )) self.__misReminderThread.start() return True, u"Mis Reminder Start" else: return False, u"Mis Reminder is running" def _startMeetingReminderServer(self, meetinUserQueue): self._logger.info("开始Meeting通知...") meetingReminder = MeetingReminder(meetinUserQueue) meetingReminder.setSchdeule(meetingReminder.job) self.__meetingReminderFlag = True while self.__meetingReminderFlag: meetingReminder.scheduleCheck() time.sleep(1) self._logger.info("Meeting通知结束...") return def _startMisReminderServer(self, misUserQueue): self._logger.info("开始Mis通知...") misReminder = MISReminder(misUserQueue) misReminder.setSchdeule(misReminder.job) self.__misReminderFlag = True while self.__misReminderFlag: misReminder.scheduleCheck() time.sleep(1) self._logger.info("Mis通知结束...") return def stopMeetingReminder(self): if self.__meetingReminderThread and self.__meetingReminderThread.is_alive( ): self._logger.info("停止Meeting通知...") self.__meetingReminderFlag = False return True, u"Meeting Reminder is stopped" else: return False, u"Meeting Reminder has stopped" def stopMisReminder(self): if self.__misReminderThread and self.__misReminderThread.is_alive(): self._logger.info("停止Mis通知...") self.__misReminderFlag = False return True, u"Mis Reminder is stopped" else: return False, u"Mis Reminder has stopped" def getAllMeetingUsers(self): return self.__userdb.get_meetinguser_all() def getAllMisUsers(self): return self.__userdb.get_misuser_all() def getAllActiveMeetingUsers(self): return self.__userdb.get_meetinguser_all_valid() def getAllActiveMisUsers(self): return self.__userdb.get_misuser_all_valid() def updateMeetingUsers(self, accout, active=True): print(accout, active, (1 if active == True else 0)) if accout: _user = self.__userdb.query_meetinguser_by_account(accout)[0] if _user: _user.isValid = (1 if active == True else 0) self.__userdb.update_meetinguser(_user) else: print("更新失败!") def updateMisUsers(self, accout, active=True): print(accout, active, (1 if active == True else 0)) if accout: _user = self.__userdb.query_misuser_by_account(accout)[0] if _user: _user.isValid = (1 if active == True else 0) self.__userdb.update_misuser(_user) else: print("更新失败!") @classmethod def __rSortUsers(self, firstOrderUser, users): firstOrder = firstOrderUser.order size = len(users) for user in users: _order = ((user.order - firstOrder) + size) % size user.order = _order + 1 users.sort(cmp=None, key=lambda x: x.order, reverse=False) return users @classmethod def __getUserQueue(self, users): _queue = Queue(len(users) + 1) for user in users: _queue.enQueue(user) return _queue
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Name: app Description : Author : guodongqing date: 2019/11/11 ------------------------------------------------- """ from flask import Flask, render_template, request, jsonify from manager.manager import Manager from util.Logger import Logger logger = Logger.instance() sysManager = Manager() app = Flask(__name__) def start_web_server(port=5500, debug=True): app.run(host='0.0.0.0', port=port, debug=debug) @app.route('/') def index(): return render_template('index.html') @app.route('/meeting') def meeting(): meetingUsers = sysManager.getAllMeetingUsers() logger.info(request.remote_addr + " - meeting user's size:" + str(len(meetingUsers)))
#encoding:utf-8 ''' Created on 2018年8月1日 @author: guodongqing ''' from util.Logger import Logger import web if __name__ == '__main__': Logger.instance().info("---------服务启动-----------") web.start_web_server()