test: add unit tests for scheduled Bitcoin price job handler
This commit is contained in:
parent
cd2285395d
commit
9db43c474e
2 changed files with 121 additions and 21 deletions
|
|
@ -98,6 +98,36 @@ def register_job_handlers(qm: QueueManager, db_pool: asyncpg.Pool) -> None:
|
|||
await process_random_number_job(job, db_pool)
|
||||
|
||||
|
||||
async def process_bitcoin_price_job(db_pool: asyncpg.Pool) -> None:
|
||||
"""
|
||||
Fetch and store Bitcoin price from Bitfinex.
|
||||
|
||||
This function is designed to fail silently - exceptions are caught and logged
|
||||
so the scheduler can continue with the next scheduled run.
|
||||
"""
|
||||
try:
|
||||
price, timestamp = await fetch_btc_eur_price()
|
||||
|
||||
async with db_pool.acquire() as conn:
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO price_history
|
||||
(source, pair, price, timestamp, created_at)
|
||||
VALUES ($1, $2, $3, $4, NOW())
|
||||
ON CONFLICT (source, pair, timestamp) DO NOTHING
|
||||
""",
|
||||
"bitfinex",
|
||||
"BTC/EUR",
|
||||
price,
|
||||
timestamp,
|
||||
)
|
||||
|
||||
logger.info(f"Fetched BTC/EUR price: €{price:.2f}")
|
||||
except Exception as e:
|
||||
# Fail silently - next scheduled job will continue
|
||||
logger.error(f"Failed to fetch Bitcoin price: {e}")
|
||||
|
||||
|
||||
def register_scheduled_jobs(sm: SchedulerManager, db_pool: asyncpg.Pool) -> None:
|
||||
"""Register all scheduled jobs with the scheduler manager."""
|
||||
|
||||
|
|
@ -105,27 +135,7 @@ def register_scheduled_jobs(sm: SchedulerManager, db_pool: asyncpg.Pool) -> None
|
|||
@sm.schedule(JOB_FETCH_BITCOIN_PRICE, "* * * * *")
|
||||
async def fetch_bitcoin_price(schedule: Schedule) -> None:
|
||||
"""Fetch Bitcoin price from Bitfinex every minute."""
|
||||
try:
|
||||
price, timestamp = await fetch_btc_eur_price()
|
||||
|
||||
async with db_pool.acquire() as conn:
|
||||
await conn.execute(
|
||||
"""
|
||||
INSERT INTO price_history
|
||||
(source, pair, price, timestamp, created_at)
|
||||
VALUES ($1, $2, $3, $4, NOW())
|
||||
ON CONFLICT (source, pair, timestamp) DO NOTHING
|
||||
""",
|
||||
"bitfinex",
|
||||
"BTC/EUR",
|
||||
price,
|
||||
timestamp,
|
||||
)
|
||||
|
||||
logger.info(f"Fetched BTC/EUR price: €{price:.2f}")
|
||||
except Exception as e:
|
||||
# Fail silently - next scheduled job will continue
|
||||
logger.error(f"Failed to fetch Bitcoin price: {e}")
|
||||
await process_bitcoin_price_job(db_pool)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue