コード例 #1
0
    def runMonitoring(self, jobs=None, steps=1, timeout=300):
        """
        Enable/Run the monitoring loop and wait for the monitoring steps completion.
        Parameters:
          steps:   number of monitoring steps to run
          timeout: how long to wait for monitor steps termination (seconds)
          jobs: a registry slice to be monitored (None -> all jobs), it may be passed by the user so ._impl is stripped if needed
        Return:
          False, if the loop cannot be started or the timeout occured while waiting for monitoring termination
          True, if the monitoring steps were successfully executed  
        Note:         
          This method is meant to be used in Ganga scripts to request monitoring on demand. 
        """

        log.debug("runMonitoring")

        if not isType(steps, int) and steps < 0:
            log.warning("The number of monitor steps should be a positive (non-zero) integer")
            return False

        if not self.alive:
            log.error("Cannot run the monitoring loop. It has already been stopped")
            return False

        # we don not allow the user's request the monitoring loop while the
        # internal services are stopped
        if not Coordinator.servicesEnabled:
            log.error("Cannot run the monitoring loop."
                      "The internal services are disabled (check your credentials or available disk space)")
            return False

        # if the monitoring is disabled (e.g. scripts)
        if not self.enabled:
            # and there are some required cred which are missing
            # (the monitoring loop does not monitor the credentials so we need to check 'by hand' here)
            _missingCreds = get_needed_credentials()
            if _missingCreds:
                log.error("Cannot run the monitoring loop. The following credentials are required: %s" % _missingCreds)
                return False

        #log.debug("jobs: %s" % str(jobs))
        #log.debug("self.__mainLoopCond: %s" % str(self.__mainLoopCond))

        with self.__mainLoopCond:
            log.debug('Monitoring loop lock acquired. Enabling mon loop')
            if self.enabled or self.__isInProgress():
                log.error("The monitoring loop is already running.")
                return False

            if jobs is not None:
                m_jobs = jobs

                # additional check if m_jobs is really a registry slice
                # the underlying code is not prepared to handle correctly the
                # situation if it is not
                from Ganga.GPIDev.Lib.Registry.RegistrySlice import RegistrySlice
                if not isType(m_jobs, RegistrySlice):
                    log.warning('runMonitoring: jobs argument must be a registry slice such as a result of jobs.select() or jobs[i1:i2]')
                    return False

                self.registry_slice = m_jobs
                #log.debug("m_jobs: %s" % str(m_jobs))
                self.makeUpdateJobStatusFunction()

            log.debug("Enable Loop, Clear Iterators and setCallbackHook")
            # enable mon loop
            self.enabled = True
            # set how many steps to run
            self.steps = steps
            # enable job list iterators
            self.stopIter.clear()
            # Start backend update timeout checking.
            self.setCallbackHook(self.updateDict_ts.timeoutCheck, {}, True)

            log.debug("Waking up Main Loop")
            # wake up the mon loop
            self.__mainLoopCond.notifyAll()

        log.debug("Waiting to execute steps")
        # wait to execute the steps
        self.__monStepsTerminatedEvent.wait()
        self.__monStepsTerminatedEvent.clear()

        log.debug("Test for timeout")
        # wait the steps to be executed or timeout to occur
        if not self.__awaitTermination(timeout):
            log.warning("Monitoring loop started but did not complete in the given timeout.")
            # force loops termination
            self.stopIter.set()
            return False
        return True
