Exemplo n.º 1
0
 def main():
     storage = Service("storage",
                       endpoints=[["localhost", 10053]],
                       io_loop=io)
     channel = yield storage.find('app', ['apps'])
     res = yield channel.rx.get()
     raise gen.Return(res)
Exemplo n.º 2
0
def get_info(flags):
    node = Service('node')

    ch = yield node.info('ppn', flags)
    info = yield ch.rx.get()

    raise gen.Return(info)
 def f():
     io = IOLoop.current()
     node = Service("node", endpoints=[["localhost", 10053]], io_loop=io)
     channel = yield node.list()
     app_list = yield channel.rx.get()
     assert isinstance(app_list, list)
     raise gen.Return("OK")
Exemplo n.º 4
0
def test_node_service_bad_on_read():
    io = CocaineIO.instance()
    node = Service("node", host="localhost", port=10053, loop=io)
    malformed_message = msgpack.packb([-999, 0])
    node.on_read(malformed_message)
    message = msgpack.packb([-999, 0, []])
    node.on_read(message)
Exemplo n.º 5
0
 def stop_app(self, appname):
     succeed = list()
     failed = list()
     hosts_count = len(self.hosts)
     for i, host in enumerate(self.hosts):
         log.info("Stop %s at host %d/%d %s" %
                  (appname, i, hosts_count, host))
         nodeinstance = None
         try:
             nodeinstance = Service("node", blockingConnect=False)
             yield nodeinstance.connect(host=host)
             res = yield app.Stop(nodeinstance, appname).execute()
             self.logcallback(str(res) + '\n')
         except Exception as e:
             item = "Unable to connect to node at host %s %s\n" % (host, e)
             log.error(item)
             self.logcallback(item)
             failed.append(host)
         else:
             item = "App %s has been stoped successfully\n" % appname
             log.info(item)
             self.logcallback(item)
             succeed.append(host)
         finally:
             if nodeinstance is not None:
                 nodeinstance.disconnect()
     yield (succeed, failed)
def test_node_service_bad_on_read():
    io = IOLoop.current()
    node = Service("node", endpoints=[["localhost", 10053]], io_loop=io)
    malformed_message = msgpack.packb([-999, 0])
    node.on_read(malformed_message)
    message = msgpack.packb([-999, 0, []])
    node.on_read(message)
Exemplo n.º 7
0
 def stop_app(self, appname):
     succeed = list()
     failed = list()
     hosts_count = len(self.hosts)
     for i, host in enumerate(self.hosts):
         log.info("Stop %s at host %d/%d %s" % (appname, i, hosts_count, host))
         nodeinstance = None
         try:
             nodeinstance = Service("node", blockingConnect=False)
             yield nodeinstance.connect(host=host)
             res = yield app.Stop(nodeinstance, appname).execute()
             self.logcallback(str(res) + "\n")
         except Exception as e:
             item = "Unable to connect to node at host %s %s\n" % (host, e)
             log.error(item)
             self.logcallback(item)
             failed.append(host)
         else:
             item = "App %s has been stoped successfully\n" % appname
             log.info(item)
             self.logcallback(item)
             succeed.append(host)
         finally:
             if nodeinstance is not None:
                 nodeinstance.disconnect()
     yield (succeed, failed)
Exemplo n.º 8
0
def ping_loop():
    echo = Service(DEFAULT_SERVICE)

    ch = yield echo.enqueue('version')
    version = yield ch.rx.get(timeout=5)

    print 'version: {}'.format(version)

    cnt = 1
    while True:
        msg = '{}_{}'.format(DEFAULT_MESSAGE, cnt)
        cnt += 1

        # msg = msgpack.packb(msg)

        print 'sending ping message {}'.format(msg)

        ch = yield echo.enqueue('ping')

        _ = yield ch.tx.write(msg)
        answer = yield ch.rx.get(timeout=10)

        print 'ans {}'.format(answer)

        yield gen.sleep(DEFAULT_SLEEP)
 def f():
     io = IOLoop.current()
     storage = Service("storage", endpoints=[["localhost", 10053]], io_loop=io)
     channel = yield storage.find('app', ['apps'])
     app_list = yield channel.rx.get()
     assert isinstance(app_list, list)
     raise gen.Return("OK")
