Ejemplo n.º 1
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    address = sys.argv[1]
    service = Service(address)

    service.register('test', test)
    service.start()
Ejemplo n.º 2
0
def start(configpath):
    """ Read config file and start service """
    global CONF
    CONF = util.prepare(configpath)

    service = Service(CONF.address, encoder=JSONEncoder())
    service.register('send', send)
    util.enhance(service)
    service.start()
Ejemplo n.º 3
0
Archivo: core.py Proyecto: walkr/oi
class Program(BaseProgram):
    """ Long running program with a nanoservice endpoint.

    `service` - nanoservice Service object
    `config` - the configuration parsed from --config <filepath> """

    def __init__(self, description, address):
        super(Program, self).__init__(description, address)

        self.service = Service(address) if address else None
        self.config = compat.configparser.ConfigParser()

        # Add the flag for parsing configuration file
        self.parser.add_argument(
            '--config', help='configuration file to use', nargs='?')

        if self.service is None:
            return

        # Add default service worker, which will respond to ctl commands
        # Other workers will perform other kind of work, such as
        # fetching resources from the web, etc
        self.workers.append(worker.ServiceWorker(self.service))

        # Add default commands
        self.add_command('ping', lambda: 'pong')
        self.add_command('help', self.help_function)

    def help_function(self, command=None):
        """ Show help for all available commands or just a single one """
        if command:
            return self.registered[command].get(
                'description', 'No help available'
            )
        return ', '.join(sorted(self.registered))

    def add_command(self, command, function, description=None):
        """ Register a new function for command """
        super(Program, self).add_command(command, function, description)
        self.service.register(command, function)

    def run(self, args=None):
        """ Parse comand line arguments/flags and run program """

        args = args or self.parser.parse_args()
        super(Program, self).run(args)

        # Read configuration file if any
        if args.config is not None:
            filepath = args.config
            self.config.read(filepath)

        # Start workers then wait until they finish work
        [w.start() for w in self.workers]
        [w.join() for w in self.workers]
Ejemplo n.º 4
0
Archivo: core.py Proyecto: richwu/oi
class Program(BaseProgram):
    """ Long running program with a nanoservice endpoint.

    `service` - nanoservice Service object
    `config` - the configuration parsed from --config <filepath> """
    def __init__(self, description, address):
        super(Program, self).__init__(description, address)

        self.service = Service(address) if address else None
        self.config = compat.configparser.ConfigParser()

        # Add the flag for parsing configuration file
        self.parser.add_argument('--config',
                                 help='configuration file to use',
                                 nargs='?')

        if self.service is None:
            return

        # Add default service worker, which will respond to ctl commands
        # Other workers will perform other kind of work, such as
        # fetching resources from the web, etc
        self.workers.append(worker.ServiceWorker(self.service))

        # Add default commands
        self.add_command('ping', lambda: 'pong')
        self.add_command('help', self.help_function)

    def help_function(self, command=None):
        """ Show help for all available commands or just a single one """
        if command:
            return self.registered[command].get('description',
                                                'No help available')
        return ', '.join(sorted(self.registered))

    def add_command(self, command, function, description=None):
        """ Register a new function for command """
        super(Program, self).add_command(command, function, description)
        self.service.register(command, function)

    def run(self, args=None):
        """ Parse comand line arguments/flags and run program """

        args = args or self.parser.parse_args()
        super(Program, self).run(args)

        # Read configuration file if any
        if args.config is not None:
            filepath = args.config
            self.config.read(filepath)

        # Start workers then wait until they finish work
        [w.start() for w in self.workers]
        [w.join() for w in self.workers]
Ejemplo n.º 5
0
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        addr = 'inproc://test'
        self.client = Client(addr)
        self.service = Service(addr)
        self.service.register('divide', lambda x, y: x / y)
        self.service.register('echo', lambda x: x)

    def tearDown(self):
        self.client.sock.close()
        self.service.sock.close()
Ejemplo n.º 6
0
class BaseTestCase(unittest.TestCase):

    def setUp(self):
        addr = 'inproc://test'
        self.client = Client(addr)
        self.service = Service(addr)
        self.service.register('divide', lambda x,y: x/y)
        self.service.register('echo', lambda x: x)

    def tearDown(self):
        self.client.sock.close()
        self.service.sock.close()
Ejemplo n.º 7
0
def start_service(addr, n):
    """ Start a service """
    s = Service(addr)
    s.register('add', lambda x, y: x + y)

    started = time.time()
    for _ in range(n):
        s.process()
    duration = time.time() - started

    time.sleep(0.1)
    print('Service stats:')
    util.print_stats(n, duration)
    return
Ejemplo n.º 8
0
def start(configpath):
    """ Read config file and start service """

    global CONF
    CONF = util.prepare(configpath)

    service = Service(CONF.address, encoder=JSONEncoder())
    util.enhance(service)

    service.register('decode_jwt', decode_jwt)
    service.register('generate_jwt', generate_jwt)
    service.register('generate_sms', generate_sms)
    service.register('validate_sms', validate_sms)
    service.start()
Ejemplo n.º 9
0
Archivo: core.py Proyecto: richwu/oi
    def __init__(self, description, address):
        super(Program, self).__init__(description, address)

        self.service = Service(address) if address else None
        self.config = compat.configparser.ConfigParser()

        # Add the flag for parsing configuration file
        self.parser.add_argument('--config',
                                 help='configuration file to use',
                                 nargs='?')

        if self.service is None:
            return

        # Add default service worker, which will respond to ctl commands
        # Other workers will perform other kind of work, such as
        # fetching resources from the web, etc
        self.workers.append(worker.ServiceWorker(self.service))

        # Add default commands
        self.add_command('ping', lambda: 'pong')
        self.add_command('help', self.help_function)
Ejemplo n.º 10
0
def main():
    logging.basicConfig(level=logging.DEBUG)

    address = sys.argv[1]
    service = Service(address)

    service.register('test', test)
    service.start()
Ejemplo n.º 11
0
def start_service(addr, n):
    """ Start a service """
    s = Service(addr)

    started = time.time()
    for _ in range(n):
        msg = s.sock.recv()
        s.sock.send(msg)
    s.sock.close()
    duration = time.time() - started

    print('Raw REP service stats:\n')
    util.print_stats(n, duration)
    return
Ejemplo n.º 12
0
def start_service(addr, n):
    """ Start a service """
    s = Service(addr)
    s.register('add', lambda x, y: x + y)

    started = time.time()
    for _ in range(n):
        s.process()
    duration = time.time() - started

    time.sleep(0.1)
    print('Service stats:')
    util.print_stats(n, duration)
    return
Ejemplo n.º 13
0
Archivo: core.py Proyecto: walkr/oi
    def __init__(self, description, address):
        super(Program, self).__init__(description, address)

        self.service = Service(address) if address else None
        self.config = compat.configparser.ConfigParser()

        # Add the flag for parsing configuration file
        self.parser.add_argument(
            '--config', help='configuration file to use', nargs='?')

        if self.service is None:
            return

        # Add default service worker, which will respond to ctl commands
        # Other workers will perform other kind of work, such as
        # fetching resources from the web, etc
        self.workers.append(worker.ServiceWorker(self.service))

        # Add default commands
        self.add_command('ping', lambda: 'pong')
        self.add_command('help', self.help_function)
Ejemplo n.º 14
0
 def start_service(self, addr):
     s = Service(addr)
     s.register('divide', lambda x, y: x / y)
     s.start()
Ejemplo n.º 15
0
import logging
from nanoservice import Service


def greet(name):
    return 'Hello {}'.format(name)


def add(x, y):
    return x + y


s = Service('ipc:///tmp/service.sock')
s.register('greet', greet)
s.register('add', add)
s.start()
Ejemplo n.º 16
0
 def setUp(self):
     addr = 'inproc://test'
     self.client = Client(addr)
     self.service = Service(addr)
     self.service.register('divide', lambda x,y: x/y)
     self.service.register('echo', lambda x: x)
Ejemplo n.º 17
0
 def start_service(self, addr):
     s = Service(addr)
     s.register('divide', lambda x,y: x/y)
     s.start()
Ejemplo n.º 18
0
 def setUp(self):
     addr = 'inproc://test'
     self.client = Client(addr)
     self.service = Service(addr)
     self.service.register('divide', lambda x, y: x / y)
     self.service.register('echo', lambda x: x)