class APNSFeedbackWorker(object): def __init__(self): # apns configuration session = Session() if conf.getboolean('application', 'debug'): con = session.new_connection("feedback_sandbox", cert_file=conf.get('apns', 'cert_sandbox')) else: con = session.new_connection("feedback_production", cert_file=conf.get('apns', 'cert_production')) self.srv = APNs(con) def start(self): try: # on any IO failure after successful connection this generator # will simply stop iterating. you will pick the rest of the tokens # during next feedback session. for token, when in self.srv.feedback(): # every time a devices sends you a token, you should store # {token: given_token, last_update: datetime.datetime.now()} device = Device.query.filter_by(platform_id=token).first() if not device: continue if device.updated_at and device.updated_at < when: # the token wasn't updated after the failure has # been reported, so the token is invalid and you should # stop sending messages to it. db.session.delete(device) elif not device.updated_at: db.session.delete(device) db.session.commit() except Exception as ex: db.session.rollback() logger.exception(ex)
def test_feedback(self): connection = self.get_connection(session=self.get_session(feedback=5), address="feedback_production") srv = APNs(connection) self.assertEqual(len(list(srv.feedback())), 5)
def test_feedback(self): if not self._certificate_available(): # Skip, no certificate available return connection = self.get_connection(session=self.get_session(), address="feedback_sandbox") srv = APNs(connection) self.assertEqual(len(list(srv.feedback())), 0)
def test_feedback(self): backend = DummyBackend(feedback=5) session = Session(pool=backend) feed_con = session.new_connection("feedback_production", cert_string="certificate") srv = APNs(feed_con) self.assertEqual(len(list(srv.feedback())), 5)