def sendMails(self): #发送邮件 logger.info("开始发送邮件:id=%s" % self.mail['id']) #time.sleep(1) #修改为正在发送状态 updateStateByIds(constants.MAIL_SENDING, ids=self.mail['id'], time=constants.TIME_SENDING) sm = SendMails(self.mail) result = sm.sendMail() if result == constants.MAIL_SUCCESS: #发送邮件成功,将本邮件发到成功表 updateStateByIds(state=constants.MAIL_SUCCESS, ids=self.mail['id'], time=constants.TIME_FINISH) logger.info("发送邮件成功,将本邮件发到成功表id=%s" % self.mail['id']) elif result == constants.MAIL_RETRY: #发送邮件失败,修改状态改为初始状态,发送次数count+1 logger.debug("发送邮件失败,修改发送次数count+1 id=%s" % self.mail['id']) self.sendRetry(self.mail) elif result == constants.MAIL_NETWORK_EXCEPT: #修改邮件为初始状态 updateStateByIds(state=constants.MAIL_INIT, ids=self.mail['id']) #将主进程睡眠 global event_network_except event_network_except.set() logger.debug("发送邮件失败,网络错误,睡眠 id=%s" % self.mail['id']) elif result == constants.MAIL_ATTACH_EXCEPT: #发送邮件失败,附件错误 updateStateByIds(state=constants.MAIL_ATTACH_EXCEPT, ids=self.mail['id'], time=constants.TIME_FINISH) logger.debug("发送邮件失败,附件路径错误, id=%s" % self.mail['id']) return result
def sendMails(self): #发送邮件 logger.info("开始发送邮件:id=%s" % self.mail['id']) #time.sleep(1) #修改为正在发送状态 updateStateByIds(constants.MAIL_SENDING, ids=self.mail['id'], time=constants.TIME_SENDING) sm = SendMails(self.mail) result = sm.sendMail() if result == constants.MAIL_SUCCESS: #发送邮件成功,将本邮件发到成功表 updateStateByIds(state = constants.MAIL_SUCCESS, ids=self.mail['id'], time=constants.TIME_FINISH) logger.info("发送邮件成功,将本邮件发到成功表id=%s" % self.mail['id']) elif result == constants.MAIL_RETRY: #发送邮件失败,修改状态改为初始状态,发送次数count+1 logger.debug("发送邮件失败,修改发送次数count+1 id=%s" % self.mail['id']) self.sendRetry(self.mail) elif result == constants.MAIL_NETWORK_EXCEPT: #修改邮件为初始状态 updateStateByIds(state = constants.MAIL_INIT, ids=self.mail['id']) #将主进程睡眠 global event_network_except event_network_except.set() logger.debug("发送邮件失败,网络错误,睡眠 id=%s" % self.mail['id']) elif result == constants.MAIL_ATTACH_EXCEPT: #发送邮件失败,附件错误 updateStateByIds(state = constants.MAIL_ATTACH_EXCEPT, ids=self.mail['id'], time=constants.TIME_FINISH) logger.debug("发送邮件失败,附件路径错误, id=%s" % self.mail['id']) return result
def send_mails(self, mails): """ Parameters: - mails """ try: for mailObject in mails: if not mailObject.sendto: logger.info("mailObject.sendto is empty!") continue smtp_host = getSmtpHost(mailObject.sendto) create_time = datetime.now() attach_files = json.dumps(mailObject.attach_files) dbconn().insertOne( "insert into mails (sendto, smtp_host, subject, content, attach_files, priority, create_time, send_count, state) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)", [ mailObject.sendto, smtp_host, mailObject.subject, mailObject.content, attach_files, mailObject.priority, create_time, 0, constants.MAIL_INIT ]) #设置事件 self.glocker.set() return True except Exception as e: print e return False
def startService(glocker): mailsHandler = MailsHandler(glocker) processor = MailsService.Processor(mailsHandler) transport = TSocket.TServerSocket(host=constants.MAILS_SERVER_HOST, port=constants.MAILS_PORT) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory, daemon=True) logger.info("mails server start") server.serve()
def send_mails(self, mails): """ Parameters: - mails """ try: for mailObject in mails: if not mailObject.sendto: logger.info("mailObject.sendto is empty!") continue smtp_host = getSmtpHost(mailObject.sendto) create_time = datetime.now() attach_files = json.dumps(mailObject.attach_files) dbconn().insertOne("insert into mails (sendto, smtp_host, subject, content, attach_files, priority, create_time, send_count, state) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)" , [mailObject.sendto, smtp_host, mailObject.subject, mailObject.content, attach_files, mailObject.priority, create_time, 0, constants.MAIL_INIT]) #设置事件 self.glocker.set() return True except Exception as e: print e return False
#coding=utf-8 ''' Created on Aug 20, 2015 @author: root ''' from mails.sendservice.SendMailsPool import SendMialsThread from mails.sendservice.DeleteMails import DeleteMials from mails.thriftservice import server from mails.utils import constants import threading from mails.utils.Logger import logger if __name__ == "__main__": logger.info("启动删除邮件线程") deleteMails = DeleteMials(constants.TIME_DELETE_PERIOD) deleteMails.start() #控制接收到邮件将进程池的睡眠去掉 global glocker glocker = threading.Event() logger.info("启动进程去创建进程池发送邮件") sendMialsThread = SendMialsThread(glocker) sendMialsThread.start() logger.info("启动thrift接收邮件") server.startService(glocker)
def run(self): #启动服务器之前,先把邮件表的状态全部修改为初始状态 updateStateAll(constants.MAIL_INIT) logger.info("服务器启动修改所有待发送邮件为初始状态") #time.sleep(1000) #创建进程池(大小为10) pool = mul.Pool(processes=3) global processCount processCount = 3 #i = 0 unvalid_mial = False while True: #发邮件当网络堵塞,event_network_except会设置成true if event_network_except.is_set(): event_network_except.wait(constants.SLEEP_TIME_NETWORK_EXCEPT)#等待 event_network_except.clear()#清楚set #print "开始获取邮件" mails = [] if unvalid_mial==False: mails = self.getMails() if len(mails) == 0: #print "获取邮件成功 count: %s" % len(mails) logger.info("主进程等待时间:%s" % constants.SLEEP_CLIENT_MAILS_REACH) self.glocker.wait(constants.SLEEP_CLIENT_MAILS_REACH) self.glocker.clear() unvalid_mial = False else: unvalid_mial = True logger.info("获取邮件成功 count: %s" % len(mails)) for mail in mails: while processCount <= 0: time.sleep(1) if self.isContinue(mail): #修改为初始状态,优先级降级 updatePriority(mail=mail, state=constants.MAIL_INIT, priority=mail['priority']-1) continue unvalid_mial = False #判断邮件上次发送时间,如果再10内不需要发送 dayto = datetime.datetime.now() datefrom = DateUtils.getDateBySeconds(dayto, constants.TIME_LIMIT_SECONDS) if mail['finish_time']!=None and mail['finish_time'] > datefrom: logger.info("同一封邮件再10内被读取两次,修改邮件为初始状态,跳过该邮件id=%s" % mail['id']) updateStateByIds(state = constants.MAIL_INIT, ids=mail['id']) continue #i = i + 1 #print "i=%s" % i #修改邮件为已分配状态,并将邮件分配给一个单独进程 processCount = processCount - 1 updateStateByIds(state=constants.MAIL_ASSIGNED, ids=mail['id'], time=constants.TIME_ASSIGN) pool.apply_async(func=sendMails, args=(mail,), callback=self.increaseProcessCount) #time.sleep(10000000) pool.close()#关闭进程池,不再创建新的进程 pool.join()#等带进程池中的所有进程结束
def increaseProcessCount(self, result): global processCount processCount = processCount + 1 logger.info("increaseProcessCount-processCount: %s" % processCount)
def run(self): #启动服务器之前,先把邮件表的状态全部修改为初始状态 updateStateAll(constants.MAIL_INIT) logger.info("服务器启动修改所有待发送邮件为初始状态") #time.sleep(1000) #创建进程池(大小为10) pool = mul.Pool(processes=3) global processCount processCount = 3 #i = 0 unvalid_mial = False while True: #发邮件当网络堵塞,event_network_except会设置成true if event_network_except.is_set(): event_network_except.wait( constants.SLEEP_TIME_NETWORK_EXCEPT) #等待 event_network_except.clear() #清楚set #print "开始获取邮件" mails = [] if unvalid_mial == False: mails = self.getMails() if len(mails) == 0: #print "获取邮件成功 count: %s" % len(mails) logger.info("主进程等待时间:%s" % constants.SLEEP_CLIENT_MAILS_REACH) self.glocker.wait(constants.SLEEP_CLIENT_MAILS_REACH) self.glocker.clear() unvalid_mial = False else: unvalid_mial = True logger.info("获取邮件成功 count: %s" % len(mails)) for mail in mails: while processCount <= 0: time.sleep(1) if self.isContinue(mail): #修改为初始状态,优先级降级 updatePriority(mail=mail, state=constants.MAIL_INIT, priority=mail['priority'] - 1) continue unvalid_mial = False #判断邮件上次发送时间,如果再10内不需要发送 dayto = datetime.datetime.now() datefrom = DateUtils.getDateBySeconds( dayto, constants.TIME_LIMIT_SECONDS) if mail['finish_time'] != None and mail[ 'finish_time'] > datefrom: logger.info("同一封邮件再10内被读取两次,修改邮件为初始状态,跳过该邮件id=%s" % mail['id']) updateStateByIds(state=constants.MAIL_INIT, ids=mail['id']) continue #i = i + 1 #print "i=%s" % i #修改邮件为已分配状态,并将邮件分配给一个单独进程 processCount = processCount - 1 updateStateByIds(state=constants.MAIL_ASSIGNED, ids=mail['id'], time=constants.TIME_ASSIGN) pool.apply_async(func=sendMails, args=(mail, ), callback=self.increaseProcessCount) #time.sleep(10000000) pool.close() #关闭进程池,不再创建新的进程 pool.join() #等带进程池中的所有进程结束
def run(self): while True: self.delete() logger.info("定时删除邮件线程") time.sleep(self.interval)