学習が終わったらMattermostで通知がほしい
作成日:2024/08/11 noka.icon
~gpu-notifications でどんなのが出てくるかみれる
欲しい情報
✔log,errorファイルの中身
???
Mattermost の統合機能を使うのが良さそう
を参考に書く.
batchファイルでsingularity実行後に通知を出す.
code:train.batch
log_file="logs/${SLURM_JOB_NAME}-${SLURM_JOB_ID}.log"
err_file="logs/${SLURM_JOB_NAME}-${SLURM_JOB_ID}.err"
date
singularity exec --nv env.sif python3 train.py
date
python3 notifi.py --log "$log_file" --error "$err_file"
code:notifi.py
import requests
import json
import argparse
def send_post_request(message, webhook_url):
headers = {
'Content-Type': 'application/json'
}
try:
response = requests.post(webhook_url, headers=headers, data=json.dumps(message))
if response.status_code == 200:
print({response.status_code})
else:
print( f"{response.status_code}")
print(response.text)
except Exception as e:
print(f"Error: {e}")
def get_detailes(fn,n_lines=10):
with open(fn, 'r') as file:
lines = file.readlines()
last_text = ''.join(last_lines)
return last_text
parser = argparse.ArgumentParser(description="log,error file")
parser.add_argument('--error', type=str, required=True, help="error file path")
parser.add_argument('--log', type=str, required=True, help="log file path")
args = parser.parse_args()
message = {
"icon_emoji": ":fire:",
"text": "@e185730",
"attachments": [
{
"title": "ERROR",
"text": get_detailes(args.error,20),
"color": "#ff3333"
},
{
"title": "LOG",
"text": get_detailes(args.log,20),
"color": "#4169e1"
}
]
}
webhook_url = "webhookのurl"
send_post_request(message, webhook_url)
作成したpythonファイルに実行権限を付与する.
code:.py
chmod +x notifi.py
投稿の様子
長いので折りたたまれてる.