Skip to content
Snippets Groups Projects
Commit f6475275 authored by snd's avatar snd Committed by Nicolas Martinelli
Browse files

[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#45017

X-original-commit: 2f21def6
Signed-off-by: default avatarNicolas Martinelli (nim) <nim@odoo.com>
Co-authored-by: default avatarNicolas Martinelli <nim@odoo.com>
parent f7771474
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,13 @@
<field name="name">Google Calendar: synchronization</field>
<field name="model_id" ref="model_google_calendar"/>
<field name="state">code</field>
<field name="code">model.synchronize_events_cron()</field>
<field name="code">
# 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()
</field>
<field name="user_id" ref="base.user_root" />
<field name="interval_number">12</field>
<field name="interval_type">hours</field>
......@@ -13,4 +19,4 @@
<field eval="False" name="doall" />
</record>
</data>
</odoo>
\ No newline at end of file
</odoo>
......@@ -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__)
......@@ -551,7 +552,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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment