Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pipes & hooks wildcard event #1305

Merged
merged 7 commits into from
May 27, 2019
Merged

Add pipes & hooks wildcard event #1305

merged 7 commits into from
May 27, 2019

Conversation

thomasarbona
Copy link
Contributor

@thomasarbona thomasarbona commented May 22, 2019

What does this PR do ?

  • pipes & hooks can now trigger wildcarded events like this:
    'foo:*'
    'foo:after*'
    'foo:before*'

How should this be manually tested?

  • Step 1 : create a plugin and declare some wildcarded events handler
  • Step 2 : trigger events

@thomasarbona thomasarbona added the changelog:new-features Increase the minor version number label May 22, 2019
@thomasarbona thomasarbona self-assigned this May 22, 2019
@codecov
Copy link

codecov bot commented May 22, 2019

Codecov Report

Merging #1305 into 1-dev will increase coverage by <.01%.
The diff coverage is 95.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##            1-dev    #1305      +/-   ##
==========================================
+ Coverage   93.84%   93.85%   +<.01%     
==========================================
  Files          98       98              
  Lines        6884     6895      +11     
==========================================
+ Hits         6460     6471      +11     
  Misses        424      424
Impacted Files Coverage Δ
lib/api/kuzzle.js 83.78% <100%> (ø) ⬆️
lib/api/core/plugins/pluginsManager.js 87.76% <95%> (+0.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update db14d78...97b50d0. Read the comment docs.

@@ -831,8 +831,15 @@ class PluginsManager {
* @returns {Promise}
*/
function triggerHooks(emitter, event, data) {
const wildcardEvents = getWildcardEvents(event)
// filter events like 'foo:*' because EventEmitter2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be simpler (and better) to completely get rid of EventEmitter2, since we're only using it for wildcards (this should be a light change since EventEmitter2 has the same interface than the native EventEmitter).

And instead manage wildcards ourselves entirely, because that kind of exception handling is a bit costly performances wide, and events triggering is a critical part of Kuzzle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this code is unsafe because getWildcardEvents can return null.

lib/api/core/plugins/pluginsManager.js Show resolved Hide resolved
}

return null;
const wildcardEvents = ['*:*', `${scope}:*`];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with the event *:* because:

  • we can have an arbitrary number of separators (for instance we have a core:auth:strategyAdded event)
  • I think that * would be much simpler
Suggested change
const wildcardEvents = ['*:*', `${scope}:*`];
const wildcardEvents = ['*', `${scope}:*`];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discuss with @thomasarbona, it could be nice to handle wildcard pattern directly as regex. So we do not need to define rules for patterns and use regex syntax.

Copy link
Contributor

@scottinet scottinet May 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Veto: regexp are slow, and the number and frequency of events triggered make them a critical part of the code.

Why do we need regular expressions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right I didn't get that this action appends at funnel level. Ignore my previous comment 😅

if (indexDelimiter !== 1) {
return event.substring(0, indexDelimiter+1) + '*';
function getWildcardEvents (event) {
const [scope, name] = event.split(':');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaves events with 3 or more separator out of the picture.

Copy link
Member

@alexandrebouthinon alexandrebouthinon May 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugly but I think it can help you to figure out how to handle more than 3 separators:

Suggested change
const [scope, name] = event.split(':');
const rawEvents = event.split(':');
const [name] = rawEvents.pop();
const [scope] = rawEvents;


if (preparedHooks) {
preparedHooks.forEach(hook => {
hook(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but what if the hook function provided by the developer throw an error ? Can't we have a unhandled rejection here ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more importantly: this bypasses Kuzzle as an event emitter, which is not the purpose of hooks

if (preparedHooks) {
preparedHooks.forEach(hook => {
try {
hook(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still broken: plugins aren't the only objects using events

why don't you let the kuzzle object, which inherits from eventemitter, emit that event?

const delimIndex = event.lastIndexOf(':');

if (delimIndex === -1) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to prevent breaking code using the return value of this function without first checking that it didn't return null:

Suggested change
return null;
return [];

}
}];

pluginsManager.run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that promise must be returned, otherwise the test isn't awaited and it always passes

}
}];

pluginsManager.run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
}];

pluginsManager.run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

scottinet added a commit that referenced this pull request May 24, 2019
@kuzzle
Copy link
Contributor

kuzzle commented May 27, 2019

SonarQube analysis reported 1 issue

Note: The following issues were found on lines that were not modified in the pull request. Because these issues can't be reported as line comments, they are summarized here:

  1. INFO pluginsManager.js#L726: Complete the task associated to this TODO comment. rule

@Aschen Aschen merged commit 3d06476 into 1-dev May 27, 2019
@Aschen Aschen deleted the KZL-291-wildcarded-event branch May 27, 2019 15:31
@Aschen Aschen changed the title [KZL-291] pipes & hooks: wildcarded event Add pipes & hooks wildcarded event May 28, 2019
@Aschen Aschen changed the title Add pipes & hooks wildcarded event Add pipes & hooks wildcard event May 28, 2019
scottinet added a commit that referenced this pull request May 28, 2019
@scottinet scottinet restored the KZL-291-wildcarded-event branch May 28, 2019 15:01
@scottinet scottinet deleted the KZL-291-wildcarded-event branch May 28, 2019 15:09
xbill82 pushed a commit to kuzzleio/documentation that referenced this pull request Jun 12, 2019
- add documentation for wildcard events (kuzzleio/kuzzle#1305)

### How should this be manually tested?

- `npm install && npm run dev`
- visit http://localhost:8080/core/1/plugins/plugins/events/wildcard-events/

### Boyscout

- [ @xbill82  ] Disabled C++ and Java tests from local execution
- fix pages sorting (page with an order set to 0 isn't classified first)
@Aschen Aschen mentioned this pull request Jun 14, 2019
Aschen added a commit that referenced this pull request Jun 14, 2019
Release 1.8.0

Bug fixes

    [ #1311 ] Fix promise leaks (scottinet)
    [ #1298 ] Fix disabled protocol initialization (Aschen)
    [ #1297 ] Fix timeouts on plugin action returing the request (benoitvidis)
    [ #1288 ] Fix an error when trying a non-partial bulk update (scottinet)
    [ #1286 ] Allows bulk inserts on aliases (benoitvidis)
    [ #1282 ] Scan keys on redis cluster (benoitvidis)
    [ #1279 ] Users must be authenticated to use auth:logout (scottinet)

New features

    [ #1315 ] Add the new Vault module to handle encrypted application secrets (Aschen)
    [ #1302 ] Add write and mWrite (Aschen)
    [ #1305 ] Add pipes & hooks wildcard event (thomasarbona)

Enhancements

    [ #1318 ] Add a maximum ttl to auth:login (benoitvidis)
    [ #1301 ] Upgrade the WebSocket libraries (scottinet)
    [ #1308 ] Events triggering refactor (scottinet)
    [ #1300 ] Collection specifications methods cloisoned to a collection (thomasarbona)
    [ #1295 ] Improve validation error messages (benoitvidis)
    [ #1292 ] Throw an error when the realtime controller is invoked by plugin developers (benoitvidis)
    [ #1257 ] Add ability to define mapping policy for new fields (Aschen)
    [ #1291 ] Fix --help on subcommands (Yoann-Abbes)
    [ #1289 ] Handle ping/pong packets (scottinet)
    [ #1273 ] Fix incomplete access logs (scottinet)

Others

    [ #1317 ] Add ps dependency to plugin-dev Docker image for pm2 (benoitvidis)
    [ #1312 ] Check that .kuzzlerc.sample is well-formed (scottinet)
    [ #1299 ] Add Kuzzle Nightly & Redis 3 and 4 test (alexandrebouthinon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:new-features Increase the minor version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants