-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_factory.py
235 lines (196 loc) · 8.92 KB
/
app_factory.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import logging
from datetime import timedelta, timezone
from flask_jwt_extended import JWTManager
from datetime import datetime
from flask import Flask, render_template
from graphene import Schema
from graphql.utils import schema_printer
from src.utils.constants import JWT_SECRET_KEY
from src.database import db_session, init_db
from src.database import Base as db
from src.database import db_url, db_user, db_password, db_name, db_host, db_port
from flask_migrate import Migrate
from src.schema import Query, Mutation
from flasgger import Swagger
from flask_graphql import GraphQLView
from src.models.token_blacklist import TokenBlocklist
# Set up logging at module level
logging.basicConfig(format="%(asctime)s %(levelname)-8s %(message)s", level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S")
logger = logging.getLogger(__name__)
def create_app(run_migrations=False):
"""
Application factory for Flask app.
Args:
run_migrations: If True, configure app for migrations only (no scrapers)
Returns:
Configured Flask application
"""
logger.info("Initializing application")
# Create and configure Flask app
app = Flask(__name__)
app.debug = True
logger.info("Flask app created with debug=%s", app.debug)
# Verify all required database variables are present
if not all([db_user, db_password, db_name, db_host, db_port]):
logger.error("Missing required database configuration variables")
raise ValueError(
"Missing required database configuration. " "Please ensure all database environment variables are set."
)
# Configure database
logger.info("Configuring database connection to %s:%s/%s", db_host, db_port, db_name)
app.config["SQLALCHEMY_DATABASE_URI"] = db_url
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Set up extensions
logger.info("Setting up Flask extensions")
migrate = Migrate(app, db)
schema = Schema(query=Query, mutation=Mutation)
swagger = Swagger(app)
app.config["JWT_SECRET_KEY"] = JWT_SECRET_KEY
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(hours=1)
app.config["JWT_REFRESH_TOKEN_EXPIRES"] = timedelta(days=30)
jwt = JWTManager(app)
@jwt.token_in_blocklist_loader
def check_if_token_revoked(jwt_header, jwt_payload: dict) -> bool:
jti = jwt_payload["jti"]
return db_session.query(TokenBlocklist.id).filter_by(jti=jti).scalar() is not None
# Configure routes
logger.info("Configuring routes")
@app.route("/")
def index():
return render_template("index.html")
app.add_url_rule("/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True))
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
# Initialize database
logger.info("Initializing database")
init_db()
# Create schema.graphql
logger.info("Generating GraphQL schema file")
with open("schema.graphql", "w+") as schema_file:
schema_file.write(schema_printer.print_schema(schema))
schema_file.close()
# Configure and run scrapers if not in migration mode
if not run_migrations:
logger.info("Setting up scrapers and scheduled tasks")
setup_scrapers(app)
else:
logger.info("Running in migration mode - scrapers disabled")
logger.info("Application initialization complete")
return app
def setup_scrapers(app):
"""Set up scrapers and scheduled tasks"""
# Import scraper-related modules only when needed
from flask_apscheduler import APScheduler
from src.scrapers.capacities_scraper import fetch_capacities, update_hourly_capacity
from src.scrapers.reg_hours_scraper import fetch_reg_building, fetch_reg_facility
from src.scrapers.scraper_helpers import clean_past_hours
from src.scrapers.sp_hours_scraper import fetch_sp_facility
from src.scrapers.equipment_scraper import scrape_equipment
from src.scrapers.class_scraper import fetch_classes
from src.scrapers.activities_scraper import fetch_activity
from src.utils.utils import create_gym_table
from src.models.openhours import OpenHours
import os
logger = logging.getLogger(__name__)
logger.info("Beginning scraper configuration")
# Initialize scheduler
scheduler = APScheduler()
logger.info("APScheduler initialized")
# Scrape hours every 15 minutes
@scheduler.task("interval", id="scrape_hours", seconds=900)
def scrape_hours():
job = scheduler.get_job("scrape_hours")
next_run = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") if job and job.next_run_time else "Unknown"
logging.info('Running job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)"', next_run)
try:
logging.info("Scraping hours from sheets...")
# Clear hours
db_session.query(OpenHours).delete()
fetch_reg_facility()
fetch_reg_building()
fetch_sp_facility()
clean_past_hours()
logging.info(
'Job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)" executed successfully', next_run
)
except Exception as e:
logging.error(f"Error in scrape_hours: {e}")
# Scrape capacities every 10 minutes
@scheduler.task("interval", id="scrape_capacities", seconds=600)
def scrape_capacities():
job = scheduler.get_job("scrape_capacities")
next_run = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") if job and job.next_run_time else "Unknown"
logging.info('Running job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)"', next_run)
try:
logging.info("Scraping capacities from C2C...")
fetch_capacities()
logging.info(
'Job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)" executed successfully',
next_run,
)
except Exception as e:
logging.error(f"Error in scrape_capacities: {e}")
# Scrape classes every hour
@scheduler.task("interval", id="scrape_classes", seconds=3600)
def scrape_classes():
job = scheduler.get_job("scrape_classes")
next_run = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") if job and job.next_run_time else "Unknown"
logging.info('Running job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)"', next_run)
try:
logging.info("Scraping classes from group-fitness-classes...")
fetch_classes(10)
logging.info(
'Job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)" executed successfully', next_run
)
except Exception as e:
logging.error(f"Error in scrape_classes: {e}")
@scheduler.task("interval", id="cleanup_expired_tokens", hours=24)
def cleanup_expired_tokens():
logger.info("Deleting expired tokens...")
now = datetime.now(timezone.utc)
db_session.query(TokenBlocklist).filter(TokenBlocklist.expires_at < now).delete()
db_session.commit()
# Update hourly average capacity every hour
@scheduler.task("cron", id="update_capacity", hour="*")
def scheduled_job():
current_time = datetime.now()
current_day = current_time.strftime("%A").upper()
current_hour = current_time.hour
try:
logging.info(f"Updating hourly average capacity for {current_day}, hour {current_hour}...")
update_hourly_capacity(current_day, current_hour)
except Exception as e:
logging.error(f"Error updating hourly average capacity for {current_day}, hour {current_hour}: {e}")
# We're now handling job execution logging within each task function
# Initialize scheduler
logger.info("Starting scheduler")
scheduler.init_app(app)
scheduler.start()
# Run initial scraping
logger.info("Running initial scraping...")
try:
create_gym_table()
logger.info("Gym table created")
logger.info("Scraping classes from group-fitness-classes...")
fetch_classes(10)
logger.info("Initial class scraping complete")
logger.info("Scraping hours from sheets...")
db_session.query(OpenHours).delete()
fetch_reg_facility()
fetch_reg_building()
fetch_sp_facility()
clean_past_hours()
logger.info("Initial hours scraping complete")
logger.info("Scraping capacities from C2C...")
fetch_capacities()
logger.info("Initial capacities scraping complete")
logger.info("Scraping equipment...")
scrape_equipment()
logger.info("Initial equipment scraping complete")
logger.info("Scraping activities from sheets...")
fetch_activity()
logger.info("Initial activities scraping complete")
logger.info("All initial scraping completed successfully")
except Exception as e:
logger.error(f"Error during initial scraping: {e}")