Skip to content

Commit

Permalink
Make pytype happy
Browse files Browse the repository at this point in the history
  • Loading branch information
jankatins committed Sep 14, 2021
1 parent b47815d commit 2d7c246
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pglookout/cluster_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def _connect_to_db(self, instance, dsn):

def _fetch_observer_state(self, instance, uri):
result = {"fetch_time": get_iso_timestamp(), "connection": True}
fetch_uri = uri + "/state.json"
try:
fetch_uri = uri + "/state.json"
response = self.session.get(fetch_uri, timeout=5.0)

# check time difference for large skews
Expand Down
13 changes: 7 additions & 6 deletions pglookout/pglookout.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
class PgLookout:
def __init__(self, config_path):
self.log = logging.getLogger("pglookout")
self.stats = None
self.running = True
self.replication_lag_over_warning_limit = False

Expand Down Expand Up @@ -106,7 +105,9 @@ def load_config(self, _signal=None, _frame=None):
self.config = json.load(fp)
except Exception as ex: # pylint: disable=broad-except
self.log.exception("Invalid JSON config, exiting")
self.stats.unexpected_exception(ex, where="load_config")
stats = getattr(self, "stats")
if stats:
stats.unexpected_exception(ex, where="load_config")
sys.exit(1)

# statsd settings may have changed
Expand Down Expand Up @@ -172,9 +173,9 @@ def write_cluster_state_to_json_file(self):
"""
start_time = time.monotonic()
state_file_path = self.config.get("json_state_file_path", "/tmp/pglookout_state.json")
overall_state = {"db_nodes": self.cluster_state, "observer_nodes": self.observer_state,
"current_master": self.current_master}
try:
overall_state = {"db_nodes": self.cluster_state, "observer_nodes": self.observer_state,
"current_master": self.current_master}
json_to_dump = json.dumps(overall_state, indent=4)
self.log.debug("Writing JSON state file to: %r, file_size: %r", state_file_path, len(json_to_dump))
with open(state_file_path + ".tmp", "w") as fp:
Expand Down Expand Up @@ -622,8 +623,8 @@ def check_for_maintenance_mode_file(self):
return os.path.exists(self.config.get("maintenance_mode_file", "/tmp/pglookout_maintenance_mode_file"))

def create_alert_file(self, filename):
filepath = os.path.join(self.config.get("alert_file_dir", os.getcwd()), filename)
try:
filepath = os.path.join(self.config.get("alert_file_dir", os.getcwd()), filename)
self.log.debug("Creating alert file: %r", filepath)
with open(filepath, "w") as fp:
fp.write("alert")
Expand All @@ -632,8 +633,8 @@ def create_alert_file(self, filename):
self.stats.unexpected_exception(ex, where="create_alert_file")

def delete_alert_file(self, filename):
filepath = os.path.join(self.config.get("alert_file_dir", os.getcwd()), filename)
try:
filepath = os.path.join(self.config.get("alert_file_dir", os.getcwd()), filename)
if os.path.exists(filepath):
self.log.debug("Deleting alert file: %r", filepath)
os.unlink(filepath)
Expand Down
6 changes: 4 additions & 2 deletions pglookout/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,26 @@ def close(self):

class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
assert isinstance(self.server, ThreadedWebServer), f"server: {self.server!r}"
self.server.log.debug("Got request: %r", self.path)
if self.path.startswith("/state.json"):
self.send_response(200)
self.send_header('Content-type', 'application/json')
response = json.dumps(self.server.cluster_state, indent=4).encode("utf8")
self.send_header('Content-length', len(response))
self.send_header('Content-length', str(len(response)))
self.end_headers()
self.wfile.write(response)
else:
self.send_response(404)

def do_POST(self):
assert isinstance(self.server, ThreadedWebServer), f"server: {self.server!r}"
self.server.log.debug("Got request: %r", self.path)
if self.path.startswith("/check"):
self.server.cluster_monitor_check_queue.put("request from webserver")
self.server.log.info("Immediate status check requested")
self.send_response(204)
self.send_header('Content-length', 0)
self.send_header('Content-length', str(0))
self.end_headers()
else:
self.send_response(404)

0 comments on commit 2d7c246

Please sign in to comment.