示例#1
0
 def cleanup(self):
     logger.info('Cleaning up %s of process %s now', self, os.getpid())
     try:
         from pytest_cov.embed import cleanup
         cleanup()
     except ImportError:  # pragma: no cover
         pass
示例#2
0
    def run(self, q):
        # Block SIGTERM and SIGINT to allow graceful shutdown and cleanup after initialization
        signal.pthread_sigmask(signal.SIG_BLOCK,
                               [signal.SIGTERM, signal.SIGINT])

        with ExitStack() as stack:
            try:
                server = stack.enter_context(self._create_server())
                port = server.add_insecure_port("127.0.0.1:0")
                server.start()
            except Exception:
                q.put(None)
                raise

            # Send port to parent
            q.put(port)

            # Sleep until termination by signal
            signal.sigwait([signal.SIGTERM, signal.SIGINT])

            server.stop(0)

        # Save collected coverage data
        try:
            from pytest_cov.embed import cleanup
        except ImportError:
            pass
        else:
            cleanup()
示例#3
0
def finish_request_cov(self, request, client_address):
    cov = None
    if pid != os.getpid():
        cov = embed.init()
    try:
        return finish_request(self, request, client_address)
    finally:
        if cov:
            embed.cleanup(cov)
示例#4
0
def finish_request_cov(self, request, client_address):
    cov = None
    if pid != os.getpid():
        cov = embed.init()
    try:
        return finish_request(self, request, client_address)
    finally:
        if cov:
            embed.cleanup(cov)
示例#5
0
    def stop():
        if p is None:
            return

        from pytest_cov.embed import cleanup
        cleanup()

        for sig in [signal.SIGTERM, signal.SIGINT]:
            os.kill(p.pid, sig)
            try:
                p.join(4)
            except:
                pass
            else:
                break
        else:
            p.terminate()
            p.join()
示例#6
0
 def terminate_subprocess(self, sig, frame):  # pragma: no cover
     cleanup()
     sys.exit(sig)
示例#7
0
def main():
    p = argparse.ArgumentParser()
    p.add_argument(
        "--log-file",
        metavar="PATH",
        help="path to the log file (default: %(default)s)",
        default=None,
    )
    p.add_argument(
        "--pid-file",
        metavar="PATH",
        help="path to the PID file (default: %(default)s)",
        default=None,
    )
    p.add_argument(
        "--port",
        metavar="PORT",
        type=int,
        help="listening port (default: %(default)d)",
        default=9999,
    )
    p.add_argument("--read-only",
                   action="store_true",
                   help="disable updates",
                   default=False)
    p.add_argument(
        "--config-file",
        metavar="PATH",
        help="path to the config file (default: %(default)s)",
        default="servicelib.yaml",
    )

    args = p.parse_args()

    cmd = [
        "uwsgi",
        "--die-on-term",
        "--enable-threads",
        "--http-socket",
        ":{}".format(args.port),
        "--manage-script-name",
        "--master",
        "--module",
        "servicelib.config.wsgi",
        "--need-app",
        "--processes",
        "1",
        "--req-logger",
        "file:/dev/null",
        "--threads",
        "1",
    ]

    if args.pid_file is not None:
        cmd.extend([
            "--safe-pidfile",
            args.pid_file,
            "--daemonize",
            args.log_file is not None and args.log_file or "/dev/null",
        ])

    os.environ.setdefault(
        *env_var("SERVICELIB_CONFIG_FILE", os.path.abspath(args.config_file)))
    os.environ.setdefault(*env_var("SERVICELIB_CONFIG_SERVER_READ_ONLY",
                                   "true" if args.read_only else "false"))

    # If we're running under `pytest-cov`, call `pytest_cov.embed.cleanup()`
    # before exec of uWSGI, so that we do not lose coverage info for this
    # Python module.
    if os.environ.get("COV_CORE_DATAFILE"):
        from pytest_cov.embed import cleanup

        cleanup()

    os.execlp(cmd[0], *cmd[0:])
示例#8
0
def main(protocol="HTTP/1.0"):
    global GPU_STATS
    """Test the HTTP request handler class.

    This runs an HTTP server on port 8000 (or the first command line
    argument).

    """
    from optparse import OptionParser

    op = OptionParser(usage='usage: %s [options]' % sys.argv[0])

    #NOTE - currently squatting on port 15348 for testing - TODO can we use an ephemeral port?
    op.add_option(
        '-p',
        '--port',
        dest='port',
        default=config.get('dataserver-port', 15348),
        help=
        "port number to serve on (default: 15348, see also 'dataserver-port' config entry)"
    )
    op.add_option('-t',
                  '--test',
                  dest='test',
                  help="Set up for bandwidth test (don't save files)",
                  action="store_true",
                  default=False)
    op.add_option('-v',
                  '--protocol',
                  dest='protocol',
                  help="HTTP protocol version",
                  default="1.1")
    op.add_option('-l',
                  '--log-requests',
                  dest='log_requests',
                  help="Display http request info",
                  default=False,
                  action="store_true")
    default_root = config.get('dataserver-root', os.curdir)
    op.add_option(
        '-r',
        '--root',
        dest='root',
        help=
        "Root directory of virtual filesystem (default %s, see also 'dataserver-root' config entry)"
        % dataserver_root,
        default=default_root)
    op.add_option('-k',
                  '--profile',
                  dest='profile',
                  help="Enable profiling",
                  default=False,
                  action="store_true")
    op.add_option('--thread-profile',
                  dest='thread_profile',
                  help="Enable thread profiling",
                  default=False,
                  action="store_true")
    default_server_filter = config.get('dataserver-filter', compName)
    op.add_option(
        '-f',
        '--server-filter',
        dest='server_filter',
        help='Add a serverfilter for distinguishing between different clusters',
        default=default_server_filter)
    op.add_option(
        '--timeout-test',
        dest='timeout_test',
        help=
        'deliberately make requests timeout for testing error handling in calling modules',
        default=0)
    op.add_option('-a',
                  '--advertisements',
                  dest='advertisements',
                  choices=['zeroconf', 'local'],
                  default='zeroconf',
                  help='Optionally restrict advertisements to local machine')

    options, args = op.parse_args()
    if options.profile:
        from PYME.util import mProfile
        mProfile.profileOn(['HTTPDataServer.py', 'clusterListing.py'])

        profileOutDir = options.root + '/LOGS/%s/mProf' % compName

    if options.thread_profile:
        from PYME.util import fProfile

        tp = fProfile.ThreadProfiler()
        #tp.profile_on(subs=['PYME/', 'http/server', 'socketserver'],outfile=options.root + '/LOGS/%s/tProf/dataserver.txt' % compName)
        tp.profile_on(subs=[
            'PYME/',
        ],
                      outfile=options.root +
                      '/LOGS/%s/tProf/dataserver.txt' % compName)

    # setup logging to file
    log_dir = '%s/LOGS/%s' % (options.root, compName)
    makedirs_safe(log_dir)

    log_file = '%s/LOGS/%s/PYMEDataServer.log' % (options.root, compName)
    fh = logging.handlers.RotatingFileHandler(filename=log_file,
                                              mode='w',
                                              maxBytes=1e6,
                                              backupCount=1)
    logger.addHandler(fh)

    logger.info(
        '========================================\nPYMEDataServer, running on python %s\n'
        % sys.version)

    #change to the dataserver root if given'
    logger.info('Serving from directory: %s' % options.root)
    os.chdir(options.root)

    if options.advertisements == 'local':
        # preference is to avoid zeroconf on clusterofone due to poor
        # performance on crowded networks
        if config.get('clusterIO-hybridns', True):
            ns = sqlite_ns.getNS('_pyme-http')
        else:
            # if we aren't using the hybridns, we are using zeroconf in clusterIO
            # TODO - warn that we might run into performance issues???
            ns = pzc.getNS('_pyme-http')
        server_address = ('127.0.0.1', int(options.port))
        ip_addr = '127.0.0.1'
    else:
        #default
        ns = pzc.getNS('_pyme-http')
        server_address = ('', int(options.port))

        try:
            ip_addr = socket.gethostbyname(socket.gethostname())
        except:
            ip_addr = socket.gethostbyname(socket.gethostname() + '.local')

    PYMEHTTPRequestHandler.protocol_version = 'HTTP/%s' % options.protocol
    PYMEHTTPRequestHandler.bandwidthTesting = options.test
    PYMEHTTPRequestHandler.timeoutTesting = options.timeout_test
    PYMEHTTPRequestHandler.logrequests = options.log_requests

    httpd = ThreadedHTTPServer(server_address, PYMEHTTPRequestHandler)
    #httpd = http.server.HTTPServer(server_address, PYMEHTTPRequestHandler)
    httpd.daemon_threads = True

    #get the actual adress (port) we bound to
    sa = httpd.socket.getsockname()
    service_name = get_service_name('PYMEDataServer [%s]' %
                                    options.server_filter)
    ns.register_service(service_name, ip_addr, sa[1])

    status['IPAddress'] = ip_addr
    status['BindAddress'] = server_address
    status['Port'] = sa[1]
    status['Protocol'] = options.protocol
    status['TestMode'] = options.test
    status['ComputerName'] = GetComputerName()

    if GPU_STATS:
        try:
            pynvml.nvmlInit()
        except:
            GPU_STATS = False

    sp = statusPoller()
    sp.start()

    logger.info("Serving HTTP on %s port %d ..." % (ip_addr, sa[1]))
    try:
        httpd.serve_forever()
    finally:
        logger.info('Shutting down ...')
        httpd.shutdown()
        httpd.server_close()

        ns.unregister(service_name)

        if options.profile:
            mProfile.report(display=False, profiledir=profileOutDir)

        if options.thread_profile:
            tp.profile_off()

        sp.stop()

        if GPU_STATS:
            pynvml.nvmlShutdown()

        try:
            from pytest_cov.embed import cleanup
            cleanup()
        except:
            pass

        sys.exit()
def handle_termination_signal(signum, frame):
    try:
        cleanup()
    finally:
        sys.exit(0)
示例#10
0
import logging
import os
import sys
import time

from stampede import StampedeWorker

try:
    from pytest_cov.embed import cleanup
    os._exit = lambda code, original=os._exit: cleanup() or original(code)
except ImportError:
    pass


PATH = '/tmp/stampede-tests'


class MockedStampedeWorker(StampedeWorker):
    alarm_time = 1

    def handle_task(self, workspace_name):
        entrypoint = sys.argv[1]
        if entrypoint == 'simple':
            logging.critical('JOB %s EXECUTED', workspace_name.decode('ascii'))
        elif entrypoint == 'fail':
            raise Exception('FAIL')
        elif entrypoint == 'queue_collapse':
            assert workspace_name == b'queue_collapse'
            time.sleep(0.35)
            logging.critical('queue_collapse OK')
        elif entrypoint == 'timeout':
示例#11
0
    # Now that routes for services have been set up, we may add the services we
    # host here to the service registry.
    worker_hostname = config.get("worker.hostname")
    worker_port = config.get("worker.port")
    service_urls = [
        (name, "http://{}:{}/services/{}".format(worker_hostname, worker_port, name,),)
        for name in services
    ]
    registry.instance().register(service_urls)

    # Now that routes for services have been set up, we are ready to
    # handle requests. Let Kubernetes know (or whoever may be sending
    # health check probes) by enabling the health check route.
    application.add_route("/health", HealthResource())

    # When we die, try reporting it to the registry.
    @atexit.register
    def unregister():
        registry.instance().unregister(service_urls)

    application.add_route("/stats", StatsResource())
except Exception as exc:
    # If we're running under `pytest-cov`, call `pytest_cov.embed.cleanup()`
    # so that we do not lose coverage info for this Python module.
    if os.environ.get("COV_CORE_DATAFILE"):  # pragma: no branch
        from pytest_cov.embed import cleanup

        cleanup()
    raise_from(exc, exc)  # pragma: no cover
示例#12
0
import logging
import os
import sys
import time

from stampede import StampedeWorker

try:
    from pytest_cov.embed import cleanup
    os._exit = lambda code, original=os._exit: cleanup() or original(code)
except ImportError:
    pass

PATH = '/tmp/stampede-tests'


class MockedStampedeWorker(StampedeWorker):
    alarm_time = 1

    def handle_task(self, workspace_name):
        entrypoint = sys.argv[1]
        if entrypoint == 'simple':
            logging.critical('JOB %s EXECUTED', workspace_name.decode('ascii'))
        elif entrypoint == 'fail':
            raise Exception('FAIL')
        elif entrypoint == 'queue_collapse':
            assert workspace_name == b'queue_collapse'
            time.sleep(0.35)
            logging.critical('queue_collapse OK')
        elif entrypoint == 'timeout':
            logging.critical('timeout STARTED')
示例#13
0
def main():
    logutils.configure_logging()

    cmdline_config = cmdline.parse_args(
        "worker.autoreload",
        "worker.hostname",
        "worker.load_workers",
        "worker.num_processes",
        "worker.num_threads",
        "worker.port",
        "worker.services_dir",
    )
    for k, v in cmdline_config.items():
        if isinstance(v, list):
            v = json.dumps(v)
        else:
            v = str(v)
        os.environ[env_var(k)] = v

    cmd = ["uwsgi", "--req-logger", "file:/dev/null"]

    autoreload = int(config.get("worker.autoreload", "0"))
    if autoreload > 0:
        cmd.extend(["--py-autoreload",
                    "{}".format(autoreload)])  # pragma: no cover

    serve_results = config.get("worker.serve_results", default=None)
    if serve_results is not None:
        for dname in serve_results.split(":"):
            cmd.extend(["--static-map", "{}={}".format(dname, dname)])

    swagger_yaml = Path(
        config.get("worker.services_dir", default="/code/services"),
        "swagger.yaml")
    if swagger_yaml.exists():
        cmd.extend(
            ["--static-map", "/services/swagger.yaml={}".format(swagger_yaml)])

    swagger_ui = Path(
        config.get("worker.swagger_ui_path", default="/usr/share/nginx/html"))
    if swagger_yaml.exists():
        cmd.extend(["--static-map", "/docs={}".format(swagger_ui)])
        cmd.extend(["--static-index", "index.html"])

    static_assets = config.get("worker.static_map", default=None)
    if static_assets is not None:
        cmd.extend(["--static-map", static_assets])

    cmd.append(
        config.get(
            "worker.uwsgi_config_file",
            default=str(Path(logutils.__file__, "..", "uwsgi.ini").resolve()),
        ))

    os.environ.setdefault(
        env_var("worker.num_processes"),
        config.get("worker.num_processes", str(psutil.cpu_count())),
    )
    os.environ.setdefault(env_var("worker.num_threads"),
                          str(config.get("worker.num_threads", 1)))
    os.environ.setdefault(env_var("worker.port"),
                          str(config.get("worker.port", 8000)))

    log = logutils.get_logger("servicelib-worker")
    log.info("Environment: %s", os.environ)
    log.info("Running: %s", " ".join(cmd))

    # If we're running under `pytest-cov`, call `pytest_cov.embed.cleanup()`
    # before exec of uWSGI, so that we do not lose coverage info for this
    # Python module.
    if os.environ.get("COV_CORE_DATAFILE"):
        from pytest_cov.embed import cleanup

        cleanup()

    os.execlp(cmd[0], *cmd[0:])