From 33f368910f856a76dcbacb00e8f90c1d173a47bd Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 19 Jun 2015 22:43:20 -0700 Subject: [PATCH 1/2] Better method for tracking reconnect of polling clients. --- api.py | 2 +- lib/models.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api.py b/api.py index b953618..2eb412b 100644 --- a/api.py +++ b/api.py @@ -75,7 +75,7 @@ def Poll(google_user, client, client_address, instance_id, args): return { 'result': 'ok', - 'new': instance.newly_created, + 'instance_generation': instance.generation, 'events': events, } diff --git a/lib/models.py b/lib/models.py index 875a768..c2d1dad 100644 --- a/lib/models.py +++ b/lib/models.py @@ -18,6 +18,7 @@ import hashlib import logging import random import struct +import uuid from google.appengine.api import channel from google.appengine.api import users @@ -95,6 +96,7 @@ class Instance(db.Model): active = db.BooleanProperty(required=True, default=False) polling = db.BooleanProperty(required=True, default=False) last_poll = db.DateTimeProperty(required=True, auto_now=True) + generation = db.StringProperty(required=True) @classmethod def FromID(cls, instance_id): @@ -107,11 +109,12 @@ class Instance(db.Model): def _FindOrCreate(): entity = cls.get_by_key_name(instance_id) if entity: - entity.newly_created = False return entity - entity = cls(key_name=instance_id, **kwargs) + entity = cls( + key_name=instance_id, + generation=str(uuid.uuid4()), + **kwargs) entity.put() - entity.newly_created = True return entity return db.run_in_transaction(_FindOrCreate) From a8d066162655a1be6de6bfc46ea0bed7e7efb892 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 19 Jun 2015 22:43:41 -0700 Subject: [PATCH 2/2] Forgot the cron handler. --- cron.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cron.py diff --git a/cron.py b/cron.py new file mode 100644 index 0000000..32baa05 --- /dev/null +++ b/cron.py @@ -0,0 +1,40 @@ +# Copyright 2014, Ian Gulliver +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import datetime +import logging +import webapp2 + +from cosmopolite.lib import models +from cosmopolite.lib import utils + +import config + + +class CleanupPollingInstances(webapp2.RequestHandler): + + @utils.local_namespace + def get(self): + cutoff = datetime.datetime.now() - datetime.timedelta(minutes=1) + query = ( + models.Instance.all() + .filter('polling =', True) + .filter('last_poll <', cutoff)) + for instance in query: + instance.Delete() + + +app = webapp2.WSGIApplication([ + (config.URL_PREFIX + '/cron/cleanup_polling_instances', CleanupPollingInstances), +])