def fire_ready(cls): '''Fires all direct subscriptions with notifications as well as all summary & digest subscriptions with notifications that are ready. Clears the mailbox queue. ''' now = datetime.utcnow() # Queries to find all matching subscription objects q_direct = dict( type='direct', queue_empty=False, ) if MAILBOX_QUIESCENT: q_direct['last_modified'] = {'$lt': now - MAILBOX_QUIESCENT} q_digest = dict(type={'$in': ['digest', 'summary']}, next_scheduled={'$lt': now}) def find_and_modify_direct_mbox(): return cls.query.find_and_modify( query=q_direct, update={'$set': dict( queue=[], queue_empty=True, )}, new=False) for mbox in take_while_true(find_and_modify_direct_mbox): try: mbox.fire(now) except: log.exception('Error firing mbox: %s with queue: [%s]', str(mbox._id), ', '.join(mbox.queue)) # re-raise so we don't keep (destructively) trying to process # mboxes raise for mbox in cls.query.find(q_digest): next_scheduled = now if mbox.frequency.unit == 'day': next_scheduled += timedelta(days=mbox.frequency.n) elif mbox.frequency.unit == 'week': next_scheduled += timedelta(days=7 * mbox.frequency.n) elif mbox.frequency.unit == 'month': next_scheduled += timedelta(days=30 * mbox.frequency.n) mbox = cls.query.find_and_modify( query=dict(_id=mbox._id), update={ '$set': dict( next_scheduled=next_scheduled, queue=[], queue_empty=True, ) }, new=False) mbox.fire(now)
def fire_ready(cls): '''Fires all direct subscriptions with notifications as well as all summary & digest subscriptions with notifications that are ready. Clears the mailbox queue. ''' now = datetime.utcnow() # Queries to find all matching subscription objects q_direct = dict( type='direct', queue_empty=False, ) if MAILBOX_QUIESCENT: q_direct['last_modified'] = {'$lt': now - MAILBOX_QUIESCENT} q_digest = dict( type={'$in': ['digest', 'summary']}, next_scheduled={'$lt': now}) def find_and_modify_direct_mbox(): return cls.query.find_and_modify( query=q_direct, update={'$set': dict( queue=[], queue_empty=True, )}, new=False) for mbox in take_while_true(find_and_modify_direct_mbox): try: mbox.fire(now) except: log.exception( 'Error firing mbox: %s with queue: [%s]', str(mbox._id), ', '.join(mbox.queue)) # re-raise so we don't keep (destructively) trying to process # mboxes raise for mbox in cls.query.find(q_digest): next_scheduled = now if mbox.frequency.unit == 'day': next_scheduled += timedelta(days=mbox.frequency.n) elif mbox.frequency.unit == 'week': next_scheduled += timedelta(days=7 * mbox.frequency.n) elif mbox.frequency.unit == 'month': next_scheduled += timedelta(days=30 * mbox.frequency.n) mbox = cls.query.find_and_modify( query=dict(_id=mbox._id), update={'$set': dict( next_scheduled=next_scheduled, queue=[], queue_empty=True, )}, new=False) mbox.fire(now)
def fire_ready(cls): """Fires all direct subscriptions with notifications as well as all summary & digest subscriptions with notifications that are ready. Clears the mailbox queue. """ now = datetime.utcnow() # Queries to find all matching subscription objects q_direct = dict(type="direct", queue_empty=False) if MAILBOX_QUIESCENT: q_direct["last_modified"] = {"$lt": now - MAILBOX_QUIESCENT} q_digest = dict(type={"$in": ["digest", "summary"]}, next_scheduled={"$lt": now}) def find_and_modify_direct_mbox(): return cls.query.find_and_modify( query=q_direct, update={"$set": dict(queue=[], queue_empty=True)}, new=False ) for mbox in take_while_true(find_and_modify_direct_mbox): try: mbox.fire(now) except: log.exception("Error firing mbox: %s with queue: [%s]", str(mbox._id), ", ".join(mbox.queue)) raise # re-raise so we don't keep (destructively) trying to process mboxes for mbox in cls.query.find(q_digest): next_scheduled = now if mbox.frequency.unit == "day": next_scheduled += timedelta(days=mbox.frequency.n) elif mbox.frequency.unit == "week": next_scheduled += timedelta(days=7 * mbox.frequency.n) elif mbox.frequency.unit == "month": next_scheduled += timedelta(days=30 * mbox.frequency.n) mbox = cls.query.find_and_modify( query=dict(_id=mbox._id), update={"$set": dict(next_scheduled=next_scheduled, queue=[], queue_empty=True)}, new=False, ) mbox.fire(now)