示例#1
0
    def __init__(self, file_name, max_queue_size=100):
        """Automatically starts the thread.

        Args:
            file_name: The name of the file.
            max_queue_size: The max queries that will be queued.
        """
        self.logger = get_logger(self.__class__.__qualname__)
        threading.Thread.__init__(self)
        self.daemon = True
        self.sqlite3_conn = apsw.Connection(file_name)

        # Configures the connection always to return a dictionary
        # instead of a tuple
        def row_factory(cursor, row):
            return {
                k[0]: row[i]
                for i, k in enumerate(cursor.getdescription())
            }

        self.sqlite3_conn.setrowtrace(row_factory)
        self.sqlite3_cursor = self.sqlite3_conn.cursor()
        self.sql_queue = Queue.Queue(maxsize=max_queue_size)
        self.results = {}
        self.max_queue_size = max_queue_size
        self.exit_set = False
        # Token that is put into queue when close() is called.
        self.exit_token = str(uuid.uuid4())
        self.start()
        self.thread_running = True
    def __init__(self,
                 config,
                 target_function,
                 thread_name,
                 start_with_call=True):
        """
        Builds an abstract thread object from the given input parameters.
        """
        # No need to pass the target function as this point, as this
        # class is abstract by design. Instead, save the reference internally
        # to allow subclasses to invoke the target function when overriding
        # the run() method
        Thread.__init__(self, name=thread_name)

        # Private attributes not to be accessed directly, but
        # through their corresponding properties
        self.__logger = get_logger(self.__class__.__qualname__)
        self.__config = config

        # Protected attributes to be accessed by child subclasses only
        self._target_function = target_function
        self._start_with_call = start_with_call

        # Protected attributes to be accessed by child subclases and
        # whose state can be probed by others (i.e., there is a corresponding
        # property)
        self._exec = False
示例#3
0
    def __init__(self,
                 wrappers_dir,
                 analyzer_name,
                 args,
                 storage_dir,
                 timeout_sec,
                 prefetch=True):
        self.__logger = get_logger(self.__class__.__qualname__)
        self.__analyzer_name = analyzer_name

        self.__home = "{0}/{1}".format(wrappers_dir, analyzer_name)
        self.__args = args

        if not dir_exists(storage_dir):
            os.makedirs(storage_dir)

        self.__storage_dir = storage_dir
        self.__timeout_sec = timeout_sec

        metadata_script = "{0}/metadata".format(self.home)
        run_script = "{0}/run".format(self.home)
        pull_script = "{0}/pull_analyzer".format(self.home)
        Wrapper.__check_for_executable_script(run_script)
        Wrapper.__check_for_executable_script(metadata_script)
        Wrapper.__check_for_executable_script(pull_script)

        self.__metadata_script = metadata_script
        self.__run_script = run_script
        self.__pull_script = pull_script

        # Prefetch the configured analyzer image. If the prefetching fails,
        # an exception is thrown, the program exits and the auto-restart feature kicks in.
        if prefetch:
            self.__prefetch_image()
示例#4
0
 def test_configure_logging_no_stream(self):
     # Since streaming is set to False, this will not invoke logger creating and will pass
     streaming_provider_name = "Nonsense"
     # The logger is not initialized proper
     streaming_provider_args = {
         'log_group': 'grp',
         'log_stream': 'stream',
         'send_interval_seconds': 10
     }
     config = {
         'is_enabled': False,
         'provider': streaming_provider_name,
         'args': streaming_provider_args
     }
     log_streaming.initialize("test-account", config, force=True)
     self.assertEqual(get_loggers(), {})
     log_streaming.get_logger("test_get_logger")
     # No configuration should happen
     self.assertFalse("test_get_logger" in get_loggers())
示例#5
0
 def test_configure_logging_stream_provider(self):
     streaming_provider_name = "CloudWatchProvider"
     # The logger is not initialized proper
     streaming_provider_args = {
         'log_group': 'grp',
         'log_stream': 'stream',
         'send_interval_seconds': 10
     }
     config = {
         'is_enabled': True,
         'provider': streaming_provider_name,
         'args': streaming_provider_args
     }
     log_streaming.initialize("test-account", config, force=True)
     logger = log_streaming.get_logger("test_get_logger")
     self.assertTrue(logger is log_streaming.get_logger("test_get_logger"))
     self.assertTrue("test_get_logger" in get_loggers())
     # Reset logging after this call
     log_streaming.initialize("account", {'is_enabled': False}, force=True)
示例#6
0
    def __init__(self, config):
        """
        Builds a QSPAuditNode object from the given input parameters.
        """
        self.__exec = False
        self.__config = config
        self.__is_initialized = False
        self.__logger = get_logger(self.__class__.__qualname__)

        self.__internal_threads = [
            ComputeGasPriceThread(config),
            UpdateMinPriceThread(config),
            ClaimRewardsThread(config),
            PollRequestsThread(config),
            PerformAuditThread(config),
            SubmitReportThread(config),
            MonitorSubmissionThread(config)
        ]

        if config.metric_collection_is_enabled:
            self.__internal_threads.append(CollectMetricsThread(config))
示例#7
0
 def __init__(self, node_version):
     self.__logger = get_logger(self.__class__.__qualname__)
     self.__node_version = node_version
示例#8
0
####################################################################################################
#                                                                                                  #
# (c) 2018, 2019 Quantstamp, Inc. This content and its use are governed by the license terms at    #
# <https://s3.amazonaws.com/qsp-protocol-license/V2_LICENSE.txt>                                   #
#                                                                                                  #
####################################################################################################

import signal
import sys

from log_streaming import get_logger

logger = get_logger(__name__)


class Stop:
    """
    A class providing a stop notification mechanism for all registered
    stoppable objects (a stoppable object is one prodiving a stop
    function), as well as an exiting mechanism for the executing process to
    terminate altogether.
    """
    __objects = []

    @classmethod
    def stop_all(cls):
        """
        Stops all registered stoppable objects.
        """
        for stoppable in Stop.__objects:
            stoppable.stop()
示例#9
0
 def __init__(self, config):
     self.__logger = get_logger(self.__class__.__qualname__)
     self.__config = config
     self.__process_identifier = "{0}-{1}".format(socket.gethostname(), os.getpid())
示例#10
0
 def __init__(self, wrapper):
     """
     Builds an Analyzer object from a given arguments string.
     """
     self.__wrapper = wrapper
     self.__logger = get_logger(self.__class__.__qualname__)
示例#11
0
 def __init__(self):
     self.__logger = get_logger(self.__class__.__qualname__)
     self.__vulnerability_types, self.__vulnerability_types_inverted = \
         ReportEncoder.__initialize_vulnerability_types()