Skip to content

Commit

Permalink
feat: variables to set jobs interval
Browse files Browse the repository at this point in the history
JOB_CORE_HEALTH_CHECK_INTERVAL
JOB_RECORD_NODE_USAGES_INTERVAL
JOB_RECORD_USER_USAGES_INTERVAL
JOB_REVIEW_USERS_INTERVAL
JOB_SEND_NOTIFICATIONS_INTERVAL
  • Loading branch information
SaintShit committed Aug 5, 2024
1 parent cea7f2f commit 2d50bf9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ UVICORN_PORT = 8000

# VITE_BASE_API="https://example.com/api/"
# JWT_ACCESS_TOKEN_EXPIRE_MINUTES = 1440

# JOB_CORE_HEALTH_CHECK_INTERVAL = 10
# JOB_RECORD_NODE_USAGES_INTERVAL = 30
# JOB_RECORD_USER_USAGES_INTERVAL = 10
# JOB_REVIEW_USERS_INTERVAL = 10
# JOB_SEND_NOTIFICATIONS_INTERVAL = 30
5 changes: 4 additions & 1 deletion app/jobs/0_xray_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from app import app, logger, scheduler, xray
from app.db import GetDB, crud
from app.models.node import NodeStatus
from config import JOB_CORE_HEALTH_CHECK_INTERVAL
from xray_api import exc as xray_exc


Expand Down Expand Up @@ -59,7 +60,9 @@ def start_core():
for node_id in node_ids:
xray.operations.connect_node(node_id, config)

scheduler.add_job(core_health_check, 'interval', seconds=10, coalesce=True, max_instances=1)
scheduler.add_job(core_health_check, 'interval',
seconds=JOB_CORE_HEALTH_CHECK_INTERVAL,
coalesce=True, max_instances=1)


@app.on_event("shutdown")
Expand Down
12 changes: 9 additions & 3 deletions app/jobs/record_usages.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from app import scheduler, xray
from app.db import GetDB
from app.db.models import NodeUsage, NodeUserUsage, System, User
from config import DISABLE_RECORDING_NODE_USAGE
from config import (DISABLE_RECORDING_NODE_USAGE,
JOB_RECORD_NODE_USAGES_INTERVAL,
JOB_RECORD_USER_USAGES_INTERVAL)
from xray_api import XRay as XRayAPI
from xray_api import exc as xray_exc

Expand Down Expand Up @@ -195,5 +197,9 @@ def record_node_usages():
record_node_stats(params, node_id)


scheduler.add_job(record_user_usages, 'interval', coalesce=True, seconds=30, max_instances=1)
scheduler.add_job(record_node_usages, 'interval', coalesce=True, seconds=10, max_instances=1)
scheduler.add_job(record_user_usages, 'interval',
seconds=JOB_RECORD_NODE_USAGES_INTERVAL,
coalesce=True, max_instances=1)
scheduler.add_job(record_node_usages, 'interval',
seconds=JOB_RECORD_USER_USAGES_INTERVAL,
coalesce=True, max_instances=1)
17 changes: 9 additions & 8 deletions app/jobs/review_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
start_user_expire, update_user_status)
from app.models.user import ReminderType, UserResponse, UserStatus
from app.utils import report
from app.utils.concurrency import GetBG
from app.utils.helpers import (calculate_expiration_days,
calculate_usage_percent)
from config import (NOTIFY_DAYS_LEFT, NOTIFY_REACHED_USAGE_PERCENT,
WEBHOOK_ADDRESS)
from config import (JOB_REVIEW_USERS_INTERVAL, NOTIFY_DAYS_LEFT,
NOTIFY_REACHED_USAGE_PERCENT, WEBHOOK_ADDRESS)

if TYPE_CHECKING:
from app.db.models import User
Expand All @@ -38,7 +37,7 @@ def add_notification_reminders(db: Session, user: "User", now: datetime = dateti
def review():
now = datetime.utcnow()
now_ts = now.timestamp()
with GetDB() as db, GetBG() as bg:
with GetDB() as db:
for user in get_users(db, status=UserStatus.active):

limited = user.data_limit and user.used_traffic >= user.data_limit
Expand All @@ -56,7 +55,7 @@ def review():
update_user_status(db, user, status)

report.status_change(username=user.username, status=status,
user=UserResponse.from_orm(user), user_admin=user.admin)
user=UserResponse.from_orm(user), user_admin=user.admin)

logger.info(f"User \"{user.username}\" status changed to {status}")

Expand All @@ -80,11 +79,13 @@ def review():

update_user_status(db, user, status)
start_user_expire(db, user)

report.status_change(username=user.username, status=status,
user=UserResponse.from_orm(user), user_admin=user.admin)
user=UserResponse.from_orm(user), user_admin=user.admin)

logger.info(f"User \"{user.username}\" status changed to {status}")


scheduler.add_job(review, 'interval', seconds=10, coalesce=True, max_instances=1)
scheduler.add_job(review, 'interval',
seconds=JOB_REVIEW_USERS_INTERVAL,
coalesce=True, max_instances=1)
12 changes: 8 additions & 4 deletions app/jobs/send_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from fastapi.encoders import jsonable_encoder
from requests import Session

from config import (WEBHOOK_SECRET, WEBHOOK_ADDRESS, NUMBER_OF_RECURRENT_NOTIFICATIONS,
RECURRENT_NOTIFICATIONS_TIMEOUT)
from app import app, logger, scheduler
from app.db import GetDB
from app.db.models import NotificationReminder
from app.utils.notification import queue
from config import (JOB_SEND_NOTIFICATIONS_INTERVAL,
NUMBER_OF_RECURRENT_NOTIFICATIONS,
RECURRENT_NOTIFICATIONS_TIMEOUT, WEBHOOK_ADDRESS,
WEBHOOK_SECRET)

session = Session()

Expand All @@ -37,7 +39,7 @@ def send(data: List[Dict[Any, Any]]) -> bool:
return False


def send_req(w_address:str, data):
def send_req(w_address: str, data):
try:
logger.debug(f"Sending {len(data)} webhook updates to {w_address}")
r = session.post(w_address, json=data, headers=headers)
Expand Down Expand Up @@ -90,5 +92,7 @@ def app_shutdown():
send_notifications()

logger.info("Send webhook job started")
scheduler.add_job(send_notifications, "interval", seconds=30, replace_existing=True)
scheduler.add_job(send_notifications, "interval",
seconds=JOB_SEND_NOTIFICATIONS_INTERVAL,
replace_existing=True)
scheduler.add_job(delete_expired_reminders, "interval", hours=2, start_date=dt.utcnow() + td(minutes=1))
8 changes: 8 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@

# discord webhook log
DISCORD_WEBHOOK_URL = config("DISCORD_WEBHOOK_URL", default="")


# Interval jobs, all values are in seconds
JOB_CORE_HEALTH_CHECK_INTERVAL = config("JOB_CORE_HEALTH_CHECK_INTERVAL", cast=int, default=10)
JOB_RECORD_NODE_USAGES_INTERVAL = config("JOB_RECORD_NODE_USAGES_INTERVAL", cast=int, default=30)
JOB_RECORD_USER_USAGES_INTERVAL = config("JOB_RECORD_USER_USAGES_INTERVAL", cast=int, default=10)
JOB_REVIEW_USERS_INTERVAL = config("JOB_REVIEW_USERS_INTERVAL", cast=int, default=10)
JOB_SEND_NOTIFICATIONS_INTERVAL = config("JOB_SEND_NOTIFICATIONS_INTERVAL", cast=int, default=30)

0 comments on commit 2d50bf9

Please sign in to comment.