@514labs/moose-lib 0.6.349 → 0.6.350
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/moose-runner.js +299 -137
- package/dist/moose-runner.js.map +1 -1
- package/dist/moose-runner.mjs +299 -137
- package/dist/moose-runner.mjs.map +1 -1
- package/package.json +1 -1
package/dist/moose-runner.mjs
CHANGED
|
@@ -2202,6 +2202,103 @@ var Cluster = class {
|
|
|
2202
2202
|
init_sqlHelpers();
|
|
2203
2203
|
init_internal();
|
|
2204
2204
|
init_compiler_config();
|
|
2205
|
+
|
|
2206
|
+
// src/utils/structured-logging.ts
|
|
2207
|
+
import * as util from "util";
|
|
2208
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2209
|
+
function setupStructuredConsole(getContextField, contextFieldName) {
|
|
2210
|
+
const contextStorage = new AsyncLocalStorage();
|
|
2211
|
+
const originalConsole = {
|
|
2212
|
+
log: console.log,
|
|
2213
|
+
info: console.info,
|
|
2214
|
+
warn: console.warn,
|
|
2215
|
+
error: console.error,
|
|
2216
|
+
debug: console.debug
|
|
2217
|
+
};
|
|
2218
|
+
console.log = createStructuredConsoleWrapper(
|
|
2219
|
+
contextStorage,
|
|
2220
|
+
getContextField,
|
|
2221
|
+
contextFieldName,
|
|
2222
|
+
originalConsole.log,
|
|
2223
|
+
"info"
|
|
2224
|
+
);
|
|
2225
|
+
console.info = createStructuredConsoleWrapper(
|
|
2226
|
+
contextStorage,
|
|
2227
|
+
getContextField,
|
|
2228
|
+
contextFieldName,
|
|
2229
|
+
originalConsole.info,
|
|
2230
|
+
"info"
|
|
2231
|
+
);
|
|
2232
|
+
console.warn = createStructuredConsoleWrapper(
|
|
2233
|
+
contextStorage,
|
|
2234
|
+
getContextField,
|
|
2235
|
+
contextFieldName,
|
|
2236
|
+
originalConsole.warn,
|
|
2237
|
+
"warn"
|
|
2238
|
+
);
|
|
2239
|
+
console.error = createStructuredConsoleWrapper(
|
|
2240
|
+
contextStorage,
|
|
2241
|
+
getContextField,
|
|
2242
|
+
contextFieldName,
|
|
2243
|
+
originalConsole.error,
|
|
2244
|
+
"error"
|
|
2245
|
+
);
|
|
2246
|
+
console.debug = createStructuredConsoleWrapper(
|
|
2247
|
+
contextStorage,
|
|
2248
|
+
getContextField,
|
|
2249
|
+
contextFieldName,
|
|
2250
|
+
originalConsole.debug,
|
|
2251
|
+
"debug"
|
|
2252
|
+
);
|
|
2253
|
+
return contextStorage;
|
|
2254
|
+
}
|
|
2255
|
+
function safeStringify(arg) {
|
|
2256
|
+
if (typeof arg === "object" && arg !== null) {
|
|
2257
|
+
if (arg instanceof Error) {
|
|
2258
|
+
return util.inspect(arg, { depth: 2, breakLength: Infinity });
|
|
2259
|
+
}
|
|
2260
|
+
try {
|
|
2261
|
+
return JSON.stringify(arg);
|
|
2262
|
+
} catch (e) {
|
|
2263
|
+
return util.inspect(arg, { depth: 2, breakLength: Infinity });
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
if (typeof arg === "string") {
|
|
2267
|
+
return arg;
|
|
2268
|
+
}
|
|
2269
|
+
return util.inspect(arg);
|
|
2270
|
+
}
|
|
2271
|
+
function createStructuredConsoleWrapper(contextStorage, getContextField, contextFieldName, originalMethod, level) {
|
|
2272
|
+
return (...args) => {
|
|
2273
|
+
const context = contextStorage.getStore();
|
|
2274
|
+
if (!context) {
|
|
2275
|
+
originalMethod(...args);
|
|
2276
|
+
return;
|
|
2277
|
+
}
|
|
2278
|
+
let ctxValue;
|
|
2279
|
+
try {
|
|
2280
|
+
ctxValue = getContextField(context);
|
|
2281
|
+
} catch {
|
|
2282
|
+
ctxValue = "unknown";
|
|
2283
|
+
}
|
|
2284
|
+
try {
|
|
2285
|
+
const message = args.map((arg) => safeStringify(arg)).join(" ");
|
|
2286
|
+
process.stderr.write(
|
|
2287
|
+
JSON.stringify({
|
|
2288
|
+
__moose_structured_log__: true,
|
|
2289
|
+
level,
|
|
2290
|
+
message,
|
|
2291
|
+
[contextFieldName]: ctxValue,
|
|
2292
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
2293
|
+
}) + "\n"
|
|
2294
|
+
);
|
|
2295
|
+
} catch {
|
|
2296
|
+
originalMethod(...args);
|
|
2297
|
+
}
|
|
2298
|
+
};
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
// src/consumption-apis/runner.ts
|
|
2205
2302
|
var toClientConfig2 = (config) => ({
|
|
2206
2303
|
...config,
|
|
2207
2304
|
useSSL: config.useSSL ? "true" : "false"
|
|
@@ -2210,12 +2307,21 @@ var createPath = (apisDir, path5, useCompiled2) => {
|
|
|
2210
2307
|
const extension = useCompiled2 ? ".js" : ".ts";
|
|
2211
2308
|
return `${apisDir}${path5}${extension}`;
|
|
2212
2309
|
};
|
|
2213
|
-
var httpLogger = (req, res, startMs) => {
|
|
2214
|
-
console.log(
|
|
2310
|
+
var httpLogger = (req, res, startMs, apiName) => {
|
|
2311
|
+
const logFn = () => console.log(
|
|
2215
2312
|
`${req.method} ${req.url} ${res.statusCode} ${Date.now() - startMs}ms`
|
|
2216
2313
|
);
|
|
2314
|
+
if (apiName) {
|
|
2315
|
+
apiContextStorage.run({ apiName }, logFn);
|
|
2316
|
+
} else {
|
|
2317
|
+
logFn();
|
|
2318
|
+
}
|
|
2217
2319
|
};
|
|
2218
2320
|
var modulesCache = /* @__PURE__ */ new Map();
|
|
2321
|
+
var apiContextStorage = setupStructuredConsole(
|
|
2322
|
+
(ctx) => ctx.apiName,
|
|
2323
|
+
"api_name"
|
|
2324
|
+
);
|
|
2219
2325
|
var apiHandler = async (publicKey, clickhouseClient, temporalClient, enforceAuth, jwtConfig) => {
|
|
2220
2326
|
const useCompiled2 = shouldUseCompiled();
|
|
2221
2327
|
const sourceDir = getSourceDir();
|
|
@@ -2223,6 +2329,7 @@ var apiHandler = async (publicKey, clickhouseClient, temporalClient, enforceAuth
|
|
|
2223
2329
|
const apis = await getApis2();
|
|
2224
2330
|
return async (req, res) => {
|
|
2225
2331
|
const start = Date.now();
|
|
2332
|
+
let matchedApiName;
|
|
2226
2333
|
try {
|
|
2227
2334
|
const url = new URL(req.url || "", "http://localhost");
|
|
2228
2335
|
const fileName = url.pathname;
|
|
@@ -2274,43 +2381,66 @@ var apiHandler = async (publicKey, clickhouseClient, temporalClient, enforceAuth
|
|
|
2274
2381
|
},
|
|
2275
2382
|
{}
|
|
2276
2383
|
);
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2384
|
+
const versionParam = url.searchParams.get("version");
|
|
2385
|
+
const cacheKey = versionParam ? `${pathName}:${versionParam}` : pathName;
|
|
2386
|
+
let userFuncModule;
|
|
2387
|
+
const cachedEntry = modulesCache.get(cacheKey);
|
|
2388
|
+
if (cachedEntry !== void 0) {
|
|
2389
|
+
userFuncModule = cachedEntry.module;
|
|
2390
|
+
matchedApiName = cachedEntry.apiName;
|
|
2391
|
+
} else {
|
|
2392
|
+
let lookupName = fileName.replace(/^\/+|\/+$/g, "");
|
|
2280
2393
|
let version = null;
|
|
2281
|
-
userFuncModule = apis.get(
|
|
2394
|
+
userFuncModule = apis.get(lookupName);
|
|
2395
|
+
if (userFuncModule) {
|
|
2396
|
+
matchedApiName = lookupName;
|
|
2397
|
+
}
|
|
2282
2398
|
if (!userFuncModule) {
|
|
2283
2399
|
version = url.searchParams.get("version");
|
|
2284
|
-
if (!version &&
|
|
2285
|
-
const pathParts =
|
|
2400
|
+
if (!version && lookupName.includes("/")) {
|
|
2401
|
+
const pathParts = lookupName.split("/");
|
|
2286
2402
|
if (pathParts.length >= 2) {
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
apiName = pathParts[0];
|
|
2290
|
-
version = pathParts.slice(1).join("/");
|
|
2291
|
-
}
|
|
2403
|
+
lookupName = pathParts[0];
|
|
2404
|
+
version = pathParts.slice(1).join("/");
|
|
2292
2405
|
}
|
|
2293
2406
|
}
|
|
2294
2407
|
if (!userFuncModule && version) {
|
|
2295
|
-
const versionedKey = `${
|
|
2408
|
+
const versionedKey = `${lookupName}:${version}`;
|
|
2296
2409
|
userFuncModule = apis.get(versionedKey);
|
|
2410
|
+
if (userFuncModule) {
|
|
2411
|
+
matchedApiName = lookupName;
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
if (!userFuncModule) {
|
|
2415
|
+
userFuncModule = apis.get(lookupName);
|
|
2416
|
+
if (userFuncModule) {
|
|
2417
|
+
matchedApiName = lookupName;
|
|
2418
|
+
}
|
|
2297
2419
|
}
|
|
2298
2420
|
}
|
|
2299
|
-
if (!userFuncModule) {
|
|
2421
|
+
if (!userFuncModule || matchedApiName === void 0) {
|
|
2300
2422
|
const availableApis = Array.from(apis.keys()).map(
|
|
2301
2423
|
(key) => key.replace(":", "/")
|
|
2302
2424
|
);
|
|
2303
|
-
const errorMessage = version ? `API ${
|
|
2425
|
+
const errorMessage = version ? `API ${lookupName} with version ${version} not found. Available APIs: ${availableApis.join(", ")}` : `API ${lookupName} not found. Available APIs: ${availableApis.join(", ")}`;
|
|
2304
2426
|
throw new Error(errorMessage);
|
|
2305
2427
|
}
|
|
2306
|
-
modulesCache.set(
|
|
2307
|
-
|
|
2428
|
+
modulesCache.set(cacheKey, {
|
|
2429
|
+
module: userFuncModule,
|
|
2430
|
+
apiName: matchedApiName
|
|
2431
|
+
});
|
|
2432
|
+
apiContextStorage.run({ apiName: matchedApiName }, () => {
|
|
2433
|
+
console.log(`[API] | Executing API: ${matchedApiName}`);
|
|
2434
|
+
});
|
|
2308
2435
|
}
|
|
2309
2436
|
const queryClient = new QueryClient(clickhouseClient, fileName);
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2437
|
+
const apiName = matchedApiName;
|
|
2438
|
+
const result = await apiContextStorage.run({ apiName }, async () => {
|
|
2439
|
+
return await userFuncModule(paramsObject, {
|
|
2440
|
+
client: new MooseClient(queryClient, temporalClient),
|
|
2441
|
+
sql,
|
|
2442
|
+
jwt: jwtPayload
|
|
2443
|
+
});
|
|
2314
2444
|
});
|
|
2315
2445
|
let body;
|
|
2316
2446
|
let status;
|
|
@@ -2326,27 +2456,32 @@ var apiHandler = async (publicKey, clickhouseClient, temporalClient, enforceAuth
|
|
|
2326
2456
|
}
|
|
2327
2457
|
if (status) {
|
|
2328
2458
|
res.writeHead(status, { "Content-Type": "application/json" });
|
|
2329
|
-
httpLogger(req, res, start);
|
|
2459
|
+
httpLogger(req, res, start, apiName);
|
|
2330
2460
|
} else {
|
|
2331
2461
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
2332
|
-
httpLogger(req, res, start);
|
|
2462
|
+
httpLogger(req, res, start, apiName);
|
|
2333
2463
|
}
|
|
2334
2464
|
res.end(body);
|
|
2335
2465
|
} catch (error) {
|
|
2336
|
-
console.log("error in path ", req.url, error);
|
|
2466
|
+
const logError2 = () => console.log("error in path ", req.url, error);
|
|
2467
|
+
if (matchedApiName) {
|
|
2468
|
+
apiContextStorage.run({ apiName: matchedApiName }, logError2);
|
|
2469
|
+
} else {
|
|
2470
|
+
logError2();
|
|
2471
|
+
}
|
|
2337
2472
|
if (Object.getPrototypeOf(error).constructor.name === "TypeGuardError") {
|
|
2338
2473
|
res.writeHead(400, { "Content-Type": "application/json" });
|
|
2339
2474
|
res.end(JSON.stringify({ error: error.message }));
|
|
2340
|
-
httpLogger(req, res, start);
|
|
2475
|
+
httpLogger(req, res, start, matchedApiName);
|
|
2341
2476
|
}
|
|
2342
2477
|
if (error instanceof Error) {
|
|
2343
2478
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
2344
2479
|
res.end(JSON.stringify({ error: error.message }));
|
|
2345
|
-
httpLogger(req, res, start);
|
|
2480
|
+
httpLogger(req, res, start, matchedApiName);
|
|
2346
2481
|
} else {
|
|
2347
2482
|
res.writeHead(500, { "Content-Type": "application/json" });
|
|
2348
2483
|
res.end();
|
|
2349
|
-
httpLogger(req, res, start);
|
|
2484
|
+
httpLogger(req, res, start, matchedApiName);
|
|
2350
2485
|
}
|
|
2351
2486
|
}
|
|
2352
2487
|
};
|
|
@@ -2518,6 +2653,10 @@ var SESSION_TIMEOUT_CONSUMER = 3e4;
|
|
|
2518
2653
|
var HEARTBEAT_INTERVAL_CONSUMER = 3e3;
|
|
2519
2654
|
var DEFAULT_MAX_STREAMING_CONCURRENCY = 100;
|
|
2520
2655
|
var CONSUMER_MAX_BATCH_SIZE = 1e3;
|
|
2656
|
+
var functionContextStorage = setupStructuredConsole(
|
|
2657
|
+
(ctx) => ctx.functionName,
|
|
2658
|
+
"function_name"
|
|
2659
|
+
);
|
|
2521
2660
|
var MAX_STREAMING_CONCURRENCY = process3.env.MAX_STREAMING_CONCURRENCY ? parseInt(process3.env.MAX_STREAMING_CONCURRENCY, 10) : DEFAULT_MAX_STREAMING_CONCURRENCY;
|
|
2522
2661
|
var metricsLog = (log) => {
|
|
2523
2662
|
const req = http3.request({
|
|
@@ -2843,56 +2982,63 @@ var startConsumer = async (args, logger2, metrics, _parallelism, consumer, produ
|
|
|
2843
2982
|
if (!isRunning() || isStale()) {
|
|
2844
2983
|
return;
|
|
2845
2984
|
}
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2985
|
+
const functionName = logger2.logPrefix;
|
|
2986
|
+
await functionContextStorage.run({ functionName }, async () => {
|
|
2987
|
+
metrics.count_in += batch.messages.length;
|
|
2988
|
+
cliLog({
|
|
2989
|
+
action: "Received",
|
|
2990
|
+
message: `${logger2.logPrefix.replace("__", " -> ")} ${batch.messages.length} message(s)`
|
|
2991
|
+
});
|
|
2992
|
+
logger2.log(`Received ${batch.messages.length} message(s)`);
|
|
2993
|
+
let index = 0;
|
|
2994
|
+
const readableStream = Readable2.from(batch.messages);
|
|
2995
|
+
const processedMessages = await readableStream.map(
|
|
2996
|
+
async (message) => {
|
|
2997
|
+
index++;
|
|
2998
|
+
if (batch.messages.length > DEFAULT_MAX_STREAMING_CONCURRENCY && index % DEFAULT_MAX_STREAMING_CONCURRENCY || index - 1 === batch.messages.length) {
|
|
2999
|
+
await heartbeat();
|
|
3000
|
+
}
|
|
3001
|
+
return handleMessage(
|
|
3002
|
+
logger2,
|
|
3003
|
+
streamingFunctions,
|
|
3004
|
+
message,
|
|
3005
|
+
producer,
|
|
3006
|
+
fieldMutations,
|
|
3007
|
+
args.logPayloads
|
|
3008
|
+
);
|
|
3009
|
+
},
|
|
3010
|
+
{
|
|
3011
|
+
concurrency: MAX_STREAMING_CONCURRENCY
|
|
2859
3012
|
}
|
|
2860
|
-
|
|
3013
|
+
).toArray();
|
|
3014
|
+
const filteredMessages = processedMessages.flat().filter((msg) => msg !== void 0 && msg.value !== void 0);
|
|
3015
|
+
if (args.targetTopic === void 0 || processedMessages.length === 0) {
|
|
3016
|
+
return;
|
|
3017
|
+
}
|
|
3018
|
+
await heartbeat();
|
|
3019
|
+
if (filteredMessages.length > 0) {
|
|
3020
|
+
await sendMessages(
|
|
2861
3021
|
logger2,
|
|
2862
|
-
|
|
2863
|
-
|
|
3022
|
+
metrics,
|
|
3023
|
+
args.targetTopic,
|
|
2864
3024
|
producer,
|
|
2865
|
-
|
|
2866
|
-
args.logPayloads
|
|
3025
|
+
filteredMessages
|
|
2867
3026
|
);
|
|
2868
|
-
},
|
|
2869
|
-
{
|
|
2870
|
-
concurrency: MAX_STREAMING_CONCURRENCY
|
|
2871
3027
|
}
|
|
2872
|
-
)
|
|
2873
|
-
const filteredMessages = processedMessages.flat().filter((msg) => msg !== void 0 && msg.value !== void 0);
|
|
2874
|
-
if (args.targetTopic === void 0 || processedMessages.length === 0) {
|
|
2875
|
-
return;
|
|
2876
|
-
}
|
|
2877
|
-
await heartbeat();
|
|
2878
|
-
if (filteredMessages.length > 0) {
|
|
2879
|
-
await sendMessages(
|
|
2880
|
-
logger2,
|
|
2881
|
-
metrics,
|
|
2882
|
-
args.targetTopic,
|
|
2883
|
-
producer,
|
|
2884
|
-
filteredMessages
|
|
2885
|
-
);
|
|
2886
|
-
}
|
|
3028
|
+
});
|
|
2887
3029
|
}
|
|
2888
3030
|
});
|
|
2889
3031
|
logger2.log("Consumer is running...");
|
|
2890
3032
|
};
|
|
2891
3033
|
var buildLogger = (args, workerId) => {
|
|
2892
|
-
const
|
|
2893
|
-
const
|
|
3034
|
+
const sourceBaseName = topicNameToStreamName(args.sourceTopic);
|
|
3035
|
+
const targetBaseName = args.targetTopic ? topicNameToStreamName(args.targetTopic) : void 0;
|
|
3036
|
+
const functionName = targetBaseName ? `${sourceBaseName}__${targetBaseName}` : sourceBaseName;
|
|
3037
|
+
const logPrefix = `${functionName} (worker ${workerId})`;
|
|
2894
3038
|
return {
|
|
2895
|
-
logPrefix
|
|
3039
|
+
// logPrefix is used for structured logging (function_name field)
|
|
3040
|
+
// Must match source_primitive.name format for log correlation
|
|
3041
|
+
logPrefix: functionName,
|
|
2896
3042
|
log: (message) => {
|
|
2897
3043
|
console.log(`${logPrefix}: ${message}`);
|
|
2898
3044
|
},
|
|
@@ -2957,82 +3103,88 @@ var runStreamingFunctions = async (args) => {
|
|
|
2957
3103
|
maxWorkerCount: args.maxSubscriberCount,
|
|
2958
3104
|
workerStart: async (worker, parallelism) => {
|
|
2959
3105
|
const logger2 = buildLogger(args, worker.id);
|
|
2960
|
-
const
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
{
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
);
|
|
2979
|
-
const consumer = kafka.consumer({
|
|
2980
|
-
kafkaJS: {
|
|
2981
|
-
groupId: streamingFuncId,
|
|
2982
|
-
sessionTimeout: SESSION_TIMEOUT_CONSUMER,
|
|
2983
|
-
heartbeatInterval: HEARTBEAT_INTERVAL_CONSUMER,
|
|
2984
|
-
retry: {
|
|
2985
|
-
retries: MAX_RETRIES_CONSUMER
|
|
3106
|
+
const functionName = logger2.logPrefix;
|
|
3107
|
+
return await functionContextStorage.run({ functionName }, async () => {
|
|
3108
|
+
const metrics = {
|
|
3109
|
+
count_in: 0,
|
|
3110
|
+
count_out: 0,
|
|
3111
|
+
bytes: 0
|
|
3112
|
+
};
|
|
3113
|
+
setTimeout(() => sendMessageMetrics(logger2, metrics), 1e3);
|
|
3114
|
+
const clientIdPrefix = HOSTNAME ? `${HOSTNAME}-` : "";
|
|
3115
|
+
const processId = `${clientIdPrefix}${streamingFuncId}-ts-${worker.id}`;
|
|
3116
|
+
const kafka = await getKafkaClient(
|
|
3117
|
+
{
|
|
3118
|
+
clientId: processId,
|
|
3119
|
+
broker: args.broker,
|
|
3120
|
+
securityProtocol: args.securityProtocol,
|
|
3121
|
+
saslUsername: args.saslUsername,
|
|
3122
|
+
saslPassword: args.saslPassword,
|
|
3123
|
+
saslMechanism: args.saslMechanism
|
|
2986
3124
|
},
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3125
|
+
logger2
|
|
3126
|
+
);
|
|
3127
|
+
const consumer = kafka.consumer({
|
|
3128
|
+
kafkaJS: {
|
|
3129
|
+
groupId: streamingFuncId,
|
|
3130
|
+
sessionTimeout: SESSION_TIMEOUT_CONSUMER,
|
|
3131
|
+
heartbeatInterval: HEARTBEAT_INTERVAL_CONSUMER,
|
|
3132
|
+
retry: {
|
|
3133
|
+
retries: MAX_RETRIES_CONSUMER
|
|
3134
|
+
},
|
|
3135
|
+
autoCommit: true,
|
|
3136
|
+
autoCommitInterval: AUTO_COMMIT_INTERVAL_MS,
|
|
3137
|
+
fromBeginning: true
|
|
3138
|
+
},
|
|
3139
|
+
"js.consumer.max.batch.size": CONSUMER_MAX_BATCH_SIZE
|
|
3140
|
+
});
|
|
3141
|
+
const maxMessageBytes = args.targetTopic?.max_message_bytes || 1024 * 1024;
|
|
3142
|
+
const producer = kafka.producer(
|
|
3143
|
+
createProducerConfig(maxMessageBytes)
|
|
3144
|
+
);
|
|
3000
3145
|
try {
|
|
3001
|
-
logger2.log("Starting
|
|
3002
|
-
await
|
|
3003
|
-
|
|
3004
|
-
logger2
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3146
|
+
logger2.log("Starting producer...");
|
|
3147
|
+
await startProducer(logger2, producer);
|
|
3148
|
+
try {
|
|
3149
|
+
logger2.log("Starting consumer...");
|
|
3150
|
+
await startConsumer(
|
|
3151
|
+
args,
|
|
3152
|
+
logger2,
|
|
3153
|
+
metrics,
|
|
3154
|
+
parallelism,
|
|
3155
|
+
consumer,
|
|
3156
|
+
producer,
|
|
3157
|
+
streamingFuncId
|
|
3158
|
+
);
|
|
3159
|
+
} catch (e) {
|
|
3160
|
+
logger2.error("Failed to start kafka consumer: ");
|
|
3161
|
+
if (e instanceof Error) {
|
|
3162
|
+
logError(logger2, e);
|
|
3163
|
+
}
|
|
3164
|
+
throw e;
|
|
3165
|
+
}
|
|
3011
3166
|
} catch (e) {
|
|
3012
|
-
logger2.error("Failed to start kafka
|
|
3167
|
+
logger2.error("Failed to start kafka producer: ");
|
|
3013
3168
|
if (e instanceof Error) {
|
|
3014
3169
|
logError(logger2, e);
|
|
3015
3170
|
}
|
|
3016
3171
|
throw e;
|
|
3017
3172
|
}
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
if (e instanceof Error) {
|
|
3021
|
-
logError(logger2, e);
|
|
3022
|
-
}
|
|
3023
|
-
throw e;
|
|
3024
|
-
}
|
|
3025
|
-
return [logger2, producer, consumer];
|
|
3173
|
+
return [logger2, producer, consumer];
|
|
3174
|
+
});
|
|
3026
3175
|
},
|
|
3027
3176
|
workerStop: async ([logger2, producer, consumer]) => {
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3177
|
+
const functionName = logger2.logPrefix;
|
|
3178
|
+
await functionContextStorage.run({ functionName }, async () => {
|
|
3179
|
+
logger2.log(`Received SIGTERM, shutting down gracefully...`);
|
|
3180
|
+
logger2.log("Stopping consumer first...");
|
|
3181
|
+
await stopConsumer(logger2, consumer, args.sourceTopic);
|
|
3182
|
+
logger2.log("Waiting for in-flight messages to complete...");
|
|
3183
|
+
await new Promise((resolve3) => setTimeout(resolve3, 2e3));
|
|
3184
|
+
logger2.log("Stopping producer...");
|
|
3185
|
+
await stopProducer(logger2, producer);
|
|
3186
|
+
logger2.log("Graceful shutdown completed");
|
|
3187
|
+
});
|
|
3036
3188
|
}
|
|
3037
3189
|
});
|
|
3038
3190
|
cluster2.start();
|
|
@@ -3092,6 +3244,10 @@ init_internal();
|
|
|
3092
3244
|
init_json();
|
|
3093
3245
|
import { log as logger, Context } from "@temporalio/activity";
|
|
3094
3246
|
import { isCancellation } from "@temporalio/workflow";
|
|
3247
|
+
var taskContextStorage = setupStructuredConsole(
|
|
3248
|
+
(ctx) => ctx.taskName,
|
|
3249
|
+
"task_name"
|
|
3250
|
+
);
|
|
3095
3251
|
var activities = {
|
|
3096
3252
|
async hasWorkflow(name) {
|
|
3097
3253
|
try {
|
|
@@ -3173,10 +3329,16 @@ var activities = {
|
|
|
3173
3329
|
const revivedInputData = inputData ? JSON.parse(JSON.stringify(inputData), jsonDateReviver) : inputData;
|
|
3174
3330
|
try {
|
|
3175
3331
|
startPeriodicHeartbeat();
|
|
3176
|
-
const
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3332
|
+
const taskIdentifier = workflow.name;
|
|
3333
|
+
const result = await taskContextStorage.run(
|
|
3334
|
+
{ taskName: taskIdentifier },
|
|
3335
|
+
async () => {
|
|
3336
|
+
return await Promise.race([
|
|
3337
|
+
fullTask.config.run({ state: taskState, input: revivedInputData }),
|
|
3338
|
+
context.cancelled
|
|
3339
|
+
]);
|
|
3340
|
+
}
|
|
3341
|
+
);
|
|
3180
3342
|
return result;
|
|
3181
3343
|
} catch (error) {
|
|
3182
3344
|
if (isCancellation(error)) {
|