summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Feyens <vfe@odoo.com>2025-01-28 15:56:09 +0000
committerVictor Feyens <vfe@odoo.com>2025-02-07 12:41:00 +0000
commit68c1a13d73176273f54686ebbad2cc9bffb24c86 (patch)
treefa0cc954d2a534cbaf29fe6b6a6007327fa18142
parentd99e44f22634ac589a940d85fab84ca2b2e85332 (diff)
[IMP] payment: disable post-processing cron when no provider enabled
The payment post-processing cron is run every 10 minutes to ensure smooth operations, but waking up crons incurs a non-negligible performance cost. Since the `payment` module is automatically installed with the `account` module, most databases have the `payment` module installed with its cron, even if they didn't enable any provider. This commit disables the cron until a provider is enabled. task-4467217 closes odoo/odoo#196814 X-original-commit: 5104b13448beaf4e08c8aac8fea91a8e6273e203 Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com> Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
-rw-r--r--addons/payment/data/payment_cron.xml1
-rw-r--r--addons/payment/models/payment_provider.py20
-rw-r--r--addons/payment/tests/test_payment_provider.py20
3 files changed, 41 insertions, 0 deletions
diff --git a/addons/payment/data/payment_cron.xml b/addons/payment/data/payment_cron.xml
index 653cf3b9c3b..55806685895 100644
--- a/addons/payment/data/payment_cron.xml
+++ b/addons/payment/data/payment_cron.xml
@@ -9,6 +9,7 @@
<field name="user_id" ref="base.user_root" />
<field name="interval_number">10</field>
<field name="interval_type">minutes</field>
+ <field name="active" eval="False"/>
</record>
</odoo>
diff --git a/addons/payment/models/payment_provider.py b/addons/payment/models/payment_provider.py
index b2da6f08ee7..3d1953777cd 100644
--- a/addons/payment/models/payment_provider.py
+++ b/addons/payment/models/payment_provider.py
@@ -299,6 +299,8 @@ class PaymentProvider(models.Model):
def create(self, values_list):
providers = super().create(values_list)
providers._check_required_if_provider()
+ if any(provider.state != 'disabled' for provider in providers):
+ self._toggle_post_processing_cron()
return providers
def write(self, values):
@@ -320,6 +322,8 @@ class PaymentProvider(models.Model):
deactivated_providers._deactivate_unsupported_payment_methods()
activated_providers._activate_default_pms()
+ if activated_providers or deactivated_providers:
+ self._toggle_post_processing_cron()
return result
@@ -350,6 +354,22 @@ class PaymentProvider(models.Model):
_("The following fields must be filled: %s", ", ".join(field_names))
)
+ def _toggle_post_processing_cron(self):
+ """ Enable the post-processing cron if some providers are enabled; disable it otherwise.
+
+ This allows for saving resources on the cron's wake-up overhead when it has nothing to do.
+
+ :return: None
+ """
+ post_processing_cron = self.env.ref(
+ 'payment.cron_post_process_payment_tx', raise_if_not_found=False
+ )
+ if post_processing_cron:
+ any_active_provider = bool(
+ self.sudo().search_count([('state', '!=', 'disabled')], limit=1)
+ )
+ post_processing_cron.active = any_active_provider
+
def _archive_linked_tokens(self):
""" Archive all the payment tokens linked to the providers.
diff --git a/addons/payment/tests/test_payment_provider.py b/addons/payment/tests/test_payment_provider.py
index db70ddd62c9..7db2871bf83 100644
--- a/addons/payment/tests/test_payment_provider.py
+++ b/addons/payment/tests/test_payment_provider.py
@@ -48,6 +48,26 @@ class TestPaymentProvider(PaymentCommon):
self.provider.state = 'disabled'
self.assertFalse(self.payment_methods.active)
+ def test_enabling_provider_activates_processing_cron(self):
+ """ Test that the post-processing cron is activated when a provider is enabled. """
+ self.env['payment.provider'].search([]).state = 'disabled' # Reset providers' state.
+ post_processing_cron = self.env.ref('payment.cron_post_process_payment_tx')
+ for enabled_state in ('enabled', 'test'):
+ post_processing_cron.active = False # Reset the cron's active field.
+ self.provider.state = 'disabled' # Prepare the dummy provider for enabling.
+ self.provider.state = enabled_state
+ self.assertTrue(post_processing_cron.active)
+
+ def test_disabling_provider_deactivates_processing_cron(self):
+ """ Test that the post-processing cron is deactivated when a provider is disabled. """
+ self.env['payment.provider'].search([]).state = 'disabled' # Reset providers' state.
+ post_processing_cron = self.env.ref('payment.cron_post_process_payment_tx')
+ for enabled_state in ('enabled', 'test'):
+ post_processing_cron.active = True # Reset the cron's active field.
+ self.provider.state = enabled_state # Prepare the dummy provider for disabling.
+ self.provider.state = 'disabled'
+ self.assertFalse(post_processing_cron.active)
+
def test_published_provider_compatible_with_all_users(self):
""" Test that a published provider is always available to all users. """
for user in (self.public_user, self.portal_user):