Various fixes
This commit is contained in:
@@ -79,11 +79,20 @@ def create_gcal_service_object():
|
||||
return None
|
||||
|
||||
|
||||
def _invalidate_service_on_error(exc):
|
||||
"""Reset the cached service object so the next call retries credential loading."""
|
||||
global _service_object
|
||||
logger.warning(f"Invalidating cached GCal service due to error: {exc}")
|
||||
_service_object = None
|
||||
|
||||
|
||||
def get_service_object():
|
||||
"""Get or create the Google Calendar service object."""
|
||||
global _service_object
|
||||
if _service_object is None:
|
||||
_service_object = create_gcal_service_object()
|
||||
if _service_object is None:
|
||||
logger.error("Failed to create Google Calendar service object")
|
||||
return _service_object
|
||||
|
||||
|
||||
@@ -93,6 +102,12 @@ def reset_service_object():
|
||||
_service_object = None
|
||||
|
||||
|
||||
def get_service_object_fresh():
|
||||
"""Force-create a new service object, bypassing and replacing the cache."""
|
||||
reset_service_object()
|
||||
return get_service_object()
|
||||
|
||||
|
||||
# --------------------- Building GCal event representation ------------------------------------
|
||||
|
||||
|
||||
@@ -303,6 +318,7 @@ def delete_all_gcal_events(service=None):
|
||||
batch.execute()
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting GCal events: {e}")
|
||||
_invalidate_service_on_error(e)
|
||||
|
||||
GCalMapping.objects.all().delete()
|
||||
|
||||
@@ -389,6 +405,7 @@ def sync_from_local_to_google(service=None):
|
||||
batch.execute()
|
||||
except Exception as e:
|
||||
logger.error(f"Error executing batch request: {e}")
|
||||
_invalidate_service_on_error(e)
|
||||
|
||||
return len(events_to_create_django_id), len(events_to_delete_google_id)
|
||||
|
||||
@@ -485,33 +502,39 @@ def check_gcal_subscription(
|
||||
|
||||
callback_url = settings.GCAL_COUPLING["push_url"]
|
||||
|
||||
try:
|
||||
db_channel = GCalPushChannel.objects.get(address=callback_url)
|
||||
channels = GCalPushChannel.objects.filter(address=callback_url)
|
||||
|
||||
cur_time = int(time.time() * 1000)
|
||||
if channels.count() > 1:
|
||||
logger.warning(
|
||||
f"Multiple GCal channels found for {callback_url}. Stopping all and creating fresh one."
|
||||
)
|
||||
for ch in channels:
|
||||
ch.stop(service)
|
||||
channels = GCalPushChannel.objects.none()
|
||||
|
||||
if db_channel.expiration > cur_time:
|
||||
# not yet expired
|
||||
if cur_time + renew_before_expiry * 1000 > db_channel.expiration:
|
||||
# will expire in less than "renew_before_expiry"
|
||||
logger.info(f"Renewing Google Calendar Subscription: {callback_url}")
|
||||
db_channel.stop(service)
|
||||
GCalPushChannel.create_new(callback_url, service, time_to_live)
|
||||
else:
|
||||
logger.info(f"Channel active until {db_channel.expiration}")
|
||||
else:
|
||||
logger.info(
|
||||
"Google calendar subscription had expired - getting new subscription"
|
||||
)
|
||||
# to get back in sync again we have to decide which data to take
|
||||
# so we use the local data as reference
|
||||
sync_from_local_to_google(service)
|
||||
GCalPushChannel.create_new(callback_url, service, time_to_live)
|
||||
db_channel = channels.first()
|
||||
|
||||
except GCalPushChannel.DoesNotExist:
|
||||
if db_channel is None:
|
||||
logger.info(f"No GCalCallback Channel exists yet for: {callback_url}")
|
||||
# to get back in sync again we have to decide which data to take
|
||||
# so we use the local data as reference
|
||||
sync_from_local_to_google(service)
|
||||
GCalPushChannel.create_new(callback_url, service, time_to_live)
|
||||
return
|
||||
|
||||
cur_time = int(time.time() * 1000)
|
||||
|
||||
if db_channel.expiration > cur_time:
|
||||
# not yet expired
|
||||
if cur_time + renew_before_expiry * 1000 > db_channel.expiration:
|
||||
# will expire in less than "renew_before_expiry"
|
||||
logger.info(f"Renewing Google Calendar Subscription: {callback_url}")
|
||||
db_channel.stop(service)
|
||||
GCalPushChannel.create_new(callback_url, service, time_to_live)
|
||||
else:
|
||||
logger.info(f"Channel active until {db_channel.expiration}")
|
||||
else:
|
||||
logger.info(
|
||||
"Google calendar subscription had expired - getting new subscription"
|
||||
)
|
||||
sync_from_local_to_google(service)
|
||||
GCalPushChannel.create_new(callback_url, service, time_to_live)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user