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 pre and post invocation hooks #548

Merged
merged 13 commits into from
Mar 24, 2022
Merged

Add pre and post invocation hooks #548

merged 13 commits into from
Mar 24, 2022

Conversation

ejizba
Copy link
Contributor

@ejizba ejizba commented Mar 1, 2022

Fixes #522. Importantly, this PR does not address all possible hook-related features, but it should allow for them in the future (hopefully near future). See that issue thread for more details on that discussion.

Sample usage:

import { registerHook } from '@azure/functions-core';

registerHook('preInvocation', (context: PreInvocationContext) => {
    // can completely change the inputs passed to a user's function
    context.inputs = ['hello'];
    // can write to `context.hookData` and read the value in a postInvocationHook
    context.hookData.value = 1;
    // can write to the _invocation_ context passed to a user's function
    context.invocationContext.dataForInvoc = 2;
});

const disposable = registerHook('postInvocation', (context: PostInvocationContext) => {
    // can read data from `context.hookData` set in the preInvocationHook
    if (context.hookData.value === 1) {
    }
    // can modify the error thrown by the function
    if (context.error) {
        context.error = new Error(`Add a prefix to error text: ${context.error.message}`);
    }
    // can completely change the result of a function
    context.result = 'world';
});

disposable.dispose(); // Can stop the hook from executing in the future

See workerTypes/index.d.ts for more details/docs. See here for why I picked the name I did and how I see this built-in module working in the new programming model. Ultimately, though, we could easily rename this module and do something completely different in the future.

@ejizba ejizba requested review from alrod and hossam-nasr March 1, 2022 01:21
src/WorkerChannel.ts Outdated Show resolved Hide resolved
src/WorkerChannel.ts Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
src/setupWorkerModule.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
Comment on lines 13 to 14
private _preInvocationHooks: HookCallback[] = [];
private _postInvocationHooks: HookCallback[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

For the interest of type safety, I would support adding separate types for pre and post invocation hook callbacks, and reflecting that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the separate types, but I ran into too many build/lint errors if I tried to reference them here. If you can come up with a simple/clean way to do that let me know, otherwise I don't think this is worth the effort

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I gave it a try but I couldn't get them to work either, without having to use different functions to retrieve/execute each hook. I guess it's another argument for having separate functions 😋

Copy link
Contributor Author

@ejizba ejizba left a comment

Choose a reason for hiding this comment

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

Thanks Hossam! Most of your comments are topics I've debated myself, so I'm glad to be ironing them out with someone else now 🙂

src/WorkerChannel.ts Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
src/setupWorkerModule.ts Outdated Show resolved Hide resolved
src/WorkerChannel.ts Outdated Show resolved Hide resolved
src/WorkerChannel.ts Outdated Show resolved Hide resolved
src/WorkerChannel.ts Show resolved Hide resolved
src/WorkerChannel.ts Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
workerTypes/index.d.ts Outdated Show resolved Hide resolved
Base automatically changed from ej/async to v3.x March 11, 2022 01:01
src/setupWorkerModule.ts Outdated Show resolved Hide resolved
@ejizba
Copy link
Contributor Author

ejizba commented Mar 14, 2022

Met with the app insights team today. Overall looks good to them, but we need to add the function callback to the hooks api somehow. Filed #553 to address separately

src/setupCoreModule.ts Outdated Show resolved Hide resolved
@ejizba ejizba merged commit 3c9383f into v3.x Mar 24, 2022
@ejizba ejizba deleted the ej/hooks2 branch March 24, 2022 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide function execution hooks
3 participants