import os
import datetime
import json
import uuid
from entrypoints import http
from nameko.events import EventDispatcher
from nameko.events import event_handler
from nameko.events import SINGLETON
from werkzeug.wrappers import Response
class ServiceA(object):
name = 'service_a'
event_dispatcher = EventDispatcher()
seen_before = False
@http('GET', '/start')
def start_service(self, request):
job_id = str(uuid.uuid4())
self.event_dispatcher('ping', {'job_id': job_id})
return Response(json.dumps({
'job_id': job_id}),
mimetype='application/json'
)
@event_handler("service_b", "pong", handler_type=SINGLETON)
def send_message(self, payload):
if ServiceA.seen_before:
print("Message is already received. Skipping Processing at time {}".format(datetime.datetime.utcnow()))
else:
print("First message received from service_b at time {}".format(datetime.datetime.utcnow()))
ServiceA.seen_before = True
I copied both of your modules into one called services.py and ran it. The only change I made was to correct the import of the http entrypoint.
I curled /start and it behaves as I’d expect:
$ nameko run services
starting services: service_a, service_b
Connected to amqp://guest:**@127.0.0.1:5672//
Connected to amqp://guest:**@127.0.0.1:5672//
127.0.0.1 - - [26/Sep/2018 14:51:28] "GET /start HTTP/1.1" 200 158 0.030309
pong from service_b with job_id 4b05fed6-a9f7-4aad-b950-2f2341922a75 at time 2018-09-26 13:51:28.020868 with app id NOT_FOUND
First message received from service_b at time 2018-09-26 13:51:28.033819
127.0.0.1 - - [26/Sep/2018 14:51:43] "GET /start HTTP/1.1" 200 158 0.006611
pong from service_b with job_id 9c64ea20-4352-4cad-a922-59587e1a35ec at time 2018-09-26 13:51:43.190117 with app id NOT_FOUND
Are you expecting it to do something else instead?
Also I just wanted to check that your definitely mean to be using the SINGLETON event hander type? SINGLETON is unusual and perhaps not doing what you’d expect (although it makes no difference if you only have one service)