Skip to content

Commit 7a72bcc

Browse files
authored
process ldap and oncall syncs concurrently (#677)
1 parent 53d7300 commit 7a72bcc

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

src/iris/bin/sync_targets.py

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
'ldap_memberships_removed': 0,
4848
'ldap_lists_failed_to_add': 0,
4949
'ldap_memberships_failed_to_add': 0,
50-
'ldap_reconnects': 0
50+
'ldap_reconnects': 0,
51+
'failed_tasks': 0
5152
}
5253

5354
# logging
@@ -736,6 +737,34 @@ def sync_ldap_lists(ldap_settings, engine):
736737
session.close()
737738

738739

740+
def oncall_sync_loop(config, engine, interval):
741+
742+
while True:
743+
logger.info('Starting oncall sync...')
744+
try:
745+
run_start = time.time()
746+
sync_from_oncall(config, engine)
747+
logger.info('oncall sync took %.2f seconds', time.time() - run_start)
748+
except Exception:
749+
metrics.incr('failed_tasks')
750+
logger.exception('Error syncing from oncall!')
751+
sleep(interval)
752+
753+
754+
def ldap_sync_loop(ldap_lists, engine, interval):
755+
sleep(60)
756+
while True:
757+
logger.info('Starting ldap sync...')
758+
try:
759+
run_start = time.time()
760+
sync_ldap_lists(ldap_lists, engine)
761+
logger.info('Ldap mailing list sync took %.2f seconds', time.time() - run_start)
762+
except Exception:
763+
metrics.incr('failed_tasks')
764+
logger.exception('Error syncing from ldap!')
765+
sleep(interval)
766+
767+
739768
def main():
740769
global ldap_timeout
741770
global ldap_pagination_size
@@ -746,15 +775,18 @@ def main():
746775
default_ldap_timeout = 60
747776
default_ldap_pagination_size = 400
748777
default_update_sleep = 0
749-
default_nap_time = 3600
778+
default_ldap_nap_time = 3600
779+
default_oncall_nap_time = 60
750780

751781
ldap_timeout = int(config.get('sync_script_ldap_timeout', default_ldap_timeout))
752782
ldap_pagination_size = int(config.get('sync_script_ldap_pagination_size', default_ldap_pagination_size))
753783
update_sleep = float(config.get('target_update_pause', default_update_sleep))
754784
try:
755-
nap_time = int(config.get('sync_script_nap_time', default_nap_time))
785+
ldap_nap_time = int(config.get('sync_script_ldap_nap_time', default_ldap_nap_time))
786+
oncall_nap_time = int(config.get('sync_script_oncall_nap_time', default_oncall_nap_time))
756787
except ValueError:
757-
nap_time = default_nap_time
788+
ldap_nap_time = default_ldap_nap_time
789+
oncall_nap_time = default_oncall_nap_time
758790

759791
# check if we are using special connection settings for this script
760792
if config.get('db_target_sync'):
@@ -777,36 +809,36 @@ def main():
777809
metrics.set('ldap_memberships_found', 0)
778810

779811
metrics_task = spawn(metrics.emit_forever)
812+
oncall_task = spawn(oncall_sync_loop, config, engine, oncall_nap_time)
813+
814+
if ldap_lists:
815+
if 'ldap_cert_path' in ldap_lists:
816+
ldap_cert_path = ldap_lists['ldap_cert_path']
817+
if not os.access(ldap_cert_path, os.R_OK):
818+
logger.error("Failed to read ldap_cert_path certificate")
819+
raise IOError
820+
else:
821+
ldap_lists['cert_path'] = ldap_cert_path
822+
ldap_task = spawn(ldap_sync_loop, ldap_lists, engine, ldap_nap_time)
780823

781824
while True:
782825
if not bool(metrics_task):
826+
metrics.incr('failed_tasks')
783827
logger.error('metrics task failed, %s', metrics_task.exception)
784-
metrics_task = spawn(metrics.emit_forever)
785-
try:
786-
sync_from_oncall(config, engine)
787-
except Exception:
788-
logger.exception('Error syncing from oncall!')
828+
spawn(metrics.emit_forever)
789829

790-
# Do ldap mailing list sync *after* we do the normal sync, to ensure we have the users
791-
# which will be in ldap already populated.
792-
if ldap_lists:
830+
if not bool(oncall_task):
831+
metrics.incr('failed_tasks')
832+
logger.error('oncall task failed, %s', oncall_task.exception)
833+
metrics_task = spawn(oncall_sync_loop, config, engine, oncall_nap_time)
793834

794-
if 'ldap_cert_path' in ldap_lists:
795-
ldap_cert_path = ldap_lists['ldap_cert_path']
796-
if not os.access(ldap_cert_path, os.R_OK):
797-
logger.error("Failed to read ldap_cert_path certificate")
798-
raise IOError
799-
else:
800-
ldap_lists['cert_path'] = ldap_cert_path
801-
try:
802-
list_run_start = time.time()
803-
sync_ldap_lists(ldap_lists, engine)
804-
logger.info('Ldap mailing list sync took %.2f seconds', time.time() - list_run_start)
805-
except Exception:
806-
logger.exception('Error syncing from ldap!')
835+
if ldap_lists:
836+
if not bool(ldap_task):
837+
metrics.incr('failed_tasks')
838+
logger.error('ldap task failed, %s', ldap_task.exception)
839+
ldap_task = spawn(ldap_sync_loop, ldap_lists, engine, ldap_nap_time)
807840

808-
logger.info('Sleeping for %d seconds' % nap_time)
809-
sleep(nap_time)
841+
sleep(10)
810842

811843

812844
if __name__ == '__main__':

0 commit comments

Comments
 (0)