Exemplo n.º 10
0
        def wrapper():
            active_apps = len(self.cache[name])
            self.logger.info(
                "%s: preparing to moving %s %s to an inactive queue (active %d)",
                app.id, app.name, "{0}:{1}".format(*app.address), active_apps)

            try:
                new_app = Service(name,
                                  locator=self.locator,
                                  timeout=RESOLVE_TIMEOUT)
                self.logger.info("%s: creating an instance of %s", new_app.id,
                                 name)
                yield new_app.connect()
                self.logger.info("%s: connect to an app %s endpoint %s ",
                                 new_app.id, new_app.name,
                                 "{0}:{1}".format(*new_app.address))
                timeout = (1 + random.random()) * self.refresh_period
                self.io_loop.call_later(timeout,
                                        self.move_to_inactive(new_app, name))
                # add to cache only after successfully connected
                self.cache[name].append(new_app)
            except Exception as err:
                self.logger.error("%s: unable to connect to `%s`: %s",
                                  new_app.id, name, err)
                # schedule later
                self.io_loop.call_later(self.get_timeout(name),
                                        self.move_to_inactive(app, name))
            else:
                self.logger.info("%s: move %s %s to an inactive queue", app.id,
                                 app.name, "{0}:{1}".format(*app.address))
                # current active app will be dropped here
                self.migrate_from_cache_to_inactive(app, name)
Exemplo n.º 11
0
    def get_service(self, name, request):
        # cache isn't full for the current application
        if len(self.cache[name]) < self.spool_size:
            logger = request.logger
            try:
                app = Service(name,
                              locator=self.locator,
                              timeout=RESOLVE_TIMEOUT)
                logger.info("%s: creating an instance of %s", app.id, name)
                self.cache[name].append(app)
                yield app.connect(request.traceid)
                logger.info("%s: connect to an app %s endpoint %s ", app.id,
                            app.name, "{0}:{1}".format(*app.address))

                timeout = (1 + random.random()) * self.refresh_period
                self.io_loop.call_later(timeout,
                                        self.move_to_inactive(app, name))
            except Exception as err:
                logger.error("%s: unable to connect to `%s`: %s", app.id, name,
                             err)
                drop_app_from_cache(self.cache, app, name)
                raise gen.Return()
            else:
                raise gen.Return(app)

        # get an instance from cache
        chosen = random.choice(self.cache[name])
        raise gen.Return(chosen)
Exemplo n.º 12
0
    def get_service(self, name, request):
        # cache isn't full for the current application
        if len(self.cache[name]) < self.spoolSize:
            logger = request.logger
            try:
                app = Service(name, locator=self.locator, timeout=RESOLVE_TIMEOUT)
                logger.info("%s: creating an instance of %s", app.id, name)
                self.cache[name].append(app)
                yield app.connect(request.traceid)
                logger.info("%s: connect to an app %s endpoint %s ",
                            app.id, app.name, "{0}:{1}".format(*app.address))

                timeout = (1 + random.random()) * self.refreshPeriod
                self.io_loop.call_later(timeout, self.move_to_inactive(app, name))
            except Exception as err:
                logger.error("%s: unable to connect to `%s`: %s", app.id, name, err)
                if app in self.cache[name]:
                    self.cache[name].remove(app)
                raise gen.Return()
            else:
                raise gen.Return(app)

        # get an instance from cache
        chosen = random.choice(self.cache[name])
        raise gen.Return(chosen)
Exemplo n.º 13
0
def loop():

    node = Service('node')

    apps = ['Echo{}'.format(i) for i in xrange(1, 2)]
    channels = dict()

    #for a in apps:
    #    print ('registering app {}'.format(a))
    #    channels[a] = yield node.control(a)

    ch = yield node.control('Echo1')
    print('before seq')

    #yield [ch.tx.write(sin_square(AMPH, t * 10)) for t in xrange(1,10)]
    print('DONE after seq')

    x = 0.0
    while True:
        try:
            print('Sending control.')

            # yield [channels[a].tx.write(sin_square(AMPH, x)) for a in apps]
            yield ch.tx.write(sin_square(AMPH, 10))

        except Exception as e:
            print 'error {}'.format(e)
            yield gen.sleep(ERR_SLEEP)
            ch = yield node.control('Echo1')
        else:
            print 'control is done'
            yield gen.sleep(LOOP_TO)
            x += DELTA
Exemplo n.º 14
0
def main():
    warning = "1; app status broken: "
    error = "2; depth is full: "
    node = Service("node")
    try:
        chan = yield node.list()
    except:
        print "1; error while connect to service node"
        exit(0)
    app_list = yield chan.rx.get()
    for name in app_list:
        app = Service(name)
        try:
            chan = yield app.info()
            info = yield chan.rx.get()
            if info["queue"]["depth"] == info["queue"]["capacity"]:
                if name != "v012-karma":
                    error = error + name + ","
        except:
            warning = warning + name + ","
    if error != "2; depth is full: ":
        print (error)
    elif warning != "1; app status broken: ":
        print (warning)
    else:
        print ("0;Ok")
