def checkSmsCode(self, info): tel = info['tel'] code = info['code'] now = datetime.now() # 判断是否已经注册 result = db.session.query(UserInfo).filter(UserInfo.tel == tel).first() if result is None: errorInfo = ErrorInfo['TENDER_23'] errorInfo['detail'] = None return (False, errorInfo) result = db.session.query(SmsCode).filter( and_( SmsCode.tel == tel, SmsCode.code == code, func.TIMESTAMPDIFF(text('MINUTE'), SmsCode.createTime, now) <= 5)).first() if result is None: errorInfo = ErrorInfo['TENDER_06'] errorInfo['detail'] = result return (False, errorInfo) return (True, None)
def choose_queue(self, session=None): wait_second = self.urgent_task_wait_threshold.total_seconds() col_count_group = func.count(ErgoTask.queue_url) col_min_execution_date = func.min(ErgoTask.ti_execution_date) dialect = session.bind.dialect.name self.log.debug(f'using dialect: {dialect}') if dialect == 'postgresql': col_is_urgent_task = ( (func.EXTRACT(text('EPOCH'), func.now()) - func.EXTRACT(text('EPOCH'), col_min_execution_date)) >= wait_second) else: col_is_urgent_task = func.TIMESTAMPDIFF(text('SECOND'), col_min_execution_date, func.now()) >= wait_second valid_queues = (session.query( ErgoTask.queue_url, col_count_group, col_is_urgent_task).filter(self.filter_ergo_task).group_by( ErgoTask.queue_url).order_by(col_is_urgent_task.desc(), col_count_group.desc())) queue = valid_queues.first() return queue.queue_url if queue else None
def execute(self): # we periodically clear any locks that have exceeded lock time # this will be messages that were locked but never sent for some reason if self.next_lock_timeout_check is None or datetime.datetime.now( ) >= self.next_lock_timeout_check: saq.db.execute( MessageRouting.__table__.update().values(lock=None).where( and_( MessageRouting.lock == None, func.TIMESTAMPDIFF(text('SECOND'), MessageRouting.lock_time, func.NOW())))) saq.db.commit() self.next_lock_timeout_check = datetime.datetime.now() + \ datetime.timedelta(seconds=saq.CONFIG['messaging'].getint('lock_timeout')) # get the next message to send message_route = saq.db.query(MessageRouting).options( joinedload('message')).filter( MessageRouting.lock == self.lock_uuid).order_by( asc(MessageRouting.message_id)).first() if message_route is None: # if we didn't get one then go ahead and lock the next batch of messages target_ids = saq.db.query(MessageRouting.id).filter(and_( MessageRouting.lock == None, MessageRouting.route == self.route))\ .order_by(asc(MessageRouting.id))\ .limit(self.batch_size)\ .all() target_ids = [_[0] for _ in target_ids] # did we not find anything? if not target_ids: if self.controlled_stop: raise ControlledStop() else: return 5 saq.db.execute(MessageRouting.__table__.update().values( lock=self.lock_uuid, lock_time=func.NOW()).where( and_(MessageRouting.id.in_(target_ids), MessageRouting.lock == None, MessageRouting.route == self.route))) saq.db.commit() # try again to get the next message to send message_route = saq.db.query(MessageRouting).options( joinedload('message')).filter( MessageRouting.lock == self.lock_uuid).order_by( asc(MessageRouting.message_id)).first() # if we still didn't get a message then we wait for a while if message_route is None: if self.controlled_stop: raise ControlledStop() else: return 5 # TODO make configurable? # dispatch the message logging.debug( f"dispatching message {message_route.message_id} to route {message_route.route} destination {message_route.destination}" ) self.dispatch(message_route.message, message_route.destination) # clear this message out saq.db.execute(MessageRouting.__table__.delete().where( MessageRouting.id == message_route.id)) # and finally clear the message if all the routes have completed saq.db.execute(Message.__table__.delete().where( Message.id.notin_(saq.db.query(MessageRouting.message_id)))) saq.db.commit() return 0
def online_time(cls): return func.TIMESTAMPDIFF(text('second'), '1970-01-01', cls.__table__.c.upsum)
def total_time(cls): return func.TIMESTAMPDIFF(text('second'), cls.__table__.c.start, cls.__table__.c.end)