]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding a short sleep after each write action
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 25 May 2023 10:47:49 +0000 (12:47 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 25 May 2023 10:47:49 +0000 (12:47 +0200)
lib/pp_admintools/app/barracuda_sync.py

index e84e497ae94287213c4201d767704cea968cc560..3646c8670641a3d9da557a5c60caf9bcc0a17c3a 100644 (file)
@@ -12,7 +12,8 @@ from __future__ import absolute_import
 # import copy
 import logging
 import re
-# import sys
+import time
+import sys
 from functools import cmp_to_key
 from pathlib import Path
 
@@ -30,7 +31,7 @@ from ldap3 import MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE
 from .ldap import BaseLdapApplication
 from ..xlate import XLATOR
 
-__version__ = '0.9.0'
+__version__ = '0.9.2'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -97,6 +98,9 @@ class BarracudaSyncApp(BaseLdapApplication):
     apply_default_ldap_instance_if_not_given = True
     default_default_ldap_instance = 'dpx-prod-mail'
 
+    default_wait_interval = 0.1
+    max_wait_interval = 10
+
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, base_dir=None):
         """Construct the application object."""
@@ -107,6 +111,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         self.postmap_command = Path('/sbin') / self.default_postmap_command
 
         self.postfix_origin = self.default_origin
+        self.wait_interval = self.default_wait_interval
 
         self.lookup_table_types = []
         self.existing_aliases = {}
@@ -140,6 +145,13 @@ class BarracudaSyncApp(BaseLdapApplication):
                 'should be located. Default: {!r}.').format(self.default_barracuda_base_dn),
         )
 
+        sync_group.add_argument(
+            '-W', '--wait', dest='wait', metavar=_('SECONDS'), type=float,
+            help=_(
+                'The time in seconds to wait after each write action to not overload '
+                'the LDAP servers. Default: {} seconds.').format(self.default_wait_interval),
+        )
+
         super(BarracudaSyncApp, self).init_arg_parser()
 
     # -------------------------------------------------------------------------
@@ -155,6 +167,18 @@ class BarracudaSyncApp(BaseLdapApplication):
         if self.args.base_dn and self.args.base_dn.strip():
             self.barracuda_base_dn = self.args.base_dn.strip()
 
+        if self.args.wait is not None:
+            wait = self.args.wait
+            if wait < 0 or wait > self.max_wait_interval:
+                msg = _(
+                    'Invalid value for wait ({v} seconds) given, it must not be less than '
+                    'zero or greater {m} seconds.').format(v=wait, m=self.max_wait_interval)
+                LOG.error(msg)
+                self.empty_line()
+                self.arg_parser.print_usage(sys.stdout)
+                self.exit(1)
+            self.wait_interval = wait
+
         self._check_postfix_commands()
         self._check_postfix_table_types()
         self._get_postfix_default_db_type()
@@ -823,7 +847,9 @@ class BarracudaSyncApp(BaseLdapApplication):
             dn = 'cn=' + cn + ',' + self.barracuda_base_dn
             (oclasses, attributes) = self._create_ldap_entry_for_create(cn)
             LOG.info(_('Creating LDAP alias {a!r} => {dn!r}.').format(a=cn, dn=dn))
-            # self.add_entry(inst, dn, oclasses, attributes)
+            self.add_entry(inst, dn, oclasses, attributes)
+            if self.wait_interval:
+                time.sleep(self.wait_interval)
 
     # -------------------------------------------------------------------------
     def _run(self):