Fix polling and acks.
This commit is contained in:
14
api.py
14
api.py
@@ -28,11 +28,8 @@ import config
|
|||||||
|
|
||||||
|
|
||||||
def CreateChannel(google_user, client, client_address, instance_id, args):
|
def CreateChannel(google_user, client, client_address, instance_id, args):
|
||||||
instance = models.Instance.FindOrCreate(instance_id)
|
instance = models.Instance.FindOrCreate(instance_id, polling=False)
|
||||||
|
assert not instance.polling
|
||||||
if instance.polling:
|
|
||||||
instance.polling = False
|
|
||||||
instance.save()
|
|
||||||
|
|
||||||
token = channel.create_channel(
|
token = channel.create_channel(
|
||||||
client_id=instance_id,
|
client_id=instance_id,
|
||||||
@@ -56,11 +53,8 @@ def CreateChannel(google_user, client, client_address, instance_id, args):
|
|||||||
|
|
||||||
|
|
||||||
def Poll(google_user, client, client_address, instance_id, args):
|
def Poll(google_user, client, client_address, instance_id, args):
|
||||||
instance = models.Instance.FindOrCreate(instance_id)
|
instance = models.Instance.FindOrCreate(instance_id, polling=True, active=True)
|
||||||
|
assert instance.polling
|
||||||
if not instance.polling:
|
|
||||||
instance.polling = True
|
|
||||||
instance.save()
|
|
||||||
|
|
||||||
events = []
|
events = []
|
||||||
if google_user:
|
if google_user:
|
||||||
|
|||||||
@@ -101,14 +101,14 @@ class Instance(db.Model):
|
|||||||
return cls.get_by_key_name(instance_id)
|
return cls.get_by_key_name(instance_id)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def FindOrCreate(cls, instance_id, polling=False):
|
def FindOrCreate(cls, instance_id, **kwargs):
|
||||||
logging.info('Instance: %s', instance_id)
|
logging.info('Instance: %s', instance_id)
|
||||||
return cls.get_or_insert(instance_id, polling=polling)
|
return cls.get_or_insert(instance_id, **kwargs)
|
||||||
|
|
||||||
def GetSubscriptions(self):
|
def GetSubscriptions(self):
|
||||||
return (
|
return (
|
||||||
Subscription.all()
|
Subscription.all()
|
||||||
.filter('instance=', self))
|
.filter('instance =', self))
|
||||||
|
|
||||||
|
|
||||||
class Subject(db.Model):
|
class Subject(db.Model):
|
||||||
@@ -412,7 +412,8 @@ class Subscription(db.Model):
|
|||||||
cls(parent=subject,
|
cls(parent=subject,
|
||||||
instance=instance,
|
instance=instance,
|
||||||
readable_only_by_me=readable_only_by_me,
|
readable_only_by_me=readable_only_by_me,
|
||||||
writable_only_by_me=writable_only_by_me).put()
|
writable_only_by_me=writable_only_by_me,
|
||||||
|
polling=polling).put()
|
||||||
return subject.GetEvents(messages, last_id, request)
|
return subject.GetEvents(messages, last_id, request)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -432,7 +433,7 @@ class Subscription(db.Model):
|
|||||||
def SendMessage(self, msg):
|
def SendMessage(self, msg):
|
||||||
encoded = json.dumps(msg, default=utils.EncodeJSON)
|
encoded = json.dumps(msg, default=utils.EncodeJSON)
|
||||||
if self.polling:
|
if self.polling:
|
||||||
Event(parent=this,
|
Event(parent=self,
|
||||||
json=encoded).save()
|
json=encoded).save()
|
||||||
else:
|
else:
|
||||||
instance_key = Subscription.instance.get_value_for_datastore(self)
|
instance_key = Subscription.instance.get_value_for_datastore(self)
|
||||||
@@ -445,7 +446,7 @@ class Subscription(db.Model):
|
|||||||
.ancestor(self))
|
.ancestor(self))
|
||||||
ret = []
|
ret = []
|
||||||
for e in events:
|
for e in events:
|
||||||
if e in acks:
|
if str(e.key()) in acks:
|
||||||
e.delete()
|
e.delete()
|
||||||
else:
|
else:
|
||||||
ret.append(e.ToEvent())
|
ret.append(e.ToEvent())
|
||||||
|
|||||||
Reference in New Issue
Block a user