Execute rule checks in multi-threads for each service

This commit is contained in:
EC2 Default User 2024-08-14 04:23:10 +00:00
parent f7923b4890
commit 6e04f6a7a9

28
main.py
View File

@ -1,9 +1,10 @@
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from InquirerLib import prompt from InquirerLib import prompt
from InquirerLib.InquirerPy.utils import InquirerPyKeybindings from InquirerLib.InquirerPy.utils import InquirerPyKeybindings
from InquirerLib.InquirerPy.base import Choice from InquirerLib.InquirerPy.base import Choice
from colorama import Style, Fore from colorama import Style, Fore
from datetime import datetime
from importlib import import_module
from utils import * from utils import *
import services import services
@ -36,17 +37,25 @@ def ask_services_to_enable(bp):
def perform_bp_rules_check(bp): def perform_bp_rules_check(bp):
for service_name, service in bp.items(): with ThreadPoolExecutor() as executor:
futures = [
executor.submit(_rule_check, service_name, service)
for service_name, service in bp.items()
]
[future.result() for future in futures]
return bp
def _rule_check(service_name, service):
now = datetime.now()
if not service["enabled"]: if not service["enabled"]:
continue return
if service_name == "Lambda": if service_name == "Lambda":
service_name = "_lambda" service_name = "_lambda"
now = datetime.now() rule_checker = getattr(services, convert_snake_case(service_name)).rule_checker()
rule_checker = getattr(
services, convert_snake_case(service_name)
).rule_checker()
for rule_name, rule in service["rules"].items(): for rule_name, rule in service["rules"].items():
if not rule["enabled"]: if not rule["enabled"]:
continue continue
@ -54,7 +63,6 @@ def perform_bp_rules_check(bp):
elapsed_time = datetime.now() - now elapsed_time = datetime.now() - now
print(convert_snake_case(service_name), elapsed_time.total_seconds()) print(convert_snake_case(service_name), elapsed_time.total_seconds())
return bp
def show_bp_result(bp): def show_bp_result(bp):