-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatcher.py
88 lines (72 loc) · 3.42 KB
/
watcher.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import time
import logging
import traceback
from rbtools.api.client import RBClient
import rbtools
import datetime
import config
import os
from WatcherMemory import WatcherMemory
def parse_server_time_stamp(timestamp):
return datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
class Watcher:
"""The thing that watches review board for things"""
def __init__(self, bot_manager):
print("The watcher is born")
self.creds = config.config['creds']
self.server = config.config['review_board_server']
self.client = RBClient(self.server, username=self.creds['username'], password=self.creds['password'])
self.bot_name_list = config.config['bots'].keys()
self.bot_group_list = {bot_name: config.config['bots'][bot_name]['groups']
if 'groups' in config.config['bots'][bot_name]
else []
for bot_name in self.bot_name_list}
self.bot_manager = bot_manager
self.requests_seen = {}
self.keep_watching = False
# TODO make a configuration
# Protect against non existance
self.bot_food_path = os.path.abspath("./botfood_folder")
self.memory = WatcherMemory(self.client, self.bot_food_path, self.bot_group_list)
# Get new requests from the reviewboard server
# Same as old function, but with smarter approach
def get_new_requests(self):
root = self.client.get_root()
# TODO do I need to remove duplicates???
requests = []
for bot_name in self.bot_name_list:
try:
requests += root.get_review_requests(to_users=bot_name)
except rbtools.api.errors.APIError as error:
if error.message != "Object does not exist":
raise error
logging.info("list of requests after filtering by bot names " + str([request["id"] for request in requests]))
return requests
def watch(self):
"""Periodically check for new reviews, spin off bots when needed"""
self.keep_watching = True
"Until my watch is ended"
print("Watcher: I am watching")
while self.keep_watching:
try:
# Get new requests from reviewboard server
new_requests = self.get_new_requests()
# Add those requets to the database
self.memory.add_requests(new_requests, self.client)
# Ask the database for requests that need "attention"
requests_in_need_of_attention = self.memory.fresh_requests()
# Handle those requests
if requests_in_need_of_attention:
self.bot_manager.process_new_requests(requests_in_need_of_attention)
self.memory.mark_attended(requests_in_need_of_attention)
except Exception as err:
# If something goes wrong, create a new client and move on.
# It's a little too broad, but it should keep the lights on.
self.client = RBClient(self.server, username=self.creds['username'], password=self.creds['password'])
traceback.print_exc()
logging.error("The watcher encountered an error while polling")
time.sleep(config.config['polling_wait_time'])
print("Watcher: My watch has ended")
def stop(self):
self.keep_watching = False
# TODO need to join