Update monitor.py to use .env instead of params.json
Complete the migration started in 42c89dc by updating the monitoring script to load configuration from .env files using python-dotenv.
This commit is contained in:
parent
3b7484fbe6
commit
f9fb7552bb
1 changed files with 8 additions and 22 deletions
30
monitor.py
30
monitor.py
|
|
@ -5,13 +5,14 @@ import sys
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
import json
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load environment variables from .env file
|
||||||
|
load_dotenv(".env")
|
||||||
|
|
||||||
def fread(filename):
|
# Configuration from environment
|
||||||
"""Read file and close the file."""
|
stacosys_url = os.getenv("STACOSYS_URL", "")
|
||||||
with open(filename, "r") as f:
|
external_check_cmd = os.getenv("EXTERNAL_CHECK", "")
|
||||||
return f.read()
|
|
||||||
|
|
||||||
|
|
||||||
def get_comment_count():
|
def get_comment_count():
|
||||||
|
|
@ -20,11 +21,8 @@ def get_comment_count():
|
||||||
Returns:
|
Returns:
|
||||||
int: Total comment count, or 0 if request fails
|
int: Total comment count, or 0 if request fails
|
||||||
"""
|
"""
|
||||||
req_url = params["stacosys_url"] + "/comments/count"
|
req_url = stacosys_url + "/comments/count"
|
||||||
query_params = dict(
|
resp = requests.get(url=req_url)
|
||||||
token=params["stacosys_token"]
|
|
||||||
)
|
|
||||||
resp = requests.get(url=req_url, params=query_params)
|
|
||||||
return 0 if not resp.ok else int(resp.json()["count"])
|
return 0 if not resp.ok else int(resp.json()["count"])
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -33,18 +31,6 @@ def _exit_program():
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
# Default parameters.
|
|
||||||
params = {
|
|
||||||
"stacosys_token": "",
|
|
||||||
"stacosys_url": "",
|
|
||||||
"external_check": "",
|
|
||||||
}
|
|
||||||
|
|
||||||
# If params.json exists, load it.
|
|
||||||
if os.path.isfile("params.json"):
|
|
||||||
params.update(json.loads(fread("params.json")))
|
|
||||||
|
|
||||||
external_check_cmd = params["external_check"]
|
|
||||||
initial_count = get_comment_count()
|
initial_count = get_comment_count()
|
||||||
print(f"Comments = {initial_count}")
|
print(f"Comments = {initial_count}")
|
||||||
while True:
|
while True:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue