Skip to content

Commit fa02f84

Browse files
committed
Complete usage.
1 parent 334a450 commit fa02f84

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

src/hooks/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ const init = (executionContextMap) => (asyncId, type, triggerAsyncId) => {
3333

3434
// Setting child process entry as ref to parent context
3535
executionContextMap.set(asyncId, {
36-
ref
36+
ref,
37+
type,
38+
created: Date.now()
3739
});
3840

39-
const { context = {}, children = [] } = executionContextMap.get(ref);
41+
const { context = {}, children = [], ...meta } = executionContextMap.get(ref);
4042

4143
// Adding current async as child to parent context in order to control cleanup better
4244
executionContextMap.set(ref, {
45+
...meta,
4346
context,
4447
children: [...children, asyncId]
4548
});
@@ -53,7 +56,7 @@ const init = (executionContextMap) => (asyncId, type, triggerAsyncId) => {
5356
* @param {Number} ref - The parent process ref asyncId
5457
*/
5558
const onChildProcessDestroy = (executionContextMap, asyncId, ref) => {
56-
const { children: parentChildren, context } = executionContextMap.get(ref);
59+
const { children: parentChildren, context, ...meta } = executionContextMap.get(ref);
5760
const children = parentChildren.filter((id) => id !== asyncId);
5861

5962
// Parent context will be released upon last child removal
@@ -64,6 +67,7 @@ const onChildProcessDestroy = (executionContextMap, asyncId, ref) => {
6467
}
6568

6669
executionContextMap.set(ref, {
70+
...meta,
6771
context,
6872
children
6973
});

src/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const asyncHooks = require('async_hooks');
22
const ExecutionContextResource = require('./lib/ExecutionContextResource')
3-
const { isProduction } = require('./lib');
3+
const { isProduction, monitorMap } = require('./lib');
44
const { create: createHooks } = require('./hooks');
55
const { ExecutionContextErrors } = require('./constants');
66

@@ -48,7 +48,9 @@ const createExecutionContext = () => {
4848
if (executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_ALREADY_DECLARED);
4949

5050
executionContextMap.set(asyncId, {
51+
asyncId,
5152
context: { ...initialContext, executionId: asyncId },
53+
created: Date.now(),
5254
children: []
5355
});
5456
},
@@ -112,12 +114,7 @@ const createExecutionContext = () => {
112114
* @return {ExecutionMapUsage}
113115
*/
114116
monitor: () => {
115-
console.log('Minotir runnig');
116-
117-
return {
118-
size: executionContextMap.size,
119-
entries: [...executionContextMap.values()]
120-
}
117+
return monitorMap(executionContextMap);
121118
}
122119
};
123120

src/lib/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,42 @@ const PRODUCTION = 'production';
66

77
const env = process.env.NODE_ENV || PRODUCTION;
88

9+
/**
10+
* Calculates a duration between a given moment and now
11+
* @param {Number} now - The current time
12+
* @param {Number} created - The created time to calculate it's duration
13+
* @return {Number}
14+
*/
15+
const getDuration = (now, created) => now - created
16+
917
module.exports = {
1018
env,
1119
isProduction: (environment = env) => environment === PRODUCTION,
12-
isUndefined: (thing) => [null, undefined].includes(thing)
20+
isUndefined: (thing) => [null, undefined].includes(thing),
21+
/**
22+
* Returns a monitoring report over the "executionContext" memory usage.
23+
* @param {ExecutionContextMap} executionContextMap The execution map to monitor
24+
* @return {ExecutionMapUsage}
25+
*/
26+
monitorMap: (executionContextMap) => {
27+
const now = Date.now();
28+
const entries = [...executionContextMap.values()]
29+
.filter(({ children }) => !!children)
30+
.map(({ asyncId, created, children, context = {} }) => ({
31+
asyncId,
32+
created,
33+
contextSize: JSON.stringify(context).length,
34+
duration: getDuration(now, created),
35+
children: children.map((childId) => {
36+
const { type, created } = executionContextMap.get(childId);
37+
38+
return { asyncId: childId, type, created, duration: getDuration(now, created) }
39+
})
40+
}));
41+
42+
return {
43+
size: executionContextMap.size,
44+
entries
45+
}
46+
}
1347
};

src/spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,20 @@ describe('Context', () => {
189189
});
190190
});
191191
});
192+
193+
describe('Monitor', () => {
194+
describe('When no context is open', () => {
195+
it('Returns empty usage', (done) => {
196+
console.log(Context.monitor())
197+
198+
setTimeout(() => console.log(Context.monitor()), 1000)
199+
setTimeout(() => console.log(Context.monitor()), 3000)
200+
setTimeout(() => {
201+
console.log(Context.monitor())
202+
done();
203+
}, 5000)
204+
205+
});
206+
});
207+
});
192208
});

src/types.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,21 @@ interface HookCallbacks {
3131
destroy?(asyncId: number): void;
3232
}
3333

34-
interface ExecutionMapUsageEntry {
34+
interface ExecutionMapUsageBaseEntry {
3535
asyncId: number;
36+
created: number;
37+
duration: number;
38+
}
39+
40+
interface ExecutionMapUsageChildEntry extends ExecutionMapUsageBaseEntry {
3641
type: string;
3742
}
3843

44+
interface ExecutionMapUsageEntry extends ExecutionMapUsageBaseEntry {
45+
asyncId: number;
46+
children: ExecutionMapUsageChildEntry[];
47+
}
48+
3949
interface ExecutionMapUsage {
4050
size: number;
4151
entries: ExecutionMapUsageEntry[];

0 commit comments

Comments
 (0)