コード例 #2
0
ファイル: GangaUnitTest.py プロジェクト: wireshark10/ganga
def start_ganga(gangadir_for_test, extra_opts=[], extra_args=None):
    """
    Startup Ganga by calling the same set of 'safe' functions each time
    Args:
        gangadir_for_test (str): This is the directory which the GangaUnitTest is to be run, a new gangadir has been created per test to avoid collisions
        extra_opts (list): A list of tuples which are used to pass command line style options to Ganga
    """

    import Ganga.PACKAGE
    Ganga.PACKAGE.standardSetup()

    # End taken from the ganga binary

    import Ganga.Runtime
    from Ganga.Utility.Config import getConfig
    from Ganga.Utility.logging import getLogger
    logger = getLogger()

    # Start ganga by passing some options for unittesting

    logger.info("Starting ganga")

    logger.info("Parsing Command Line options")
    this_argv = [
        'ganga',  # `argv[0]` is usually the name of the program so fake that here
    ]
    if extra_args:
        this_argv += extra_args

    # These are the default options for all test instances
    # They can be overridden by extra_opts

    lhcb_test = pytest.config.getoption("--testLHCb")

    if lhcb_test:
        import getpass
        cred_opts = [('Configuration', 'user', getpass.getuser()),
                     ('defaults_DiracProxy', 'group', 'lhcb_user')]
    else:
        cred_opts = [('Configuration', 'user', 'testframework'),
                     ('defaults_DiracProxy', 'group', 'gridpp_user'),
                     ('DIRAC', 'DiracEnvSource',
                      '/cvmfs/ganga.cern.ch/dirac_ui/bashrc')]

    #Sort out eos
    outputConfig = getConfig('Output')
    outputConfig['MassStorageFile']['uploadOptions']['cp_cmd'] = 'cp'
    outputConfig['MassStorageFile']['uploadOptions']['ls_cmd'] = 'ls'
    outputConfig['MassStorageFile']['uploadOptions']['mkdir_cmd'] = 'mkdir'
    outputConfig['MassStorageFile']['uploadOptions']['path'] = '/tmp'

    default_opts = [
        ('Configuration', 'RUNTIME_PATH', 'GangaTest'),
        ('Configuration', 'gangadir', gangadir_for_test),
        ('Configuration', 'repositorytype', 'LocalXML'),
        ('Configuration', 'UsageMonitoringMSG', False),  # Turn off spyware
        ('Configuration', 'lockingStrategy', 'FIXED'),
        ('TestingFramework', 'ReleaseTesting', True),
        ('Queues', 'NumWorkerThreads', 3),
        ('Output', 'MassStorageFile', outputConfig['MassStorageFile']),
    ]
    default_opts += cred_opts

    # FIXME Should we need to add the ability to load from a custom .ini file
    # to configure tests without editting this?

    # Actually parse the options
    Ganga.Runtime._prog = Ganga.Runtime.GangaProgram(argv=this_argv)
    Ganga.Runtime._prog.default_config_file = ganga_config_file
    Ganga.Runtime._prog.parseOptions()

    # For all the default and extra options, we set the session value
    from Ganga.Utility.Config import setUserValue

    for opts in default_opts, extra_opts:
        for opt in opts:
            try:
                setUserValue(*opt)
            except Exception as err:
                print("Error Setting: %s" % str(opt))
                print("Err: %s" % err)

    # The configuration is currently created at module import and hence can't be
    # regenerated.
    # The values read in from any .ini file or from command line will change this
    # but the configuration can't be obliterated and re-created. (yet, 16.06.16)

    # Perform the configuration and bootstrap steps in ganga
    logger.info("Parsing Configuration Options")
    Ganga.Runtime._prog.configure()

    logger.info("Initializing")
    Ganga.Runtime._prog.initEnvironment()

    logger.info("Bootstrapping")
    Ganga.Runtime._prog.bootstrap(interactive=False)

    # We need to test if the internal services need to be reinitialized
    from Ganga.Core.InternalServices import Coordinator
    if not Coordinator.servicesEnabled:
        # Start internal services
        logger.info("InternalServices restarting")

        from Ganga.Core.InternalServices.Coordinator import enableInternalServices
        enableInternalServices()
    else:
        logger.info("InternalServices still running")

    # Adapted from the Coordinator class, check for the required credentials and stop if not found
    # Hopefully stops us falling over due to no AFS access of something similar
    from Ganga.GPIDev.Credentials import get_needed_credentials
    missing_cred = get_needed_credentials()

    logger.info("Checking Credentials")

    if missing_cred:
        raise Exception("Failed due to missing credentials %s" % missing_cred)

    # Make sure that all the config options are really set.
    # Some from plugins may not have taken during startup
    for opts in default_opts, extra_opts:
        for opt in opts:
            try:
                setUserValue(*opt)
            except Exception as err:
                print("Error Setting: %s" % str(opt))
                print("Err: %s" % err)

    logger.info("Passing to Unittest")
