-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsgi.py
More file actions
50 lines (41 loc) · 1.89 KB
/
Copy pathwsgi.py
File metadata and controls
50 lines (41 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from app import app # Import your Flask app
import threading
import time
from datetime import datetime
import subprocess
import logging
import sys
import os
# Configure logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s")
def run_scraper_periodically():
"""Function to run the scraper periodically."""
first_run_done = False # Flag to ensure the first run happens only once
while True:
now = datetime.now()
# Run scraper once immediately after deployment
if not first_run_done:
logging.info("Running scraper for the first time...")
try:
venv_python = sys.executable
subprocess.run([venv_python, "main.py"], check=True)
logging.info("Initial scraper run completed successfully.")
first_run_done = True # Set flag to avoid running again
except subprocess.CalledProcessError as e:
logging.error(f"Error while running scraper on first run: {e}")
# Run daily at midnight
if now.hour == 0 and now.minute == 0: # Run daily at midnight
logging.info("Running scraper at scheduled time (testing)...")
try:
subprocess.run([venv_python, "main.py"], check=True)
logging.info("Scraper run completed successfully.")
except subprocess.CalledProcessError as e:
logging.error(f"Error while running scraper: {e}")
time.sleep(60) # Sleep for a minute to avoid multiple triggers
time.sleep(30) # Check every 30 seconds if it's time to run
# Start the scheduler thread when the app starts
threading.Thread(target=run_scraper_periodically, daemon=True).start()
if __name__ == "__main__":
from waitress import serve
logging.info("Starting the Flask application with Waitress...")
serve(app, host="0.0.0.0", port=8080)