Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Event not emitted even though emit() call executed successfully #502

Open
johnlim opened this issue Aug 14, 2018 · 4 comments
Open

Event not emitted even though emit() call executed successfully #502

johnlim opened this issue Aug 14, 2018 · 4 comments

Comments

@johnlim
Copy link

johnlim commented Aug 14, 2018

This is a Bug Report

Running this code snippet

module.exports.createUsers = (event, context, callback) => {
  const user = event.data.body;
  if (!user.firstName || !user.lastName || !user.email || !user.id) {
    callback(null, {
      statusCode: 400,
      body: JSON.stringify({errors: [`Invalid request. Missing required field.`]})
    });
    return
  }

  const params = {
    TableName: USERS_EVENT_STORE,
    Item: {
      id: user.id,
      firstName: user.firstName,
      lastName: user.lastName,
      email: user.email,
    }
  };

  eventStore.put(params, (error, result) => {
    if (error) {
      console.error(error);
      callback(null, {
        statusCode: error.statusCode || 501,
        body: JSON.stringify({errors: ['Could not create user']})
      });
      return
    }

    eventGateway
        .emit({
          eventID: '1',
          eventType: 'user.created',
          cloudEventsVersion: '0.1',
          source: '/services/users',
          contentType: 'application/json',
          data: params.Item
        })
        .then(() => console.log('Emitted user.created event'));

    const response = {
      statusCode: 200,
      body: JSON.stringify(params.Item),
    };
    callback(null, response)
  });
};

resulted in the console log Emitted user.created event being captured but the event was not emitted. I know that the event was not emitted because it wasn't captured in the Event Gateway logs (i.e from the Dashboard) and the subscriber function wasn't triggered.

While deploying the console shows that the events were correctly set up as follows:

vent Gateway Plugin
Serverless: WARNING: Inappropriate call of provider.request()
EventGateway: Event Type "user.created" created.
EventGateway: Event Type "http.request" created.
EventGateway: Function "huubhr" registered. (ID: UsersService-dev-huubhr)
EventGateway: Function "createUsers" registered. (ID: dev-UsersService-createUsers)
EventGateway: Function "createUsers" subscribed to "http.request" event.
EventGateway: Function "huubhr" subscribed to "user.created" event.
@johnlim johnlim changed the title Event not emitted even though call executed successfully Event not emitted even though emit() call executed successfully Aug 15, 2018
@johnlim
Copy link
Author

johnlim commented Aug 15, 2018

##Update
I believe this is associated with issue #501 . I am encountering events not being emitted only after experiencing issue #501. I have experienced it across different apps, services and tenants but as I wasn't specifically trying to reproduce it, didn't document how to duplicate it. I'll update this issue if I can re-duplicate the issue.

@johnlim
Copy link
Author

johnlim commented Aug 15, 2018

Update 2

To duplicate this issue

  1. Continuing off from issue Event Subscription/Deletion bug #501, duplicate but rename the createUser function in handler.js and update serverless.yml as follows
  createUserDuplicate:
    handler: handler.createUserDuplicate
    events:
      - eventgateway:
          type: sync
          eventType: http.request
          path: /usersduplicate
          method: POST
  1. run sls deploy
  2. do
curl -X POST -H "Content-Type: application/json" https://${APP}/usersduplicate --data '{ "id": "10", "firstName": "Donald", "lastName": "Duck", "email": "[email protected]" }'
  1. run sls logs --function emailUser. It shows event received
Received user.created event with user:
{ email: '[email protected]',
  firstName: 'Donald',
  id: '10',
  lastName: 'Duck' }
  1. run sls remove
  2. run sls deploy
  3. run
curl -X POST -H "Content-Type: application/json" https://${APP)/usersduplicate --data '{ "id": "10", "firstName": "Donald", "lastName": "Duck", "email": "[email protected]" }'
  1. run sls logs --function createUserDuplicate. It'll show Emitted user.created event
  2. run sls logs --function emailUser. It'll show No existing streams for the function

@alexdebrie
Copy link
Contributor

Hey @johnlim, thanks for raising.

Can you add the configuration for the function that's subscribed to the user.created event? That should help with debugging.

@johnlim
Copy link
Author

johnlim commented Aug 18, 2018

Hi @alexdebrie, thanks for responding.
I'm basing the code from https://github.com/serverless/event-gateway-getting-started, therefore the configuration that subscribed to the user.created event is as follows:

  emailUser:
    handler: handler.emailUser
    events:
      - eventgateway:
          type: async
          eventType: user.created

I just double-checked the dashboard and realised event-gateway actually fired the event but the subscriber was not triggered in this particular instance.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants