Mercurial > templog
annotate web/settings.py @ 638:a9f353f488d0 rust
fix channels
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 09 Nov 2019 11:35:59 +0800 |
parents | 03e540c3ec24 |
children | 87c20b8c5472 |
rev | line source |
---|---|
253 | 1 import gevent |
2 import fcntl | |
3 import hashlib | |
4 | |
258
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
5 import binascii |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
6 import os |
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
7 |
253 | 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 | |
258
03e540c3ec24
fix server side long polling
Matt Johnston <matt@ucc.asn.au>
parents:
253
diff
changeset
|
28 self.update(None, 'startepoch') |
253 | 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 |