def sendRetry(self, mail): sql = "update mails set send_count=%s, state=%s, finish_time=%s where id = %s" if mail['send_count'] < constants.SEND_RETRY_COUNT: param = [mail['send_count']+1, constants.MAIL_INIT, datetime.datetime.now(), mail['id']]; else: param = [mail['send_count']+1, constants.MAIL_FAIL, datetime.datetime.now(), mail['id']]; dbconn().update(sql, param=param)
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 sendRetry(self, mail): sql = "update mails set send_count=%s, state=%s, finish_time=%s where id = %s" if mail['send_count'] < constants.SEND_RETRY_COUNT: param = [ mail['send_count'] + 1, constants.MAIL_INIT, datetime.datetime.now(), mail['id'] ] else: param = [ mail['send_count'] + 1, constants.MAIL_FAIL, datetime.datetime.now(), mail['id'] ] dbconn().update(sql, param=param)
def getMails(): sql = "select id, sendto, smtp_host, subject, content, attach_files, send_count, priority, create_time, send_count, state from mails where state=%s and send_count<%s order by priority desc, send_count asc, create_time asc, id asc limit %s" % (constants.MAIL_INIT, constants.SEND_RETRY_COUNT, constants.READ_CONUT) mails = dbconn().getAll(sql) if len(mails) != 0:#将读取到的邮件状态修改为加载完成状态(2) ids = map( lambda a: str(a["id"]), mails) updateStateByIds(state=constants.MAIL_LOADED, ids=ids, time='loaded_time') return mails
def delete(self): dayto = datetime.datetime.now() datefrom = DateUtils.getDate(dayto, constants.TIME_LIMIT) sql = "delete from mails where (state=%s or state<0) and finish_time < '%s'::timestamp" % ( constants.MAIL_SUCCESS, datefrom) #print sql return dbconn().delete(sql)
def isContinue(mail): dayto = datetime.datetime.now() datefrom = DateUtils.getDate(dayto, constants.TIME_LIMIT) sql = "select count(*) as count from mails where smtp_host='%s' and state=5 and finish_time between '%s'::timestamp and '%s'::timestamp" % (mail['smtp_host'], datefrom, dayto) result = dbconn().getOne(sql) if result['count'] >= constants.TIME_MAX_COUNT: return True else: return False
def getMails(): sql = "select id, sendto, smtp_host, subject, content, attach_files, send_count, priority, create_time, send_count, state from mails where state=%s and send_count<%s order by priority desc, send_count asc, create_time asc, id asc limit %s" % ( constants.MAIL_INIT, constants.SEND_RETRY_COUNT, constants.READ_CONUT) mails = dbconn().getAll(sql) if len(mails) != 0: #将读取到的邮件状态修改为加载完成状态(2) ids = map(lambda a: str(a["id"]), mails) updateStateByIds(state=constants.MAIL_LOADED, ids=ids, time='loaded_time') return mails
def isContinue(self, mail): dayto = datetime.datetime.now() datefrom = DateUtils.getDate(dayto, constants.TIME_LIMIT) sql = "select count(*) as count from mails where smtp_host='%s' and state=5 and finish_time between '%s'::timestamp and '%s'::timestamp" % ( mail['smtp_host'], datefrom, dayto) result = dbconn().getOne(sql) if result['count'] >= constants.TIME_MAX_COUNT: return True else: return False
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 delete(self): dayto = datetime.datetime.now() datefrom = DateUtils.getDate(dayto, constants.TIME_LIMIT) sql = "delete from mails where (state=%s or state<0) and finish_time < '%s'::timestamp"% (constants.MAIL_SUCCESS, datefrom) #print sql return dbconn().delete(sql)