This commit is contained in:
2025-05-30 17:31:59 -04:00
parent 5c85411c69
commit f2c2b69d63
9 changed files with 38 additions and 41 deletions

View File

@ -22,16 +22,6 @@ class BaseScheduler:
*args,
**kwargs
) -> None:
"""Schedule a task to run at regular intervals or at specific times using APScheduler
Args:
task_name: Name of the task
func: Function to execute
interval_seconds: Interval in seconds for periodic execution (mutually exclusive with cron_expression)
cron_expression: Cron expression for time-based scheduling (mutually exclusive with interval_seconds)
*args: Additional positional arguments for the function
**kwargs: Additional keyword arguments for the function
"""
if task_name in self.jobs:
logger.warning(f"Task {task_name} already exists. Removing existing job.")
self.jobs[task_name].remove()
@ -47,20 +37,22 @@ class BaseScheduler:
trigger = CronTrigger.from_crontab(cron_expression)
job = self.scheduler.add_job(
func,
func=func,
trigger=trigger,
args=args,
kwargs=kwargs,
id=task_name,
replace_existing=True
)
self.jobs[task_name] = job
if interval_seconds:
logger.info(f"Scheduled task {task_name} to run every {interval_seconds} seconds")
else:
logger.info(f"Scheduled task {task_name} with cron expression: {cron_expression}")
def remove_task(self, task_name: str) -> None:
"""Remove a scheduled task"""
if task_name in self.jobs:

View File

@ -74,20 +74,23 @@ class SchedulerService(BaseService):
# Schedule open orders update to run hourly at 00 minutes
await self.scheduler.schedule_task(
task_name="update_open_orders_hourly",
func=lambda: self.update_open_orders_hourly(db),
cron_expression="0 * * * *" # Run at minute 0 of every hour
func=self.update_open_orders_hourly,
cron_expression="10 * * * *", # Run at minute 10 of every hour
db=db
)
# Schedule all orders update to run daily at 3 AM
await self.scheduler.schedule_task(
task_name="update_all_orders_daily",
func=lambda: self.update_all_orders_daily(db),
cron_expression="0 3 * * *" # Run at 3:00 AM every day
func=self.update_all_orders_daily,
cron_expression="0 3 * * *", # Run at 3:00 AM every day
db=db
)
# Schedule TCGPlayer inventory refresh to run every 3 hours
await self.scheduler.schedule_task(
task_name="refresh_tcgplayer_inventory_table",
func=lambda: self.refresh_tcgplayer_inventory_table(db),
cron_expression="21 */3 * * *" # Run at minute 0 of every 3rd hour
func=self.refresh_tcgplayer_inventory_table,
cron_expression="28 */3 * * *", # Run at minute 28 of every 3rd hour
db=db
)
self.scheduler.start()