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