diff --git a/app/cron_pocketbase.py b/app/cron_pocketbase.py index 102d635..9c5ab65 100644 --- a/app/cron_pocketbase.py +++ b/app/cron_pocketbase.py @@ -1,3 +1,4 @@ +import sys import pytz from datetime import datetime, timedelta from urllib.request import urlopen @@ -115,18 +116,25 @@ async def downgrade_user(): user_data = pb.collection('users').get_full_list() for item in tqdm(user_data): if item.tier not in ['Pro', 'Plus']: - stock_screener_data = pb.collection("stockscreener").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) - for screener in stock_screener_data: - pb.collection('stockscreener').delete(screener.id) + try: + pb.collection("users").update(item.id, { + "credits": 10, + }) - options_watchlist_data = pb.collection("optionsWatchlist").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) - for watchlist in options_watchlist_data: - pb.collection('optionsWatchlist').delete(watchlist.id) + stock_screener_data = pb.collection("stockscreener").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) + for screener in stock_screener_data: + pb.collection('stockscreener').delete(screener.id) + + options_watchlist_data = pb.collection("optionsWatchlist").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) + for watchlist in options_watchlist_data: + pb.collection('optionsWatchlist').delete(watchlist.id) - payment_data = pb.collection("payments").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) - for item in payment_data: - pb.collection('payments').delete(item.id) + payment_data = pb.collection("payments").get_full_list(query_params = {"filter": f"user = '{item.id}'"}) + for item in payment_data: + pb.collection('payments').delete(item.id) + except: + pass async def delete_old_notifications(): @@ -145,13 +153,38 @@ async def delete_old_notifications(): except: pass +async def refresh_bulk_credits(): + user_data = pb.collection('users').get_full_list() + for item in tqdm(user_data): + try: + if item.tier == 'Plus': + pb.collection("users").update(item.id, { + "credits": 500, + }) + elif item.tier == 'Pro': + pb.collection("users").update(item.id, { + "credits": 1000, + }) + + else: + pb.collection("users").update(item.id, { + "credits": 10, + }) + except Exception as e: + print(e) -async def run(): - +async def run_all_except_refresh(): await update_free_trial() await downgrade_user() await delete_old_notifications() -asyncio.run(run()) +def main(): + if '--refresh' in sys.argv: + asyncio.run(refresh_bulk_credits()) + else: + asyncio.run(run_all_except_refresh()) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/app/secondary_cron_job.py b/app/secondary_cron_job.py index cb482d2..447c7f9 100755 --- a/app/secondary_cron_job.py +++ b/app/secondary_cron_job.py @@ -1,20 +1,9 @@ -import pytz -from datetime import datetime, timedelta -from urllib.request import urlopen -import certifi -import json -import ujson import schedule import time import subprocess -import asyncio -import aiohttp +import threading +from datetime import datetime import pytz -import sqlite3 -import pandas as pd -import numpy as np -import threading # Import threading module for parallel execution - berlin_tz = pytz.timezone('Europe/Berlin') @@ -23,43 +12,51 @@ subprocess.run(["timedatectl", "set-timezone", "Europe/Berlin"]) def run_pocketbase(): - # Run the asynchronous function inside an asyncio loop subprocess.run(["python3", "cron_pocketbase.py"]) subprocess.run(["python3", "cron_notification_channel.py"]) - + def run_restart_cache(): - #update db daily + # Update db daily week = datetime.today().weekday() if week <= 5: - subprocess.run(["pm2", "restart","fastapi"]) - subprocess.run(["pm2", "restart","fastify"]) - subprocess.run(["pm2", "restart","websocket"]) + subprocess.run(["pm2", "restart", "fastapi"]) + subprocess.run(["pm2", "restart", "fastify"]) + subprocess.run(["pm2", "restart", "websocket"]) - def run_json_job(): - # Run the asynchronous function inside an asyncio loop subprocess.run(["python3", "restart_json.py"]) - subprocess.run(["pm2", "restart","fastapi"]) - subprocess.run(["pm2", "restart","fastify"]) - subprocess.run(["pm2", "restart","websocket"]) + subprocess.run(["pm2", "restart", "fastapi"]) + subprocess.run(["pm2", "restart", "fastify"]) + subprocess.run(["pm2", "restart", "websocket"]) def run_cron_price_alert(): week = datetime.today().weekday() if week <= 4: subprocess.run(["python3", "cron_price_alert.py"]) -# Create functions to run each schedule in a separate thread +def run_refresh_pocketbase(): + """Runs cron_pocketbase.py with --refresh at the start of each month.""" + today = datetime.now(berlin_tz) + if today.day == 1: # Check if today is the 1st day of the month + subprocess.run(["python3", "cron_pocketbase.py", "--refresh"]) + + +# Run each job in a separate thread def run_threaded(job_func): job_thread = threading.Thread(target=job_func) job_thread.start() +# Existing scheduled tasks schedule.every().day.at("06:30").do(run_threaded, run_pocketbase).tag('pocketbase_job') schedule.every().day.at("15:31").do(run_threaded, run_restart_cache) schedule.every().day.at("23:00").do(run_threaded, run_restart_cache) schedule.every(2).hours.do(run_threaded, run_json_job).tag('json_job') schedule.every(1).minutes.do(run_threaded, run_cron_price_alert).tag('price_alert_job') +schedule.every().day.at("00:00").do(run_threaded, run_refresh_pocketbase) + +# Keep the scheduler running while True: schedule.run_pending() - time.sleep(3) \ No newline at end of file + time.sleep(3)