Beispiel #1
0
def main():
    logging.basicConfig(level=logging.DEBUG)

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

    service.register('test', test)
    service.start()
Beispiel #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()
Beispiel #3
0
Datei: core.py Projekt: 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]
Beispiel #4
0
Datei: core.py Projekt: 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]
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()
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()
Beispiel #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
Beispiel #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()
Beispiel #9
0
Datei: core.py Projekt: 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)
Beispiel #10
0
def main():
    logging.basicConfig(level=logging.DEBUG)

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

    service.register('test', test)
    service.start()
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
Beispiel #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
Beispiel #13
0
Datei: core.py Projekt: 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)
Beispiel #14
0
 def start_service(self, addr):
     s = Service(addr)
     s.register('divide', lambda x, y: x / y)
     s.start()
Beispiel #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()
 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)
Beispiel #17
0
 def start_service(self, addr):
     s = Service(addr)
     s.register('divide', lambda x,y: x/y)
     s.start()
 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)