1
1
import logging
2
+ from datetime import datetime
2
3
from flask import Flask , render_template
3
4
from graphene import Schema
4
5
from graphql .utils import schema_printer
11
12
from flask_graphql import GraphQLView
12
13
13
14
# Set up logging at module level
14
- logging .basicConfig (format = "%(asctime)s %(levelname)-8s %(message)s" ,
15
- level = logging .INFO ,
16
- datefmt = "%Y-%m-%d %H:%M:%S" )
15
+ logging .basicConfig (format = "%(asctime)s %(levelname)-8s %(message)s" , level = logging .INFO , datefmt = "%Y-%m-%d %H:%M:%S" )
17
16
logger = logging .getLogger (__name__ )
18
17
18
+
19
19
def create_app (run_migrations = False ):
20
20
"""
21
21
Application factory for Flask app.
@@ -37,14 +37,13 @@ def create_app(run_migrations=False):
37
37
if not all ([db_user , db_password , db_name , db_host , db_port ]):
38
38
logger .error ("Missing required database configuration variables" )
39
39
raise ValueError (
40
- "Missing required database configuration. "
41
- "Please ensure all database environment variables are set."
40
+ "Missing required database configuration. " "Please ensure all database environment variables are set."
42
41
)
43
42
44
43
# Configure database
45
44
logger .info ("Configuring database connection to %s:%s/%s" , db_host , db_port , db_name )
46
- app .config [' SQLALCHEMY_DATABASE_URI' ] = db_url
47
- app .config [' SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
45
+ app .config [" SQLALCHEMY_DATABASE_URI" ] = db_url
46
+ app .config [" SQLALCHEMY_TRACK_MODIFICATIONS" ] = False
48
47
49
48
# Set up extensions
50
49
logger .info ("Setting up Flask extensions" )
@@ -85,11 +84,12 @@ def shutdown_session(exception=None):
85
84
logger .info ("Application initialization complete" )
86
85
return app
87
86
87
+
88
88
def setup_scrapers (app ):
89
89
"""Set up scrapers and scheduled tasks"""
90
90
# Import scraper-related modules only when needed
91
91
from flask_apscheduler import APScheduler
92
- from src .scrapers .capacities_scraper import fetch_capacities
92
+ from src .scrapers .capacities_scraper import fetch_capacities , update_hourly_capacity
93
93
from src .scrapers .reg_hours_scraper import fetch_reg_building , fetch_reg_facility
94
94
from src .scrapers .scraper_helpers import clean_past_hours
95
95
from src .scrapers .sp_hours_scraper import fetch_sp_facility
@@ -110,9 +110,9 @@ def setup_scrapers(app):
110
110
# Scrape hours every 15 minutes
111
111
@scheduler .task ("interval" , id = "scrape_hours" , seconds = 900 )
112
112
def scrape_hours ():
113
- job = scheduler .get_job (' scrape_hours' )
114
- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
115
- logging .info (" Running job \ " scrape_hours (trigger: interval[0:15:00], next run at: %s EST)\" " , next_run )
113
+ job = scheduler .get_job (" scrape_hours" )
114
+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
115
+ logging .info (' Running job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)"' , next_run )
116
116
try :
117
117
logging .info ("Scraping hours from sheets..." )
118
118
# Clear hours
@@ -122,38 +122,54 @@ def scrape_hours():
122
122
fetch_sp_facility ()
123
123
clean_past_hours ()
124
124
logging .info (
125
- "Job \" scrape_hours (trigger: interval[0:15:00], next run at: %s EST)\" executed successfully" , next_run )
125
+ 'Job "scrape_hours (trigger: interval[0:15:00], next run at: %s EST)" executed successfully' , next_run
126
+ )
126
127
except Exception as e :
127
128
logging .error (f"Error in scrape_hours: { e } " )
128
129
129
130
# Scrape capacities every 10 minutes
130
131
@scheduler .task ("interval" , id = "scrape_capacities" , seconds = 600 )
131
132
def scrape_capacities ():
132
- job = scheduler .get_job (' scrape_capacities' )
133
- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
134
- logging .info (" Running job \ " scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)\" " , next_run )
133
+ job = scheduler .get_job (" scrape_capacities" )
134
+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
135
+ logging .info (' Running job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)"' , next_run )
135
136
try :
136
137
logging .info ("Scraping capacities from C2C..." )
137
138
fetch_capacities ()
138
139
logging .info (
139
- "Job \" scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)\" executed successfully" , next_run )
140
+ 'Job "scrape_capacities (trigger: interval[0:10:00], next run at: %s EST)" executed successfully' ,
141
+ next_run ,
142
+ )
140
143
except Exception as e :
141
144
logging .error (f"Error in scrape_capacities: { e } " )
142
145
143
146
# Scrape classes every hour
144
147
@scheduler .task ("interval" , id = "scrape_classes" , seconds = 3600 )
145
148
def scrape_classes ():
146
- job = scheduler .get_job (' scrape_classes' )
147
- next_run = job .next_run_time .strftime (' %Y-%m-%d %H:%M:%S' ) if job and job .next_run_time else "Unknown"
148
- logging .info (" Running job \ " scrape_classes (trigger: interval[1:00:00], next run at: %s EST)\" " , next_run )
149
+ job = scheduler .get_job (" scrape_classes" )
150
+ next_run = job .next_run_time .strftime (" %Y-%m-%d %H:%M:%S" ) if job and job .next_run_time else "Unknown"
151
+ logging .info (' Running job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)"' , next_run )
149
152
try :
150
153
logging .info ("Scraping classes from group-fitness-classes..." )
151
154
fetch_classes (10 )
152
155
logging .info (
153
- "Job \" scrape_classes (trigger: interval[1:00:00], next run at: %s EST)\" executed successfully" , next_run )
156
+ 'Job "scrape_classes (trigger: interval[1:00:00], next run at: %s EST)" executed successfully' , next_run
157
+ )
154
158
except Exception as e :
155
159
logging .error (f"Error in scrape_classes: { e } " )
156
160
161
+ # Update hourly average capacity every hour
162
+ @scheduler .task ("cron" , id = "update_capacity" , hour = "*" )
163
+ def scheduled_job ():
164
+ current_time = datetime .now ()
165
+ current_day = current_time .strftime ("%A" ).upper ()
166
+ current_hour = current_time .hour
167
+ try :
168
+ logging .info (f"Updating hourly average capacity for { current_day } , hour { current_hour } ..." )
169
+ update_hourly_capacity (current_day , current_hour )
170
+ except Exception as e :
171
+ logging .error (f"Error updating hourly average capacity for { current_day } , hour { current_hour } : { e } " )
172
+
157
173
# We're now handling job execution logging within each task function
158
174
159
175
# Initialize scheduler
0 commit comments