Skip to content

Instantly share code, notes, and snippets.

@mattbennett
Last active March 29, 2018 23:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbennett/53c0fb4bf8260b7e086ad0f817cdd005 to your computer and use it in GitHub Desktop.
Save mattbennett/53c0fb4bf8260b7e086ad0f817cdd005 to your computer and use it in GitHub Desktop.
AMQP_URI: 'pyamqp://guest:guest@localhost'
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10
LOGGING:
version: 1
handlers:
console:
class: logging.StreamHandler
formatter: simple
file:
class: logging.handlers.TimedRotatingFileHandler
level: DEBUG
formatter: simple
when: W0
backupCount: 4
filename: /var/log/reports.log
loggers:
nameko:
level: DEBUG
handlers: [console]
amqp:
level: DEBUG
handlers: [console]
formatters:
simple:
format: '%(asctime)s > %(thread)d - %(levelname)s - %(message)s'
FROM debian:stretch
RUN apt-get update
RUN apt-get install -y python3 python3-pip erlang rabbitmq-server unixodbc unixodbc-dev
RUN pip3 install pyodbc apscheduler nameko
COPY config.yaml /srv/config.yaml
COPY service.py /srv/service.py
CMD /etc/init.d/rabbitmq-server start; cd /srv; nameko run service --config config.yaml
build:
docker build -t debian-nameko-478 -f Dockerfile .;
run:
docker run -it --rm --name debian-nameko-478 debian-nameko-478
from nameko.rpc import rpc
class Serv:
name = "serv"
@rpc
def meth(self):
return "OK"
@akotulu
Copy link

akotulu commented Sep 30, 2017

Try this

reports.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Reports (c) by Koodinurk Ltd
#
# Reports is licensed under a
# Creative Commons Attribution 4.0 International License.

# You should have received a copy of the license along with this
# work. If not, see <http://creativecommons.org/licenses/by/4.0/>.

from nameko.rpc import rpc
from nameko.containers import ServiceContainer
from pathlib import Path
from shared.common import Report

import yaml,logging,logging.config
import importlib,inspect

class ReportsService:
  """
  Reports runner, manages microservices
  """
  name = 'reports_dispatch'

  __slots__ = (
    '_reports', '_pipe', '_config'
  )

  def __init__(self, config=None):
    if config is None:
      with open(str(Path(Path.cwd(), 'config.yml'))) as f:
        self._config = yaml.load(f)
    else:
      self._config = yaml.load(config)
    
    logging.config.dictConfig(self._config.get('LOGGING'))

  def start(self):
    """ Starts reports found in directory """
    self._pipe = ServiceContainer(ReportsService, config=self._config)
    self._pipe.start()
    print('wurx')

    report_dirs = [p for p in Path(Path.cwd(), 'reports').glob('**/service.py')]
    for report_dir in report_dirs:
      module_spec = importlib.util.find_spec('reports.%s.service' % report_dir.parents[0].stem)
      if module_spec is None:
        continue

      module = importlib.util.module_from_spec(module_spec)
      module_spec.loader.exec_module(module)

    self._reports = []
    for report in Report.__subclasses__():
      report = ServiceContainer(report, config=self._config)
      report.start()
      self._reports.append(report)
    print('wurx1')

  def stop(self):
    for report in self._reports:
      report.stop()

    self._pipe.stop()

  def wait(self):
    self._pipe.wait()

  @rpc
  def run(self, service, args=None):
    pass

  @rpc
  def kill(self, service, args=None):
    pass

  @rpc
  def ps(self, service, args=None):
    pass


if __name__ == '__main__':
  service = ReportsService()
  service.start()
  service.wait()

Create reports directory and inside it service.pyfile

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Reports (c) by Koodinurk Ltd
#
# Reports is licensed under a
# Creative Commons Attribution 4.0 International License.

# You should have received a copy of the license along with this
# work. If not, see <http://creativecommons.org/licenses/by/4.0/>.

from shared.common import Report
from nameko.rpc import rpc

"""
Documentation guide
  https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
"""

VERSION = '1.0.0'

class CustomReport(Report):
  """
  Report
  """
  name = 'report'

  @rpc
  def status(self):
    """
    Return service status
    """
    return 'Wurx: ' + str(self) 

Create shared directory and inside it `common.py``

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Reports (c) by Koodinurk Ltd
#
# Reports is licensed under a
# Creative Commons Attribution 4.0 International License.

# You should have received a copy of the license along with this
# work. If not, see <http://creativecommons.org/licenses/by/4.0/>.

from nameko.rpc import rpc, RpcProxy

"""
Documentation guide
  https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
"""

class Report:
  """
  Base report service class
  """

  runner_rpc = RpcProxy('reports_dispatch')

  @rpc
  def status(self):
    """
    Return service status
    """
    return 'Wurx: ' + str(self) 

This code should work, but it hangs in the first start() call.

@mattbennett
Copy link
Author

The code above is broken in many ways. You seem to be implementing a custom runner but have misunderstood the distinction between it and the class which defines the service. Please see the example app to get a better understanding of the fundamental concepts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment