ir_cron_trigger.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from odoo import _, api, models
  3. from odoo.exceptions import ValidationError
  4. class IrCronTrigger(models.Model):
  5. _inherit = 'ir.cron.trigger'
  6. @api.constrains('cron_id')
  7. def _check_image_cron_is_not_already_triggered(self):
  8. """ Ensure that there is a maximum of one trigger at a time for `ir_cron_fetch_image`.
  9. This cron is triggered in an optimal way to retrieve fastly the images without blocking a
  10. worker for a long amount of time. It fetches images in multiples batches to allow other
  11. crons to run in between. The cron also schedules itself if there are remaining products to
  12. be processed or if it encounters errors like a rate limit reached, a ConnectionTimeout, or
  13. service unavailable. Multiple triggers at the same will trouble the rate limit management
  14. and/or errors handling. More information in `product_fetch_image_wizard.py`.
  15. :return: None
  16. :raise ValidationError: If the maximum number of coexisting triggers for
  17. `ir_cron_fetch_image` is reached
  18. """
  19. ir_cron_fetch_image = self.env.ref(
  20. 'product_images.ir_cron_fetch_image', raise_if_not_found=False
  21. )
  22. if ir_cron_fetch_image and self.cron_id.id != ir_cron_fetch_image.id:
  23. return
  24. cron_triggers_count = self.env['ir.cron.trigger'].search_count(
  25. [('cron_id', '=', ir_cron_fetch_image.id)]
  26. )
  27. # When the cron is automatically triggered, we must allow two triggers to exists at the same
  28. # time: the one that triggered the cron and the one that will schedule another cron run. We
  29. # check whether the cron was automatically triggered rather than manually triggered to cover
  30. # the case where the admin would create an ir.cron.trigger manually.
  31. max_coexisting_cron_triggers = 2 if self.env.context.get('automatically_triggered') else 1
  32. if cron_triggers_count > max_coexisting_cron_triggers:
  33. raise ValidationError(_("This action is already scheduled. Please try again later."))