/
InvocationHandler.ts
179 lines (158 loc) · 6.35 KB
/
InvocationHandler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.
import * as coreTypes from '@azure/functions-core';
import {
HookData,
InvocationState,
PostInvocationContext,
PreInvocationContext,
ProgrammingModel,
RpcFunctionMetadata,
RpcInvocationRequest,
RpcLogCategory,
RpcLogLevel,
} from '@azure/functions-core';
import { AzureFunctionsRpcMessages as rpc } from '../../azure-functions-language-worker-protobuf/src/rpc';
import { fromCoreInvocationResponse } from '../coreApi/converters/fromCoreInvocationResponse';
import { fromCoreLogCategory, fromCoreLogLevel } from '../coreApi/converters/fromCoreStatusResult';
import { toCoreFunctionMetadata } from '../coreApi/converters/toCoreFunctionMetadata';
import { toCoreInvocationRequest } from '../coreApi/converters/toCoreInvocationRequest';
import { AzFuncSystemError, isError, ReadOnlyError } from '../errors';
import { nonNullProp } from '../utils/nonNull';
import { RegisteredFunction, WorkerChannel } from '../WorkerChannel';
import { EventHandler } from './EventHandler';
/**
* Host requests worker to invoke a Function
*/
export class InvocationHandler extends EventHandler<'invocationRequest', 'invocationResponse'> {
readonly responseName = 'invocationResponse';
getDefaultResponse(_channel: WorkerChannel, msg: rpc.IInvocationRequest): rpc.IInvocationResponse {
return { invocationId: msg.invocationId };
}
async handleEvent(channel: WorkerChannel, msg: rpc.IInvocationRequest): Promise<rpc.IInvocationResponse> {
const functionId = nonNullProp(msg, 'functionId');
let registeredFunc: RegisteredFunction | undefined;
if (channel.isUsingWorkerIndexing) {
registeredFunc = channel.functions[functionId];
} else {
registeredFunc = channel.legacyFunctionLoader.getFunction(functionId);
}
if (!registeredFunc) {
throw new AzFuncSystemError(`Function code for '${functionId}' is not loaded and cannot be invoked.`);
}
let { metadata, callback } = registeredFunc;
const msgCategory = `${nonNullProp(metadata, 'name')}.Invocation`;
const coreCtx = new CoreInvocationContext(
channel,
toCoreInvocationRequest(msg),
toCoreFunctionMetadata(metadata),
msgCategory
);
// Log invocation details to ensure the invocation received by node worker
coreCtx.log('debug', 'system', `Worker ${channel.workerId} received FunctionInvocationRequest`);
const programmingModel: ProgrammingModel = nonNullProp(channel, 'programmingModel');
const invocModel = programmingModel.getInvocationModel(coreCtx);
const hookData: HookData = {};
let { context, inputs } = await invocModel.getArguments();
const preInvocContext: PreInvocationContext = {
get hookData() {
return hookData;
},
set hookData(_obj) {
throw new ReadOnlyError('hookData');
},
get appHookData() {
return channel.appHookData;
},
set appHookData(_obj) {
throw new ReadOnlyError('appHookData');
},
get invocationContext() {
return context;
},
set invocationContext(_obj) {
throw new ReadOnlyError('invocationContext');
},
functionCallback: callback,
inputs,
};
coreCtx.state = 'preInvocationHooks';
try {
await channel.executeHooks('preInvocation', preInvocContext, msg.invocationId, msgCategory);
} finally {
coreCtx.state = undefined;
}
inputs = preInvocContext.inputs;
callback = preInvocContext.functionCallback;
const postInvocContext: PostInvocationContext = {
get hookData() {
return hookData;
},
set hookData(_obj) {
throw new ReadOnlyError('hookData');
},
get appHookData() {
return channel.appHookData;
},
set appHookData(_obj) {
throw new ReadOnlyError('appHookData');
},
get invocationContext() {
return context;
},
set invocationContext(_obj) {
throw new ReadOnlyError('invocationContext');
},
inputs,
result: null,
error: null,
};
coreCtx.state = 'invocation';
try {
postInvocContext.result = await invocModel.invokeFunction(context, inputs, callback);
} catch (err) {
postInvocContext.error = err;
} finally {
coreCtx.state = undefined;
}
coreCtx.state = 'postInvocationHooks';
try {
await channel.executeHooks('postInvocation', postInvocContext, msg.invocationId, msgCategory);
} finally {
coreCtx.state = undefined;
}
if (isError(postInvocContext.error)) {
throw postInvocContext.error;
}
return fromCoreInvocationResponse(await invocModel.getResponse(context, postInvocContext.result));
}
}
class CoreInvocationContext implements coreTypes.CoreInvocationContext {
invocationId: string;
request: RpcInvocationRequest;
metadata: RpcFunctionMetadata;
state?: InvocationState;
#channel: WorkerChannel;
#msgCategory: string;
constructor(
channel: WorkerChannel,
request: RpcInvocationRequest,
metadata: RpcFunctionMetadata,
msgCategory: string
) {
this.invocationId = nonNullProp(request, 'invocationId');
this.#channel = channel;
this.request = request;
this.metadata = metadata;
this.#msgCategory = msgCategory;
}
log(level: RpcLogLevel, logCategory: RpcLogCategory, message: string): void {
this.#channel.log({
invocationId: this.request.invocationId,
category: this.#msgCategory,
message,
level: fromCoreLogLevel(level),
logCategory: fromCoreLogCategory(logCategory),
});
}
}