-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathworker-functions.ts
More file actions
131 lines (114 loc) · 3.38 KB
/
Copy pathworker-functions.ts
File metadata and controls
131 lines (114 loc) · 3.38 KB
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
import path from 'node:path';
import { register, publish, threadId } from '../worker/thread/runtime';
import { increment } from './counter.js';
import { ExecutionPlan, Job } from '@openfn/lexicon';
const tasks = {
test: async (result = 42) => {
publish('test-message', {
result,
});
return result;
},
wait: (duration = 500) =>
new Promise((resolve) => {
setTimeout(() => resolve(1), duration);
}),
readEnv: async (key: string) => {
if (key) {
return process.env[key];
}
return process.env;
},
threadId: async () => threadId,
processId: async () => process.pid,
getExecArgv: async () => process.execArgv,
// very very simple intepretation of a run function
// Most tests should use the mock-worker instead
run: async (plan: ExecutionPlan, _input: any, _adaptorPaths: any) => {
const workflowId = plan.id;
await publish('worker:workflow-start', {
workflowId,
});
try {
const [job] = plan.workflow.steps as Job[];
const result = eval(job.expression!);
await publish('worker:workflow-complete', {
workflowId,
state: result,
});
} catch (err) {
// console.error(err);
// // @ts-ignore TODO sort out error typing
// publish({
// type: 'worker:workflow-error',
// workflowId,
// message: err.message,
// threadId,
// });
// actually, just throw the error back out
throw err;
}
},
throw: async () => {
throw new Error('test_error');
},
weirdExit: async () => {
// https://www.youtube.com/watch?v=Z2cXRtblqjQ
process.exit(72);
},
// Experiments with freezing the global scope
// We may do this in the actual worker
freeze: async () => {
// This is not a deep freeze, so eg global.Error is not frozen
// Also some things like Uint8Array are not freezable, so these remain ways to scribble
Object.freeze(global);
Object.freeze(globalThis);
// Note that this is undefined, so this doesn't matter
Object.freeze(this);
},
setGlobalX: async (newValue = 42) => {
// @ts-ignore
global.x = newValue;
},
getGlobalX: async () => {
// @ts-ignore
return global.x;
},
// @ts-ignore
writeToGlobalError: async (obj) => {
Object.assign(Error, obj);
// @ts-ignore
console.log(Error.y);
},
getFromGlobalError: async (key: string) => {
// @ts-ignore
return Error[key];
},
// Tests of module state across executions
// Ie, does a module get re-initialised between runs? (No.)
incrementStatic: async () => increment(),
incrementDynamic: async () => {
const { increment } = await import(path.resolve('src/test/counter.js'));
return increment();
},
// Creating a big enough array with `Array(1e9).fill('mario')`
// is enghuh to OOM the _process_, taking the whole engine out
// This function should blow the thread's memory without
// killing the parent process
blowMemory: async () => {
let data = [];
while (true) {
data.push(Array(1e6).fill('mario'));
}
// This is too extreme and will kill the process
// Array(1e9).fill('mario')
},
// Some useful code
// const stats = v8.getHeapStatistics();
// console.log(
// `node heap limit = ${
// stats.heap_size_limit / 1024 / 1024
// } Mb\n heap used = ${hprocess.memoryUsage().heapUsed / 1024 / 1024}mb`
// );
};
register(tasks);