serverless / event-gateway

React to any event with serverless functions across clouds
https://www.serverless.com/event-gateway
Apache License 2.0
1.65k stars 97 forks source link

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

Open johnlim opened 6 years ago

johnlim commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

Update 2

To duplicate this issue 1) Continuing off from issue #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

2) run sls deploy 3) do

curl -X POST -H "Content-Type: application/json" https://${APP}/usersduplicate --data '{ "id": "10", "firstName": "Donald", "lastName": "Duck", "email": "donalds.duckied@disney.com" }'

4) run sls logs --function emailUser. It shows event received

Received user.created event with user:
{ email: 'donalds.duckied@disney.com',
  firstName: 'Donald',
  id: '10',
  lastName: 'Duck' }

5) run sls remove 6) run sls deploy 7) run

curl -X POST -H "Content-Type: application/json" https://${APP)/usersduplicate --data '{ "id": "10", "firstName": "Donald", "lastName": "Duck", "email": "donalds.duck@disney.com" }'

8) run sls logs --function createUserDuplicate. It'll show Emitted user.created event 8) run sls logs --function emailUser. It'll show No existing streams for the function

alexdebrie commented 6 years ago

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 commented 6 years ago

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.