annotate web/settings.py @ 287:97d99eb42d27 py3client

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