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: