Googolplexic commited on
Commit
826d4db
·
1 Parent(s): 79b56df

Ported db cleanup

Browse files
README.md CHANGED
@@ -36,3 +36,38 @@ user flow ish
36
  3. attempts to grab info for api
37
  4. add said info to database
38
  5. make sure that this schedule is robust hopefully
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  3. attempts to grab info for api
37
  4. add said info to database
38
  5. make sure that this schedule is robust hopefully
39
+
40
+ ## Deployment to Tailscale Server
41
+
42
+ 1. Update the server configuration in `deploy.sh`:
43
+
44
+ ```bash
45
+ SERVER_IP="your-tailscale-server-ip"
46
+ SERVER_USER="your-username"
47
+ ```
48
+
49
+ 2. Make the deploy script executable:
50
+
51
+ ```bash
52
+ chmod +x deploy.sh
53
+ ```
54
+
55
+ 3. Run the deployment:
56
+
57
+ ```bash
58
+ ./deploy.sh
59
+ ```
60
+
61
+ 4. On the server, set up the daily cron job:
62
+
63
+ ```bash
64
+ ./setup-cron.sh
65
+ ```
66
+
67
+ The application will now run continuously and execute daily at 2 AM.
68
+
69
+ ## Server Management
70
+
71
+ - Check service status: `sudo systemctl status mcp-hackathon`
72
+ - View logs: `sudo tail -f /var/log/mcp-hackathon.log`
73
+ - Restart service: `sudo systemctl restart mcp-hackathon`
database.py DELETED
File without changes
db-cleanup/Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Copy requirements and install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy the cleanup script
10
+ COPY cleanup.py .
11
+
12
+ # Run the cleanup job
13
+ CMD ["python", "cleanup.py"]
db-cleanup/cleanup.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import psycopg2
3
+ import psycopg2.extras
4
+ from dotenv import load_dotenv
5
+ from apscheduler.schedulers.blocking import BlockingScheduler
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv(os.path.join(os.path.dirname(__file__), "..", ".env"))
9
+
10
+
11
+ def connect_to_db():
12
+ """
13
+ Connect to the PostgreSQL database using environment variables.
14
+ Returns a connection object.
15
+ """
16
+ db_password = os.getenv("DB_PASSWORD")
17
+ if not db_password:
18
+ raise ValueError(
19
+ "Database password not found in environment variables. Please set DB_PASSWORD."
20
+ )
21
+
22
+ # Get database connection details from environment variables with defaults
23
+ db_host = os.getenv("DB_HOST")
24
+ db_port = int(os.getenv("DB_PORT"))
25
+ db_name = os.getenv("DB_NAME")
26
+ db_user = os.getenv("DB_USER")
27
+
28
+ return psycopg2.connect(
29
+ host=db_host,
30
+ port=db_port,
31
+ database=db_name,
32
+ user=db_user,
33
+ password=db_password,
34
+ cursor_factory=psycopg2.extras.DictCursor,
35
+ )
36
+
37
+
38
+ def cleanup_old_configurations():
39
+ cleanup_situations = [
40
+ """
41
+ DELETE FROM api_configurations
42
+ WHERE stop_at IS NOT NULL
43
+ AND stop_at < NOW() - INTERVAL '14 days';
44
+ """,
45
+ ]
46
+
47
+ conn = None
48
+ try:
49
+ conn = connect_to_db()
50
+ with conn.cursor() as cur:
51
+ for raw_sql in cleanup_situations:
52
+ sql = raw_sql.strip()
53
+ if not sql:
54
+ continue
55
+
56
+ cur.execute(sql)
57
+ deleted = cur.rowcount
58
+ print(f"[CLEANUP] {deleted} rows deleted.")
59
+ conn.commit()
60
+ print("[SUCCESS] Database cleanup completed successfully")
61
+
62
+ except Exception as e:
63
+ print(f"[ERROR] cleanup failed: {e}")
64
+
65
+ finally:
66
+ if conn:
67
+ conn.close()
68
+
69
+
70
+ def job_schedule():
71
+ sched = BlockingScheduler()
72
+ sched.add_job(cleanup_old_configurations, "cron", hour=0, minute=0)
73
+ print("cleanup job scheduled at 00:00 UTC")
74
+ sched.start()
75
+
76
+
77
+ if __name__ == "__main__":
78
+ print("[INFO] Starting database cleanup scheduler...")
79
+ job_schedule()
db-cleanup/docker-compose.yml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ db-cleanup:
5
+ build: .
6
+ environment:
7
+ - DB_PASSWORD=${DB_PASSWORD}
8
+ - DB_HOST=${DB_HOST}
9
+ - DB_PORT=${DB_PORT}
10
+ - DB_NAME=${DB_NAME}
11
+ - DB_USER=${DB_USER}
12
+ restart: unless-stopped
13
+ volumes:
14
+ - ../logs:/app/logs
db-cleanup/requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ psycopg2-binary==2.9.7
2
+ python-dotenv==1.0.0
3
+ apscheduler==3.10.4