diff --git a/api.py b/api.py index 33a8399..b953618 100644 --- a/api.py +++ b/api.py @@ -75,6 +75,7 @@ def Poll(google_user, client, client_address, instance_id, args): return { 'result': 'ok', + 'new': instance.newly_created, 'events': events, } @@ -161,7 +162,6 @@ def Subscribe(google_user, client, client_address, instance_id, args): 'result': 'access_denied', } - if not instance or not instance.active: # Probably a race with the channel opening return { diff --git a/lib/models.py b/lib/models.py index 8f0c98e..3015844 100644 --- a/lib/models.py +++ b/lib/models.py @@ -104,7 +104,16 @@ class Instance(db.Model): @classmethod def FindOrCreate(cls, instance_id, **kwargs): logging.info('Instance: %s', instance_id) - return cls.get_or_insert(instance_id, **kwargs) + 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.put() + entity.newly_created = True + return entity + return db.run_in_transaction(_FindOrCreate) def Delete(self): logging.info('Deleting instance %s', self.key().name())