-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
51 lines (37 loc) · 1.19 KB
/
worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import os
from time import sleep
import redis
from light_emitting_desk.desk import Desk
from light_emitting_desk.utils import conf
class Worker:
"""Worker to process the lighting jobs."""
def __init__(self):
"""Construct."""
self.redis = redis.Redis()
desk_conf = conf["sectors"]
if "TEST_SECTORS" in os.environ: # nocov
desk_conf = json.loads(os.environ["TEST_SECTORS"])
self.desk = Desk(desk_conf)
def process(self, job):
"""Process a job."""
try:
args = json.loads(job.decode("utf-8"))
self.desk.light_up(args["colour"], args)
except json.decoder.JSONDecodeError: # nocov
print("Your data is bad")
def poll(self):
"""If there's a job on the queue, pull it off and process it."""
data = self.redis.lpop("jobs")
if data:
self.process(data)
else: # nocov
sleep(conf["worker"]["interval"])
def work(self): # nocov
"""Keep working forever."""
while True:
self.poll()
if __name__ == "__main__": # nocov
print("worker starting")
worker = Worker()
worker.work()