import psycopg2 import time
DB_PARAMS = { "dbname": "scheduler_db", "user": "postgres", "password": "password", "host": "localhost", "port": 5432 }
def run_scheduler(): conn = psycopg2.connect(**DB_PARAMS) cur = conn.cursor() while True: cur.execute(""" SELECT id, task_name FROM events WHERE scheduled_time <= now() AND executed = false; """) tasks = cur.fetchall() for t in tasks: print(f"Executing task: {t[1]}") cur.execute("UPDATE events SET executed = true WHERE id = %s;", (t[0],)) conn.commit() time.sleep(5)
if name == "main": run_scheduler()