Files
ihrm/outlook_auth/scheduler.py

42 lines
991 B
Python

"""
outlook_auth/scheduler.py
"""
import sys, logging
from apscheduler.schedulers.background import BackgroundScheduler
logger = logging.getLogger(__name__)
def refresh_outlook_auth_token():
"""
scheduler method to refresh token
"""
from outlook_auth.views import refresh_outlook_token
from outlook_auth.models import AzureApi
apis = AzureApi.objects.filter(token__isnull=False)
for api in apis:
try:
refresh_outlook_token(api)
logger.info(f"Updated token for {api} outlook auth")
print(f"Updated token for {api} outlook auth")
except Exception as e:
logger.error(e)
if not any(
cmd in sys.argv
for cmd in ["makemigrations", "migrate", "compilemessages", "flush", "shell"]
):
scheduler = BackgroundScheduler()
scheduler.add_job(
refresh_outlook_auth_token,
"interval",
minutes=30,
id="refresh_outlook_auth_token",
)
scheduler.start()