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