Skip to content

Commit

Permalink
restructure and ready to test on slurm
Browse files Browse the repository at this point in the history
  • Loading branch information
kerrychu committed Jan 23, 2024
1 parent c3c7b4a commit 5f55721
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 23 deletions.
5 changes: 4 additions & 1 deletion dot_env_template
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
slack_webhook=""
SLACK_JOB_WEBHOOK="" <- start with https://
SLACK_QUOTA_WEBHOOK=""
ENABLE_JOB_DEBUG_MODE=False <- True or False only
PROJECT_IDS="S0090,S0090" <- no space, comma separated, no trailing comma
10 changes: 2 additions & 8 deletions hooks/slack.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import requests
import json
from typing import Dict
from dotenv import load_dotenv
import os

load_dotenv()
slack_webhook = os.getenv("slack_webhook")


def send_slack_message(data: Dict[str, str]) -> str:
def send_slack_message(data: dict[str, str], webhook: str) -> str:
"""
send slack message with given data
Args:
Expand All @@ -20,7 +14,7 @@ def send_slack_message(data: Dict[str, str]) -> str:
payload_json = json.dumps(payload)
headers = {"Content-type": "application/json"}
response = requests.request(
"POST", slack_webhook, headers=headers, data=payload_json, timeout=3600
"POST", webhook, headers=headers, data=payload_json, timeout=3600
)
print(payload_json)
print({"response": response.text, "response_code": response.status_code})
Expand Down
Empty file added monitor/__init__.py
Empty file.
25 changes: 15 additions & 10 deletions monitor.py → monitor/jobs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import os
import subprocess
from typing import Optional
import os

from dotenv import load_dotenv

from hooks.slack import send_slack_message
from utils.stdout_processing import stdout_to_job_records, JOB_RECORDS
from utils.data_serialization import (
read_json_as_job_records,
write_job_records_to_json,
)
from utils.subprocess_operations import stdout_to_job_records, JOB_RECORDS, get_cmd_stdout

load_dotenv()

JOB_FOLDER = "jobs"
JOB_FILE = "last_updated.json"
JOB_FILE_PATH = os.path.join(JOB_FOLDER, JOB_FILE)
ENABLE_DEBUG_MODE = bool(os.getenv("ENABLE_JOB_DEBUG_MODE"))
SLACK_WEBHOOK = os.getenv("SLACK_JOB_WEBHOOK")


def list_my_job_records() -> JOB_RECORDS:
Expand All @@ -19,10 +26,8 @@ def list_my_job_records() -> JOB_RECORDS:
Returns:
pd.DataFrame: dataframe of current sbatch job details
"""
result = subprocess.run(
["squeue", "--me"], capture_output=True, text=True, check=False
)
return stdout_to_job_records(result.stdout)
stdout: str = get_cmd_stdout("squeue --me")
return stdout_to_job_records(stdout)


def get_last_updated_job_records() -> Optional[JOB_RECORDS]:
Expand All @@ -49,28 +54,28 @@ def monitor_my_jobs():
if current_job_ids != last_updated_job_ids:
new_job_ids = current_job_ids.difference(last_updated_job_ids)
finished_job_ids = last_updated_job_ids.difference(current_job_ids)

if new_job_ids != set():
new_job_records = [
record
for record in current_jobs_records
if record["JOBID"] in new_job_ids
]
data = {"update": "NEW JOBS", "jobs": new_job_records}
send_slack_message(data=data)
send_slack_message(data=data, webhook=SLACK_WEBHOOK)
if finished_job_ids != set():
finished_job_records = [
record
for record in last_updated_job_records
if record["JOBID"] in finished_job_ids
]
data = {"update": "Finished JOBS", "jobs": finished_job_records}
send_slack_message(data=data)
send_slack_message(data=data, webhook=SLACK_WEBHOOK)
write_job_records_to_json(current_jobs_records, JOB_FILE_PATH)
else:
last_updated_job_records = list_my_job_records()
write_job_records_to_json(last_updated_job_records, JOB_FILE_PATH)


if __name__ == "__main__":
monitor_my_jobs()
if not ENABLE_DEBUG_MODE:
monitor_my_jobs()
24 changes: 24 additions & 0 deletions monitor/quota_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from dotenv import load_dotenv
from hooks.slack import send_slack_message
from utils.subprocess_operations import get_cmd_stdout

load_dotenv()

PROJECT_IDs: list[str] = os.getenv("PROJECT_IDS").split(',')
SLACK_WEBHOOK: str = os.getenv("SLACK_QUOTA_WEBHOOK")


def get_fileset_quota(project_id: str) -> str:
cmd = f"rquota | grep ${project_id}"
stdout: str = get_cmd_stdout(cmd)
return stdout


def monitor():
...


if __name__ == "__main__":
for project_id in PROJECT_IDs:
get_fileset_quota(project_id)
2 changes: 1 addition & 1 deletion utils/data_serialization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from .stdout_processing import JOB_RECORDS
from .subprocess_operations import JOB_RECORDS


def write_job_records_to_json(records: JOB_RECORDS, filepath: str):
Expand Down
12 changes: 9 additions & 3 deletions utils/stdout_processing.py → utils/subprocess_operations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import List, Dict
import subprocess

JOB_RECORDS = List[Dict[str, str]]
JOB_RECORDS = list[dict[str, str]]


def strip_spaces(l: List[str]) -> List[str]:
def get_cmd_stdout(command: str) -> str:
return subprocess.run(
command.split(" "), capture_output=True, text=True, check=False
).stdout


def strip_spaces(l: list[str]) -> list[str]:
return [x for x in l if x != ""]


Expand Down

0 comments on commit 5f55721

Please sign in to comment.