Exemplo n.º 15
0
    def reelect_app(self, request, app):
        """tries to connect to the same app on differnet host from dist-info"""

        # disconnect app explicitly to break possibly existing connection
        app.disconnect()
        endpoints_size = len(app.locator.endpoints)

        # try x times, where x is the number of different endpoints in app locator.
        for _ in xrange(0, endpoints_size + 1):
            # last chance to take app from common pool
            if len(app.locator.endpoints) == 0:
                request.logger.info(
                    "giving up on connecting to dist-info hosts, falling back to common pool processing"
                )
                app = yield self.proxy.reelect_app(request, app)
                raise gen.Return(app)

            try:
                # always create new locator to prevent locking as we do connect with timeout
                # however lock can be still held during TCP timeout
                locator = Locator(endpoints=app.locator.endpoints)
                request.logger.info("connecting to locator %s",
                                    locator.endpoints[0])

                # first try to connect to locator only on remote host with timeout
                yield gen.with_timeout(self.service_connect_timeout,
                                       locator.connect())
                request.logger.debug("connected to locator %s for %s",
                                     locator.endpoints[0], app.name)
                app = Service(app.name,
                              locator=locator,
                              timeout=RESOLVE_TIMEOUT)

                # try to resolve and connect to application itself
                yield gen.with_timeout(self.service_connect_timeout,
                                       app.connect())
                request.logger.debug("connected to application %s via %s",
                                     app.name, app.endpoints)
            except gen.TimeoutError:
                # on timeout try next endpoint first
                request.logger.warning(
                    "timed out while connecting to application")
                continue
            except ServiceError as err:
                request.logger.warning("got error while resolving app - %s",
                                       err)
                if err.category in LOCATORCATEGORY and err.code == ESERVICENOTAVAILABLE:
                    # if the application is down - also try next endpoint
                    continue
                else:
                    raise err
            finally:
                # drop first endpoint to start next connection from different endpoint
                # we do this, as default logic of connection attempts in locator do not fit here
                app.locator.endpoints = app.locator.endpoints[1:]
            # return connected app
            raise gen.Return(app)
        raise PluginApplicationError(42, 42,
                                     "could not connect to application")
def main():
    locator = Service("locator")
    try:
        yield locator.connect()
    except:
        print "2; error while connect to locator"
        exit(1)
    print "0;Ok"
Exemplo n.º 17
0
def func3(args):
    print "Start"
    s = Service("storage")
    try:
        k = yield s.write("A", "A", "KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK", [])
    except Exception as err:
        print err
    print "END"
Exemplo n.º 18
0
    def process_synchronous(self, cocaine_service_name, cocaine_method, data):
        """Synchronous Cocaine worker handling."""
        self.log("In process_synchronous()")
        service = Service(cocaine_service_name)
        response = service.enqueue(cocaine_method, msgpack.dumps(data)).get()

        service.disconnect()
        self.log("process_synchronous() finished")
        return response
Exemplo n.º 19
0
 def f():
     io = IOLoop.current()
     storage = Service("storage",
                       endpoints=[["localhost", 10053]],
                       io_loop=io)
     channel = yield storage.find('app', ['apps'])
     app_list = yield channel.rx.get()
     assert isinstance(app_list, list)
     raise gen.Return("OK")
Exemplo n.º 20
0
    def process_asynchronous(self, cocaine_service_name, cocaine_method, data):
        """Run selected service and get all chunks as generator."""
        self.log("In process_asynchronous()")
        service = Service(cocaine_service_name)

        chunks_g = service.enqueue(cocaine_method, msgpack.dumps(data))

        yield chunks_g
        service.disconnect()
        self.log("process_asynchronous() finished")
Exemplo n.º 21
0
    def get_service_with_seed(self, name, seed, request):
        logger = request.logger
        app = Service(name, seed=seed, locator=self.locator)
        try:
            logger.info("%s: creating an instance of %s, seed %s", app.id, name, seed)
            yield app.connect(logger.traceid)
        except Exception as err:
            logger.error("%s: unable to connect to `%s`: %s", app.id, name, err)
            raise gen.Return()

        raise gen.Return(app)
Exemplo n.º 22
0
def delete_node(node):

    unicorn = Service('unicorn')

    ch = yield unicorn.get(node)
    _, version = yield ch.rx.get()

    if version == -1:
        return
    else:
        ch = yield unicorn.remove(node, version)
        yield ch.rx.get()
Exemplo n.º 23
0
    def __init__(self, client_id, client_secret, endpoints=None, name='tvm'):
        """TVM

        :param client_id: Integer client identifier.
        :param client_secret: Client secret.
        :param endpoints: TVM service endpoints list in format (host, port).
        :param name: TVM service name, defaults to 'tvm'.
        """
        self._client_id = client_id
        self._client_secret = client_secret

        self._tvm = Service(name, endpoints) if endpoints else Service(name)
Exemplo n.º 24
0
def raw(request, response):
    inc  = yield request.read()
    goapp = Service("gococaine")
    storage = Service("storage")
    # Send data to the  go application
    response.write("Send data to the go application")
    key = yield goapp.enqueue("process", str(inc))
    response.write("Data was processed and saved to the storage, key: %s" % key)
    # Read data from storage
    res = yield storage.read("namespace", key)
    response.write(res)
    response.close()
Exemplo n.º 25
0
    def get_service_with_seed(self, name, seed, request):
        logger = request.logger
        app = Service(name, seed=seed, locator=self.locator)
        try:
            logger.info("%s: creating an instance of %s, seed %s", app.id,
                        name, seed)
            yield app.connect(request.traceid)
        except Exception as err:
            logger.error("%s: unable to connect to `%s`: %s", app.id, name,
                         err)
            raise gen.Return()

        raise gen.Return(app)
Exemplo n.º 26
0
def add_node(node, val):
    unicorn = Service('unicorn')

    ch = yield unicorn.get(node)
    _, version = yield ch.rx.get()

    if version == -1:
        ch = yield unicorn.create(node, val)
        yield ch.rx.get()
    else:
        print 'puting data {}'.format(val)
        ch = yield unicorn.put(node, val, version)
        yield ch.rx.get()
def main():
    locator = Service("locator")
    try:
        yield locator.connect()
    except:
        print "1; error while connect to locator"
        exit(1)
    try:
        chan = yield locator.resolve("graphite")
        result = yield chan.rx.get()
    except:
        print "2; error while resolv service graphite"
        exit(1)
    print "0;Ok"
Exemplo n.º 28
0
def create(path):
    unicorn = Service('unicorn')

    print 'creating node {}'.format(path)

    ch = yield unicorn.create_with(path, {}, dict(ephemeral=True))
    # ch = yield unicorn.create(path, {})
    _ = yield ch.rx.get()

    print 'creating done'
    yield gen.sleep(10)

    print 'node should be removed {}'.format(path)

    yield ch.tx.close()
Exemplo n.º 29
0
 def execute(self):
     appNames = yield self.node.list()
     appInfoList = {}
     for appName in appNames:
         info = ''
         try:
             app = Service(appName, blockingConnect=False)
             yield app.connectThroughLocator(self.locator)
             info = yield app.info()
         except Exception as err:
             info = str(err)
         finally:
             appInfoList[appName] = info
     result = {'apps': appInfoList}
     yield result
Exemplo n.º 30
0
def reconnect():
    yield gen.sleep(10)

    while True:
        try:
            echo = Service(DEFAULT_SERVICE)
            ch = yield echo.enqueue('ping')

            for i in xrange(10):
                msg = 'boo{}'.format(i)
                yield ch.tx.write(msg)
                data = yield ch.rx.get()

        except Exception as e:
            print 'error1 {}'.format(e)
            yield gen.sleep(3)
Exemplo n.º 31
0
def test_service_invalid_api_version():
    io = IOLoop.current()
    storage = Service("storage",
                      endpoints=[["localhost", 10053]],
                      version=100,
                      io_loop=io)
    io.run_sync(storage.connect)
Exemplo n.º 32
0
    def _get(self, app):
        now = time.time()
        app_service = Service(app) \
            if app not in self.apps else self.apps[app].application

        self.apps[app] = _AppsCache.Record(app_service, now)
        return app_service
Exemplo n.º 33
0
def main(
        uuid_prefix, uniresis_stub_uuid, to_sleep, state_file, verify_url,
        max_workers, proportion):

    config = Config(SharedStatus())
    config.update()

    def load_state(fname):
        print('reading state for emulation from {}'.format(fname))
        with open(fname) as fl:
            return yaml.load(fl)

    emul_state = load_state(state_file) if state_file else dict(
        ppn=[(DEFAULT_PROFILE1, 100), ],
        Echo=[(DEFAULT_PROFILE1, 50), (DEFAULT_PROFILE2, 5)],
        EchoWeb=[(DEFAULT_PROFILE1, 50), (DEFAULT_PROFILE2, 10)],
    )

    # TODO: not yet implemented (released actually) in framework!
    unicorn = SecureServiceFabric.make_secure_adaptor(
        Service('unicorn'), *config.secure, endpoints=config.locator_endpoints)

    IOLoop.current().run_sync(
        lambda:
            state_pusher(
                unicorn, uuid_prefix, uniresis_stub_uuid, emul_state,
                max_workers, to_sleep, verify_url, proportion))
Exemplo n.º 34
0
def get_secure_unicorn():

    config = Config(SharedStatus())
    config.update()

    return SecureServiceFabric.make_secure_adaptor(
        Service('unicorn'), *config.secure, endpoints=config.locator_endpoints)
Exemplo n.º 35
0
def app_ctl():
    apps = [
        Service('Echo'),
        Service('ppn'),
        # Service('Echo1'),
        # Service('Echo2'),
        # Service('Echo3'),
        # Service('Echo4'),
        # Service('Echo5'),
    ]

    x = 0
    while True:
        yield [control_app(app, x + i) for i, app in enumerate(apps)]
        x += 0.02
        break
Exemplo n.º 36
0
def loop():
    node = Service('node')

    apps = [
        ('Echo1', 'IsoProcess'),
        ('Echo2', 'IsoProcess'),
        ('Echo3', 'IsoProcess'),
        ('Echo4', 'IsoProcess'),
        # from unsable hosts
        #('faces:1', 'vision'),
        #('licenseplate:8', 'vision'),
        #('superres:2', 'vision'),
    ]

    i = 0
    z = 0
    while True:

        i ^= 1
        z += 1

        if i:
            yield [start(node, app, profile) for app, profile in apps]
            chs = yield [make_ch(node, app) for app, _ in apps]

            for y in xrange(10):
                yield [control(c, y + z) for c in chs]
                yield gen.sleep(1)

            yield [close_ch(c) for c in chs]
        else:
            yield [stop(node, app) for app, _ in apps]

        yield gen.sleep(10)
Exemplo n.º 37
0
 def create_service(self, name):
     if name not in self._cache:
         if name == 'locator':
             service = Locator(endpoints=self._endpoints)
         else:
             service = Service(name, endpoints=self._endpoints)
         self._cache[name] = service
     return self._cache[name]
Exemplo n.º 38
0
 def put(self, name):
     try:
         s = Service(name)
     except Exception:
         return None
     else:
         self._cache[name] = s
         return s
Exemplo n.º 39
0
def control(times):
    node = Service('node')
    logger = Service('logging')

    try:
        ch = yield node.start_app(APP_NAME, PROFILE)
        result = yield ch.rx.get()

        print('start res {}'.format(result))
        yield logger.emit
    except Exception:
        pass

    control_channel = yield node.control(APP_NAME)
    for i in xrange(0, times):
        # print('running {}'.format(i))
        yield control_channel.tx.write(i % 10)
Exemplo n.º 40
0
 def execute(self):
     appNames = yield self.node.list()
     appInfoList = {}
     for appName in appNames:
         info = ''
         try:
             app = Service(appName, blockingConnect=False)
             yield app.connectThroughLocator(self.locator)
             info = yield app.info()
         except Exception as err:
             info = str(err)
         finally:
             appInfoList[appName] = info
     result = {
         'apps': appInfoList
     }
     yield result
Exemplo n.º 41
0
 def connect(self, host='localhost', port=10053):
     try:
         self.storage = Service('storage', host, port)
     except socket.error as err:
         if err.errno == errno.ECONNREFUSED:
             raise ConnectionRefusedError((host, port))
         else:
             raise ConnectionError(
                 (host, port), 'Unknown connection error: {0}'.format(err))
Exemplo n.º 42
0
    def __init__(self, locators=("localhost:10053",),
                 cache=DEFAULT_SERVICE_CACHE_COUNT,
                 request_id_header="", sticky_header="X-Cocaine-Sticky",
                 forcegen_request_header=False,
                 default_tracing_chance=DEFAULT_TRACING_CHANCE,
                 configuration_service="unicorn",
                 tracing_conf_path="/zipkin_sampling",
                 ioloop=None, **config):
        # stats
        self.requests_in_progress = 0
        self.requests_disconnections = 0
        self.requests_total = 0

        self.io_loop = ioloop or tornado.ioloop.IOLoop.current()
        self.service_cache_count = cache
        self.spool_size = int(self.service_cache_count * 1.5)
        self.refresh_period = config.get("refresh_timeout", DEFAULT_REFRESH_PERIOD)
        self.timeouts = config.get("timeouts", {})
        self.locator_endpoints = [parse_locators_endpoints(i) for i in locators]
        # it's initialized after start
        # to avoid an io_loop creation before fork
        self.locator = Locator(endpoints=self.locator_endpoints)
        # it's used to reply on `ping` method
        self.locator_status = False

        # active applications
        self.cache = collections.defaultdict(list)

        self.logger = logging.getLogger("cocaine.proxy.general")
        self.access_log = logging.getLogger("cocaine.proxy.access")
        self.access_log.propagate = False
        self.logger.info("locators %s",
                         ','.join("%s:%d" % (h, p) for h, p in self.locator_endpoints))

        self.sticky_header = sticky_header

        self.logger.info("conf path in `%s` configuration service: %s",
                         configuration_service, tracing_conf_path)
        self.unicorn = Service(configuration_service, locator=self.locator)
        self.sampled_apps = {}
        self.default_tracing_chance = default_tracing_chance
        self.tracing_conf_path = tracing_conf_path

        self.io_loop.add_future(self.on_sampling_updates(),
                                lambda x: self.logger.error("the sample updater must not exit"))

        if request_id_header:
            self.get_request_id = functools.partial(get_request_id, request_id_header,
                                                    force=forcegen_request_header)
        else:
            self.get_request_id = generate_request_id

        # post the watcher for routing groups
        self.io_loop.add_future(self.on_routing_groups_update(),
                                lambda x: self.logger.error("the updater must not exit"))
        # run infinity check locator health status
        self.locator_health_check()
Exemplo n.º 43
0
 def info(self, apps):
     infos = {}
     for app_ in apps:
         info = ''
         try:
             app = Service(app_, blockingConnect=False)
             yield app.connectThroughLocator(self.locator)
             info = yield app.info()
             if all([self._expand, self._storage is not None, 'profile' in info]):
                 info['profile'] = yield profile.View(self._storage, info['profile']).execute()
         except Exception as err:
             info = str(err)
         finally:
             infos[app_] = info
     result = {
         'apps': infos
     }
     yield result
Exemplo n.º 44
0
 def __new__(cls):
     if not hasattr(cls, "_instanse"):
         instanse = object.__new__(cls)
         try:
             _logger = Service("logging")
             for verbosity in _logger.perform_sync("verbosity"): #only one chunk and read choke also.
                 pass
             setattr(instanse, "_logger", _logger)
             try:
                 setattr(instanse, "target", "app/%s" % sys.argv[sys.argv.index("--app") + 1])
             except ValueError: 
                 setattr(instanse, "target", "app/%s" % "standalone" )
             _construct_logger_methods(instanse, verbosity)
         except Exception as err:
             instanse = _STDERR_Logger()
             instanse.warn("Logger init error: %s. Use stderr logger" % err)
         cls._instanse = instanse
     return cls._instanse
Exemplo n.º 45
0
 def instance(host="localhost", port=10053):
     if not hasattr(FlowTools, "_instance"):
         with FlowTools._instance_lock:
             if not hasattr(FlowTools, "_instance"):
                 from cocaine.services import Service
                 FlowTools._instance = Service("flow-tools",
                                               host=host,
                                               port=port)
     return FlowTools._instance
Exemplo n.º 46
0
    def start_async(self, login, power):
        self.log("In start_async()")
        service = Service("login")

        login_response = yield service.enqueue("login", msgpack.dumps(login))

        service.disconnect()
        self.log("got login!")

        if "error" in login_response:
            self.log("Login '{0}' is invalid!".format(login))
            self.write(login_response)
            self.finish()
        else:
            self.log("Login '{0}' ok!".format(login))
            self.process_powers("powers", "binary_powers", power)

        self.log("Finished start_async()")
Exemplo n.º 47
0
    def powers_8(self):
        self.log("In powers_8()")
        service = Service("powers")

        chunk = yield service.enqueue("binary_powers", msgpack.dumps(8))

        chunks = [chunk]
        try:
            while True:
                ch = yield
                chunks.append(ch)

        except ChokeEvent as err:
            pass

        self.powers_8_res = chunks
        service.disconnect()
        self.log("powers_8() finished")
Exemplo n.º 48
0
    def reelect_app(self, request, app):
        """tries to connect to the same app on differnet host from dist-info"""

        # store current endpoints of locator
        locator_endpoints = app.locator.endpoints

        # disconnect app explicitly to break possibly existing connection
        app.disconnect()
        endpoints_size = len(locator_endpoints)

        # try x times, where x is the number of different endpoints in app locator.
        for _ in xrange(0, endpoints_size):
            try:
                # move first endpoint to the end to start new connection from different endpoint
                # we do this, as default logic of connection attempts in locator do not fit here
                locator_endpoints = locator_endpoints[1:] + locator_endpoints[:1]

                # always create new locator to prevent locking as we do connect with timeout
                # however lock can be still held during TCP timeout
                locator = Locator(endpoints=locator_endpoints)
                request.logger.info("connecting to locator %s", locator.endpoints[0])

                # first try to connect to locator only on remote host with timeout
                yield gen.with_timeout(self.service_connect_timeout, locator.connect())
                request.logger.debug("connected to locator %s for %s", locator.endpoints[0], app.name)
                app = Service(app.name, locator=locator, timeout=RESOLVE_TIMEOUT)

                # try to resolve and connect to application itself
                yield gen.with_timeout(self.service_connect_timeout, app.connect())
                request.logger.debug("connected to application %s via %s", app.name, app.endpoints)
            except gen.TimeoutError:
                # on timeout try next endpoint first
                request.logger.warning("timed out while connecting to application")
                continue
            except ServiceError as err:
                request.logger.warning("got error while resolving app - %s", err)
                if err.category in LOCATORCATEGORY and err.code == ESERVICENOTAVAILABLE:
                    # if the application is down - also try next endpoint
                    continue
                else:
                    raise err
            # return connected app
            raise gen.Return(app)
        raise PluginApplicationError(42, 42, "could not connect to application")
Exemplo n.º 49
0
def main(cmd, path, subscribers, state_size, times):
    unicorn = Service('unicorn')

    if cmd == 'prod':
        IOLoop.current().run_sync(
            lambda: do_prod(unicorn, path, state_size, times))
    elif cmd == 'subs':
        IOLoop.current().run_sync(
            lambda: do_subscribe(unicorn, path, subscribers))
    else:
        click.secho('unknown command {}'.format(cmd), fg='red')
Exemplo n.º 50
0
def test_ref_count_of_service():
    s = Service("storage")
    ws = weakref.ref(s)
    io.run_sync(s.connect, timeout=2)
    wpipe = weakref.ref(s.pipe)
    fd = wpipe().fileno().fileno()
    s = None
    gc.collect()
    # there should be no referres to the service
    assert ws() is None, gc.get_referrers(ws())
    assert fd not in io._handlers, "%d %s" % (fd, io._handlers)
Exemplo n.º 51
0
    def process_powers(self, cocaine_service_name, cocaine_method, data):
        self.log("In process_powers()")
        service = Service(cocaine_service_name)

        chunk = yield service.enqueue(cocaine_method, msgpack.dumps(data))

        if chunk:
            try:
                while True:
                    ch = yield
                    self.log(ch)
                    self.write_chunk("{0} ".format(ch))

            except ChokeEvent as err:
                pass
        else:
            self.write_chunk("no data!")

        service.disconnect()
        self.log("process_powers() finished")
        self.finish()
Exemplo n.º 52
0
    def _reconnect_if_needed(self):
        if not self.upstream:
            host, port = self.addresses.next()
            self.upstream = Service(self.app_name, blockingConnect=False)
            self.logger.debug('Connecting to upstream service "{}", host={}, '
                              'port={}'.format(self.app_name, host, port))
            yield self.upstream.connect(host=host,
                                        port=port,
                                        timeout=self.connect_timeout,
                                        blocking=False)

        if not self.upstream.isConnected():
            try:
                self.logger.debug(
                    'Reconnecting to upstream service "{}"'.format(
                        self.app_name))
                yield self.upstream.reconnect(timeout=self.connect_timeout,
                                              blocking=False)
            except IllegalStateError:
                # seems to be in connecting state
                pass
Exemplo n.º 53
0
def app_info(task, response):
    info = dict()
    try:
        name = task["appname"]
        version = task["version"]
        username = task["username"]
        # todo: without version - search by mask? regex?
        appname = appname_from_name_version(name, version)

        if username:
            # not admin - all apps
            user_apps = yield app.List(storage).execute()
        else:
            user_apps = yield db.user_apps(username)

        if appname not in user_apps:
            raise ValueError("App %s doesn't exist" % appname)

        hosts = yield hostdb.hosts()
        for host in hosts:
            appinstance = None
            try:
                appinstance = Service(appname, blockingConnect=False)
                yield appinstance.connect(host=host)
                info[host] = yield appinstance.info()
            except Exception as err:
                log.error("Unable to connect to app %s host %s" % (appname,
                                                                   host))
            finally:
                if appinstance is not None:
                    appinstance.disconnect()
    except KeyError as err:
        response.error(-500, "Missing argument %s" % str(err))
    except Exception as err:
        log.error("Unknown error %s" % repr(err))
        response.error(-100, "Unknown error %s" % repr(err))
    else:
        response.write(info)
    finally:
        response.close()
def main():
    chrono = Service("chrono")
    try:
        chan = yield chrono.notify_after(0.1)
    except:
        print "1; error while connect to service"
        exit(1)
    try:
        id = yield chan.rx.get()
        try:
            yield chan.rx.get()
        except:
            print "2; error while read notify on notify_after method"
            exit(1)
    except:
        print "2; error while read id on notify_after method"
        exit(1)
    chan = yield chrono.notify_every(0.1)
    try:
        id = yield chan.rx.get()
        try:
            yield chan.rx.get()
        except:
            print "2; error while read notify on notify_every method"
            exit(1)
    except:
        print "2; error while read id on notify_every method"
        exit(1)
    try:
        yield chrono.restart(id)
    except:
        print "2; error while restart id on notify_every method"
        exit(1)
    try:
        chan = yield chrono.cancel(id)
        yield chan.rx.get() 
    except:
        print "2; error while close id on notify_every method"
        exit(1)
    print "0;Ok"
Exemplo n.º 55
0
        def wrapper():
            active_apps = len(self.cache[name])
            self.logger.info("%s: preparing to moving %s %s to an inactive queue (active %d)",
                             app.id, app.name, "{0}:{1}".format(*app.address), active_apps)

            try:
                new_app = Service(name, locator=self.locator, timeout=RESOLVE_TIMEOUT)
                self.logger.info("%s: creating an instance of %s", new_app.id, name)
                yield new_app.connect()
                self.logger.info("%s: connect to an app %s endpoint %s ",
                                 new_app.id, new_app.name, "{0}:{1}".format(*new_app.address))
                timeout = (1 + random.random()) * self.refresh_period
                self.io_loop.call_later(timeout, self.move_to_inactive(new_app, name))
                # add to cache only after successfully connected
                self.cache[name].append(new_app)
            except Exception as err:
                self.logger.error("%s: unable to connect to `%s`: %s", new_app.id, name, err)
                # schedule later
                self.io_loop.call_later(self.get_timeout(name), self.move_to_inactive(app, name))
            else:
                self.logger.info("%s: move %s %s to an inactive queue",
                                 app.id, app.name, "{0}:{1}".format(*app.address))
                # current active app will be dropped here
                self.migrate_from_cache_to_inactive(app, name)
Exemplo n.º 56
0
    def _reconnect_if_needed(self):
        if not self.upstream:
            host, port = self.addresses.next()
            self.upstream = Service(self.app_name, blockingConnect=False)
            self.logger.debug('Connecting to upstream service "{}", host={}, '
                              'port={}'.format(self.app_name, host, port))
            yield self.upstream.connect(host=host, port=port,
                                        timeout=self.connect_timeout,
                                        blocking=False)

        if not self.upstream.isConnected():
            try:
                self.logger.debug(
                    'Reconnecting to upstream service "{}"'.format(
                        self.app_name))
                yield self.upstream.reconnect(timeout=self.connect_timeout,
                                              blocking=False)
            except IllegalStateError:
                # seems to be in connecting state
                pass
Exemplo n.º 57
0
def apps(request):
    node = Service('node')
    list_ = yield node.list()
    yield render(request, 'list.html', {
        'apps': list_
    })
Exemplo n.º 58
0
# coding=utf-8
from tornado.ioloop import IOLoop
from cocaine.services import Service

__author__ = 'EvgenySafronov <*****@*****.**>'


if __name__ == '__main__':
    def on(chunk):
        print('Response received - {0}'.format(chunk))

    def error(reason):
        print('Error received - {0}'.format(reason))

    service = Service('Chunker')
    future = service.invoke('chunkMe', '1')
    future.bind(on, error)
    loop = IOLoop.instance()
    loop.start()
Exemplo n.º 59
0
from cocaine.services import Service
from cocaine.exceptions import TimeoutError

__author__ = 'EvgenySafronov <*****@*****.**>'


if __name__ == '__main__':
    s = Service('node')
    while True:
        try:
            info = s.info().then(lambda r: r.get()).then(lambda r: r.get()).then(lambda r: r.get()).get()
            print(info)
        except TimeoutError as err:
            print(err)
        except Exception as err:
            print(err, type(err))
Exemplo n.º 60
0
def read(request, response):
    key = yield request.read()
    storage = Service("storage")
    data = yield storage.read("namespace", key)
    response.write(data)
    response.close()