コード例 #3
0
ファイル: GangaUnitTest.py プロジェクト: ganga-devs/ganga
def start_ganga(gangadir_for_test, extra_opts=[], extra_args=None):
    """
    Startup Ganga by calling the same set of 'safe' functions each time
    Args:
        gangadir_for_test (str): This is the directory which the GangaUnitTest is to be run, a new gangadir has been created per test to avoid collisions
        extra_opts (list): A list of tuples which are used to pass command line style options to Ganga
    """

    import Ganga.PACKAGE
    Ganga.PACKAGE.standardSetup()

    # End taken from the ganga binary

    import Ganga.Runtime
    from Ganga.Utility.logging import getLogger
    logger = getLogger()

    # Start ganga by passing some options for unittesting

    logger.info("Starting ganga")

    logger.info("Parsing Command Line options")
    this_argv = [
        'ganga',  # `argv[0]` is usually the name of the program so fake that here
    ]
    if extra_args:
        this_argv += extra_args

    # These are the default options for all test instances
    # They can be overridden by extra_opts
    default_opts = [
        ('Configuration', 'RUNTIME_PATH', 'GangaTest'),
        ('Configuration', 'gangadir', gangadir_for_test),
        ('Configuration', 'user', 'testframework'),
        ('Configuration', 'repositorytype', 'LocalXML'),
        ('Configuration', 'UsageMonitoringMSG', False),  # Turn off spyware
        ('TestingFramework', 'ReleaseTesting', True),
        ('Queues', 'NumWorkerThreads', 2),
    ]

    # FIXME Should we need to add the ability to load from a custom .ini file
    # to configure tests without editting this?

    # Actually parse the options
    Ganga.Runtime._prog = Ganga.Runtime.GangaProgram(argv=this_argv)
    Ganga.Runtime._prog.default_config_file = ganga_config_file
    Ganga.Runtime._prog.parseOptions()

    # For all the default and extra options, we set the session value
    from Ganga.Utility.Config import setConfigOption
    for opt in default_opts + extra_opts:
        setConfigOption(*opt)

    # The configuration is currently created at module import and hence can't be
    # regenerated.
    # The values read in from any .ini file or from command line will change this
    # but the configuration can't be obliterated and re-created. (yet, 16.06.16)

    # Perform the configuration and bootstrap steps in ganga
    logger.info("Parsing Configuration Options")
    Ganga.Runtime._prog.configure()

    logger.info("Initializing")
    Ganga.Runtime._prog.initEnvironment()

    logger.info("Bootstrapping")
    Ganga.Runtime._prog.bootstrap(interactive=False)

    # We need to test if the internal services need to be reinitialized
    from Ganga.Core.InternalServices import Coordinator
    if not Coordinator.servicesEnabled:
        # Start internal services
        logger.info("InternalServices restarting")

        from Ganga.Core.InternalServices.Coordinator import enableInternalServices
        enableInternalServices()
    else:
        logger.info("InternalServices still running")

    # Adapted from the Coordinator class, check for the required credentials and stop if not found
    # Hopefully stops us falling over due to no AFS access of something similar
    from Ganga.GPIDev.Credentials import get_needed_credentials
    missing_cred = get_needed_credentials()

    logger.info("Checking Credentials")

    if missing_cred:
        raise Exception("Failed due to missing credentials %s" % missing_cred)

    # Make sure that all the config options are really set.
    # Some from plugins may not have taken during startup
    for opt in default_opts + extra_opts:
        setConfigOption(*opt)

    logger.info("Passing to Unittest")