Skip to content

Commit

Permalink
Merge pull request #8 from Tch1b0/add-cpu-job
Browse files Browse the repository at this point in the history
add cpu job
  • Loading branch information
Tch1b0 authored Aug 1, 2024
2 parents 78b4102 + d0b5eb7 commit 837698e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docker~=6.0.0
python-telegram-bot~=20.0a2
python-dotenv~=0.20.0
python-dotenv~=0.20.0
psutil~=6.0.0
21 changes: 20 additions & 1 deletion src/register_jobs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import logging
import os
import psutil
import docker
from dotenv import load_dotenv
from src.core.utility import exec_sh, time_in_seconds

from src.core.utility import exec_sh, time_in_seconds
from src.core.jobs import Jobs
from src.core.metrics import Metrics
from src.core.telegrambot import TelegramBot
Expand All @@ -28,6 +29,9 @@
)
bot.informants.append(
lambda: f"Running Containers: {metrics.running_container_count}")
bot.informants.append(
lambda: f"Current CPU usage: {psutil.cpu_percent() * 100:.2f}%"
)


@jobs.register(time_in_seconds(hours=1))
Expand Down Expand Up @@ -70,3 +74,18 @@ async def update_packages():
async def resend_status_message():
await bot.stop()
await bot.start()

@jobs.register(time_in_seconds(seconds=30), run_initially=True)
# JOB: check CPU usage
async def check_cpu_usage(cpu_high=[False]):
cpu_usage = psutil.cpu_percent()
if cpu_usage > 0.8:
if not cpu_high[0]:
cpu_high[0] = True
return
decision = bot.decide("CPU has been over 80%% for at least 1 minute", ["ignore", "reboot", "shutdown"])
match decision:
case 2:
os.system("shutdown now -h")
case 3:
os.system("reboot")

0 comments on commit 837698e

Please sign in to comment.