annotate web/settings.py @ 557:830ee5663703

increase waiter timeout
author Matt Johnston <matt@ucc.asn.au>
date Tue, 09 Jun 2015 23:37:49 +0800
parents 9499bd2f344b
children 03e540c3ec24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import gevent
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import fcntl
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import hashlib
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 class Settings(object):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 RAND_SIZE = 15 # 120 bits
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 """ Handles state updates from both the web UI and from the fridge client.
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 The fridge client is canonical. It provides the epoch (apart from 'startepoch'), that
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 is changed any time the fridge reloads its local config. The fridge only accepts
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 updates that have the same epoch.
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 When the web UI changes it keeps the same epoch but generates a new tag. The fridge sends
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 its current known tag and waits for it to change.
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 content is opaque, presently a dictionary of decoded json
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 """
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 def __init__(self):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 self.event = gevent.event.Event()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 self.contents = None
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.epoch = None
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.tag = None
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self.update(self, None, 'startepoch')
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def wait(self, epoch_tag = None, timeout = None):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 """ returns false if the timeout was hit """
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 if self.epoch_tag() != epoch_tag:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 # has alredy changed
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 return True
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return self.event.wait(timeout)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def epoch_tag(self):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 return '%s-%s' % (self.epoch, self.tag)
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 def random(self):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return binascii.hexlify(os.urandom(self.RAND_SIZE))
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 def update(self, contents, epoch = None):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 """ replaces settings contents and updates waiters if changed """
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 if epoch:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 if self.epoch == epoch:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 else:
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 self.epoch = epoch
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 self.tag = self.random()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 self.contents = contents
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 self.event.set()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 self.event.clear()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 def get(self):
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 """ Returns (contents, epoch-tag) """
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 return self.contents, self.epoch_tag()
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61