From f7eb653e7a4f801ad038ba7658913e13aeff6ee7 Mon Sep 17 00:00:00 2001 From: snd Date: Mon, 10 Feb 2020 13:05:39 +0000 Subject: [PATCH] [FIX] google_calendar: synchronization cron timeout In a database with many users / events to synchronize, the cron `ir_cron_sync_all_cals` may time out. Consequently, even if a `commit` is performed for each user, some users are never synchronized. In this commit, we first sort the users by last synchronization date, meaning that all users will ultimately be synchronized, even if the cron times out. On top of that, we introduce the context key `last_sync_hours` to prevent the synchronization in case the cron is restarted after timeout. opw-2158372 closes odoo/odoo#45032 X-original-commit: 2f21def63c61324e2286dbee6abead091171a041 Signed-off-by: Nicolas Martinelli (nim) Co-authored-by: Nicolas Martinelli --- addons/google_calendar/data/google_calendar_data.xml | 10 ++++++++-- addons/google_calendar/models/google_calendar.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/addons/google_calendar/data/google_calendar_data.xml b/addons/google_calendar/data/google_calendar_data.xml index 42b583cf7da..4fe2a9ee3e5 100644 --- a/addons/google_calendar/data/google_calendar_data.xml +++ b/addons/google_calendar/data/google_calendar_data.xml @@ -5,7 +5,13 @@ Google Calendar: synchronization code - model.synchronize_events_cron() + +# The context key 'last_sync_hours' allows specifying the minimum delay between consecutive syncs. +# Indeed, in case there are many users / events to sync, the cron might time out. In this case, the +# solution is to force the last_sync_hours to the expected synchronization interval expected. This +# will avoid the synchronization of users that succeeded in the previous failing cron. +model.synchronize_events_cron() + 12 hours @@ -13,4 +19,4 @@ - \ No newline at end of file + diff --git a/addons/google_calendar/models/google_calendar.py b/addons/google_calendar/models/google_calendar.py index 528c5e61702..45c673986d1 100644 --- a/addons/google_calendar/models/google_calendar.py +++ b/addons/google_calendar/models/google_calendar.py @@ -12,6 +12,7 @@ import pytz from werkzeug import urls from odoo import api, fields, models, tools, _ +from odoo.osv import expression from odoo.tools import exception_to_unicode _logger = logging.getLogger(__name__) @@ -550,7 +551,15 @@ class GoogleCalendar(models.AbstractModel): @api.model def synchronize_events_cron(self): """ Call by the cron. """ - users = self.env['res.users'].search([('google_calendar_last_sync_date', '!=', False)]) + domain = [('google_calendar_last_sync_date', '!=', False)] + if self.env.context.get('last_sync_hours'): + last_sync_hours = self.env.context['last_sync_hours'] + last_sync_date = datetime.now() - timedelta(hours=last_sync_hours) + domain = expression.AND([ + domain, + [('google_calendar_last_sync_date', '<=', fields.Datetime.to_string(last_sync_date))] + ]) + users = self.env['res.users'].search(domain, order='google_calendar_last_sync_date') _logger.info("Calendar Synchro - Started by cron") for user_to_sync in users.ids: