roccomuso / node-webhooks

:arrow_right_hook: Node.js module to create and trigger your own webHooks.
190 stars 47 forks source link

Emitter or Webhook trigger no longer works #24

Closed anthonydevelops closed 5 years ago

anthonydevelops commented 5 years ago

` // Initialize webhook const webHooks = new Webhooks({ db: './webHooksDB.json', httpSuccessCodes: [200, 201, 202, 203, 204], })

    // Encode key
    const base64Key = Buffer.from(`/vnf-agent/vpp1/config/generator/v1/template/${state.projectName}`).toString('base64')
    const base65Key = Buffer.from(`/vnf-agent/vpp1/config/generator/v1/project/${state.projectName}`).toString('base64')

    // Add webhook watcher onto etcd /template keys
    webHooks.add("etcd", 'http://localhost:2379/v3beta/watch')
    webHooks.add("project", 'http://0.0.0.0:2379/v3beta/watch')

    // Trigger webhook & send watch request
    webHooks.trigger("etcd", { key: base64Key })
    webHooks.trigger("project", { key: base65Key })

    // Shows emitted events
    const emitter = webHooks.getEmitter()
    emitter.on("*.success", (name, statusCode, response) => {
        console.log('SUCCESS triggering webHook ' + name + ' with status code', statusCode, 'and body', body)

        // Create object from string response
        const body = JSON.parse(response)

        // Decode value
        let value = body.kvs[0].value.toString()
        value = Buffer.from(value, 'base64')

        // Decode tar
        let buffer = JSON.parse(value)
        buffer = Buffer.from(buffer.tar_file, 'base64')

        // Displays code to frontend
        fs.appendFile('public/code.txt', buffer, function (err) {
            if (err) throw err;
            console.log('Saved!');
        });

        // Create tar folder
        fs.appendFile('public/template.tgz', buffer, function (err) {
            console.log('Saved!');
        });

    })`

We're using our webhook to grab a response for a specified key on ETCD. Everything was good and working properly, where all of a sudden for some reason the code stopped working. We were originally getting an error such as:

MaxListenersExceededWarning: (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

Where once we fixed this (by properly calling webhook.remove()), the error went away but the emitter does not pick up anything. We know the key is being properly inserted into ETCD, and the emitter was working properly, except now it doesn't work at all with no change to the code itself. Is there something you could think of that might have caused this? We even went into your source code and tried to set the webhooks.emitter = new EventEmitter2({ newListener: true, wildcard: true }) as we noticed EventEmitter2 for us had newListener set to false on default for some reason. Thanks!

roccomuso commented 5 years ago

That warning can be "ignored" anyway. It's just related to the number of listeners an EventEmitter can have. (You can also disable with process.setMaxListeners(0)).

What version of the module are you using? the latest one? Can you paste simple code to reproduce the issue?

thanks

anthonydevelops commented 5 years ago

@roccomuso After doing more testing, the webhook and emitter does work, except it doesn't work when calling the watch method using ETCD's api, so I'm going to close this as I feel this isn't an issue with the node-webhook library. Thanks!