Removed debug logs and made all locks private to the module

This commit is contained in:
Shay Nehmad 2019-10-02 16:39:59 +03:00
parent 61a81c2da4
commit b14fd4687c
2 changed files with 7 additions and 20 deletions

View File

@ -1,7 +1,6 @@
import functools import functools
import itertools import itertools
import logging import logging
import time
import ipaddress import ipaddress
from bson import json_util from bson import json_util
@ -692,7 +691,6 @@ class ReportService:
@staticmethod @staticmethod
def generate_report(): def generate_report():
time.sleep(40)
domain_issues = ReportService.get_domain_issues() domain_issues = ReportService.get_domain_issues()
issues = ReportService.get_issues() issues = ReportService.get_issues()
config_users = ReportService.get_config_users() config_users = ReportService.get_config_users()

View File

@ -6,47 +6,37 @@ logger = logging.getLogger(__name__)
# These are pseudo-singletons - global Locks. These locks will allow only one thread to generate a report at a time. # These are pseudo-singletons - global Locks. These locks will allow only one thread to generate a report at a time.
# Report generation can be quite slow if there is a lot of data, and the UI queries the Root service often; without # Report generation can be quite slow if there is a lot of data, and the UI queries the Root service often; without
# the locks, these requests would accumulate, overload the server, eventually causing it to crash. # the locks, these requests would accumulate, overload the server, eventually causing it to crash.
logger.debug("Initializing report generation lock.") logger.debug("Initializing report generation locks.")
report_generating_lock = threading.Semaphore() __report_generating_lock = threading.Semaphore()
__attack_report_generating_lock = threading.Semaphore() __attack_report_generating_lock = threading.Semaphore()
__regular_report_generating_lock = threading.Semaphore() __regular_report_generating_lock = threading.Semaphore()
def safe_generate_reports(): def safe_generate_reports():
# Wait until report generation is available. # Entering the critical section; Wait until report generation is available.
logger.debug("Waiting for report generation...") __report_generating_lock.acquire()
# Entering the critical section.
report_generating_lock.acquire()
logger.debug("Report generation locked.")
report = safe_generate_regular_report() report = safe_generate_regular_report()
attack_report = safe_generate_attack_report() attack_report = safe_generate_attack_report()
# Leaving the critical section. # Leaving the critical section.
report_generating_lock.release() __report_generating_lock.release()
logger.debug("Report generation released.")
return report, attack_report return report, attack_report
def safe_generate_regular_report(): def safe_generate_regular_report():
# Local import to avoid circular imports # Local import to avoid circular imports
from monkey_island.cc.services.reporting.report import ReportService from monkey_island.cc.services.reporting.report import ReportService
logger.debug("Waiting for regular report generation...")
__regular_report_generating_lock.acquire() __regular_report_generating_lock.acquire()
logger.debug("Regular report generation locked.")
report = ReportService.generate_report() report = ReportService.generate_report()
__regular_report_generating_lock.release() __regular_report_generating_lock.release()
logger.debug("Regular report generation released.")
return report return report
def safe_generate_attack_report(): def safe_generate_attack_report():
# Local import to avoid circular imports # Local import to avoid circular imports
from monkey_island.cc.services.attack.attack_report import AttackReportService from monkey_island.cc.services.attack.attack_report import AttackReportService
logger.debug("Waiting for attack report generation...")
__attack_report_generating_lock.acquire() __attack_report_generating_lock.acquire()
logger.debug("Attack report generation locked.")
attack_report = AttackReportService.generate_new_report() attack_report = AttackReportService.generate_new_report()
__attack_report_generating_lock.release() __attack_report_generating_lock.release()
logger.debug("Attack report generation released.")
return attack_report return attack_report
@ -55,9 +45,8 @@ def is_report_being_generated():
# When invoked with blocking set to false, do not block. # When invoked with blocking set to false, do not block.
# If a call without an argument would block, return false immediately; # If a call without an argument would block, return false immediately;
# otherwise, do the same thing as when called without arguments, and return true. # otherwise, do the same thing as when called without arguments, and return true.
is_report_being_generated_right_now = not report_generating_lock.acquire(blocking=False) is_report_being_generated_right_now = not __report_generating_lock.acquire(blocking=False)
logger.debug("is_report_being_generated_right_now == " + str(is_report_being_generated_right_now))
if not is_report_being_generated_right_now: if not is_report_being_generated_right_now:
# We're not using the critical resource; we just checked its state. # We're not using the critical resource; we just checked its state.
report_generating_lock.release() __report_generating_lock.release()
return is_report_being_generated_right_now return is_report_being_generated_right_now