File size: 1,142 Bytes
1a92f7e |
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 |
from tortoise import Tortoise
from datetime import datetime, timedelta
from App.Subscriptions.Model import Subscription
import logging
logger = logging.getLogger(__name__)
async def check_expiring_subscriptions():
"""
Background task to check for subscriptions that are about to expire
and update their status accordingly.
"""
try:
# Get the current time
current_time = datetime.now()
# Define the threshold for expiring subscriptions (e.g., 1 hour)
threshold = current_time + timedelta(hours=1)
# Fetch subscriptions that are about to expire
expiring_subscriptions = await Subscription.filter(
active=True, expiration_time__lt=threshold
).all()
for subscription in expiring_subscriptions:
# Deactivate the subscription
subscription.active = False
await subscription.save()
logger.info(
f"Subscription {subscription.id} has been deactivated due to expiration."
)
except Exception as e:
logger.error(f"Error checking expiring subscriptions: {str(e)}")
|