[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) <nim@odoo.com>
Co-authored-by: Nicolas Martinelli <nim@odoo.com>
This commit is contained in:
snd
2020-02-10 17:38:28 +00:00
committed by Nicolas Martinelli
co-authored by Nicolas Martinelli
parent 7d254d3e36
commit f7eb653e7a
2 changed files with 18 additions and 3 deletions
@@ -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>
</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__)
@@ -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: