forked from Azure/azure-functions-nodejs-worker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorkerChannel.ts
300 lines (270 loc) · 9.98 KB
/
WorkerChannel.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { format, isFunction } from 'util';
import { AzureFunctionsRpcMessages as rpc } from '../azure-functions-language-worker-protobuf/src/rpc';
import { IFunctionLoader } from './FunctionLoader';
import { CreateContextAndInputs, LogCallback, ResultCallback } from './Context';
import { IEventStream } from './GrpcService';
import { toTypedData } from './converters';
import { augmentTriggerMetadata } from './augmenters';
import { systemError } from './utils/Logger';
import { InternalException } from './utils/InternalException';
import LogCategory = rpc.RpcLog.RpcLogCategory;
import LogLevel = rpc.RpcLog.Level;
/**
* The worker channel should have a way to handle all incoming gRPC messages.
* This includes all incoming StreamingMessage types (exclude *Response types and RpcLog type)
*/
interface IWorkerChannel {
startStream(requestId: string, msg: rpc.StartStream): void;
workerInitRequest(requestId: string, msg: rpc.WorkerInitRequest): void;
workerHeartbeat(requestId: string, msg: rpc.WorkerHeartbeat): void;
workerTerminate(requestId: string, msg: rpc.WorkerTerminate): void;
workerStatusRequest(requestId: string, msg: rpc.WorkerStatusRequest): void;
fileChangeEventRequest(requestId: string, msg: rpc.FileChangeEventRequest): void;
functionLoadRequest(requestId: string, msg: rpc.FunctionLoadRequest): void;
invocationRequest(requestId: string, msg: rpc.InvocationRequest): void;
invocationCancel(requestId: string, msg: rpc.InvocationCancel): void;
functionEnvironmentReloadRequest(requestId: string, msg: rpc.IFunctionEnvironmentReloadRequest): void;
}
/**
* Initializes handlers for incoming gRPC messages on the client
*/
export class WorkerChannel implements IWorkerChannel {
private _eventStream: IEventStream;
private _functionLoader: IFunctionLoader;
private _workerId: string;
constructor(workerId: string, eventStream: IEventStream, functionLoader: IFunctionLoader) {
this._workerId = workerId;
this._eventStream = eventStream;
this._functionLoader = functionLoader;
// call the method with the matching 'event' name on this class, passing the requestId and event message
eventStream.on('data', (msg) => {
let event = <string>msg.content;
let eventHandler = (<any>this)[event];
if (eventHandler) {
eventHandler.apply(this, [msg.requestId, msg[event]]);
} else {
this.log({
message: `Worker ${workerId} had no handler for message '${event}'`,
level: LogLevel.Error,
logCategory: LogCategory.System
});
}
});
eventStream.on('error', function (err) {
systemError(`Worker ${workerId} encountered event stream error: `, err);
throw new InternalException(err);
});
// wrap event stream write to validate message correctness
let oldWrite = eventStream.write;
eventStream.write = function checkWrite(msg) {
let msgError = rpc.StreamingMessage.verify(msg);
if (msgError) {
systemError(`Worker ${workerId} malformed message`, msgError);
throw new InternalException(msgError);
}
oldWrite.apply(eventStream, arguments);
}
}
/**
* Captured logs or relevant details can use the logs property
* @param requestId gRPC message request id
* @param msg gRPC message content
*/
private log(log: rpc.IRpcLog) {
this._eventStream.write({
rpcLog: log
});
}
/**
* Host sends capabilities/init data to worker and requests the worker to initialize itself
* @param requestId gRPC message request id
* @param msg gRPC message content
*/
public workerInitRequest(requestId: string, msg: rpc.WorkerInitRequest) {
const capabilitiesDictionary = {
RpcHttpTriggerMetadataRemoved: "true",
RpcHttpBodyOnly: "true"
};
this._eventStream.write({
requestId: requestId,
workerInitResponse: {
result: this.getStatus(),
capabilities : capabilitiesDictionary,
}
});
}
/**
* Worker responds after loading required metadata to load function with the load result
* @param requestId gRPC message request id
* @param msg gRPC message content
*/
public functionLoadRequest(requestId: string, msg: rpc.FunctionLoadRequest) {
if (msg.functionId && msg.metadata) {
let err, errorMessage;
try {
this._functionLoader.load(msg.functionId, msg.metadata);
}
catch(exception) {
errorMessage = `Worker was unable to load function ${msg.metadata.name}: '${exception}'`;
this.log({
message: errorMessage,
level: LogLevel.Error,
logCategory: LogCategory.System
});
err = exception;
}
this._eventStream.write({
requestId: requestId,
functionLoadResponse: {
functionId: msg.functionId,
result: this.getStatus(err, errorMessage)
}
});
}
}
/**
* Host requests worker to invoke a Function
* @param requestId gRPC message request id
* @param msg gRPC message content
*/
public invocationRequest(requestId: string, msg: rpc.InvocationRequest) {
// Repopulate triggerMetaData if http.
augmentTriggerMetadata(msg);
let info = this._functionLoader.getInfo(<string>msg.functionId);
let logCallback: LogCallback = (level, category, ...args) => {
this.log({
invocationId: msg.invocationId,
category: `${info.name}.Invocation`,
message: format.apply(null, args),
level: level,
logCategory: category
});
}
let resultCallback: ResultCallback = (err, result) => {
let response: rpc.IInvocationResponse = {
invocationId: msg.invocationId,
result: this.getStatus(err)
}
try {
if (result) {
if (result.return) {
response.returnValue = toTypedData(result.return);
}
if (result.bindings) {
response.outputData = Object.keys(info.outputBindings)
.filter(key => result.bindings[key] !== undefined)
.map(key => <rpc.IParameterBinding>{
name: key,
data: info.outputBindings[key].converter(result.bindings[key])
});
}
}
} catch (e) {
response.result = this.getStatus(e)
}
this._eventStream.write({
requestId: requestId,
invocationResponse: response
});
}
let { context, inputs } = CreateContextAndInputs(info, msg, logCallback, resultCallback);
let userFunction = this._functionLoader.getFunc(<string>msg.functionId);
// catch user errors from the same async context in the event loop and correlate with invocation
// throws from asynchronous work (setTimeout, etc) are caught by 'unhandledException' and cannot be correlated with invocation
try {
let result = userFunction(context, ...inputs);
if (result && isFunction(result.then)) {
result.then(result => (<any>context.done)(null, result, true))
.catch(err => (<any>context.done)(err, null, true));
}
} catch (err) {
resultCallback(err);
}
}
/**
* Worker sends the host information identifying itself
*/
public startStream(requestId: string, msg: rpc.StartStream): void {
// Not yet implemented
}
/**
* Message is empty by design - Will add more fields in future if needed
*/
public workerHeartbeat(requestId: string, msg: rpc.WorkerHeartbeat): void {
// Not yet implemented
}
/**
* Warning before killing the process after grace_period
* Worker self terminates ..no response on this
*/
public workerTerminate(requestId: string, msg: rpc.WorkerTerminate): void {
// Not yet implemented
}
/**
* NOT USED
*/
public workerStatusRequest(requestId: string, msg: rpc.WorkerStatusRequest): void {
// Not yet implemented
}
/**
* Host notifies worker of file content change
*/
public fileChangeEventRequest(requestId: string, msg: rpc.FileChangeEventRequest): void {
// Not yet implemented
}
/**
* Host requests worker to cancel invocation
*/
public invocationCancel(requestId: string, msg: rpc.InvocationCancel): void {
// Not yet implemented
}
/**
* Environment variables from the current process
*/
public functionEnvironmentReloadRequest(requestId: string, msg: rpc.IFunctionEnvironmentReloadRequest): void {
// Add environment variables from incoming
let numVariables = (msg.environmentVariables && Object.keys(msg.environmentVariables).length) || 0;
this.log({
message: `Reloading environment variables. Found ${numVariables} variables to reload.`,
level: LogLevel.Information,
logCategory: LogCategory.System
});
let error = null;
try {
process.env = Object.assign({}, msg.environmentVariables);
// Change current working directory
if (msg.functionAppDirectory)
{
this.log({
message: `Changing current working directory to ${msg.functionAppDirectory}`,
level: LogLevel.Information,
logCategory: LogCategory.System
});
process.chdir(msg.functionAppDirectory);
}
} catch (e)
{
error = e;
}
let functionEnvironmentReloadResponse: rpc.IFunctionEnvironmentReloadResponse = {
result: this.getStatus(error)
};
this._eventStream.write({
requestId: requestId,
functionEnvironmentReloadResponse
});
}
private getStatus(err?: any, errorMessage?: string): rpc.IStatusResult{
let status: rpc.IStatusResult = {
status: rpc.StatusResult.Status.Success
};
if (err) {
status.status = rpc.StatusResult.Status.Failure;
status.exception = {
message: errorMessage || err.toString(),
stackTrace: err.stack
}
}
return status;
}
}