@noldova/context.processes 0.0.0-stage → 0.0.14
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/LICENSE +21 -0
- package/README.md +66 -2
- package/api/index.d.ts +214 -0
- package/api/index.js +13 -0
- package/api/index.js.map +1 -0
- package/exceptions/process-tree-end.exception.js +17 -0
- package/exceptions/process-tree-end.exception.js.map +1 -0
- package/exceptions/processes.exception.js +14 -0
- package/exceptions/processes.exception.js.map +1 -0
- package/interfaces/i-process-identity.js +9 -0
- package/interfaces/i-process-identity.js.map +1 -0
- package/models/process-entry.js +34 -0
- package/models/process-entry.js.map +1 -0
- package/models/process-identity-comparer.js +17 -0
- package/models/process-identity-comparer.js.map +1 -0
- package/models/process-identity.js +27 -0
- package/models/process-identity.js.map +1 -0
- package/models/process-listing.js +27 -0
- package/models/process-listing.js.map +1 -0
- package/models/process-signals.js +20 -0
- package/models/process-signals.js.map +1 -0
- package/models/process-tree-end-options.js +24 -0
- package/models/process-tree-end-options.js.map +1 -0
- package/models/process-tree-members.js +40 -0
- package/models/process-tree-members.js.map +1 -0
- package/package.json +25 -4
- package/resources.js +123 -0
- package/resources.js.map +1 -0
- package/services/lister-process.js +106 -0
- package/services/lister-process.js.map +1 -0
- package/services/process-lister.js +62 -0
- package/services/process-lister.js.map +1 -0
- package/services/process-tree.js +83 -0
- package/services/process-tree.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Noldova
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# noldova.context.processes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Ends a process and every process it started, and returns only once they are
|
|
4
|
+
gone. A process is named by its id and its start time, so an id the system has
|
|
5
|
+
since given to another process is never ended.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @noldova/context.processes
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { spawn } from "node:child_process";
|
|
17
|
+
|
|
18
|
+
import "@noldova/context.core";
|
|
19
|
+
import { ProcessIdentity, ProcessTree, ProcessTreeEndOptions } from "@noldova/context.processes";
|
|
20
|
+
|
|
21
|
+
const server = spawn(process.execPath, ["server.js"], { detached: true });
|
|
22
|
+
const identity = await ProcessIdentity.readAsync(server.pid ?? 0);
|
|
23
|
+
|
|
24
|
+
if (!Object.isUndefined(identity))
|
|
25
|
+
await ProcessTree.endAsync(identity, new ProcessTreeEndOptions(2000));
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Read the identity while you own the process, such as right after starting it.
|
|
29
|
+
Start the root with `detached: true`, so on Linux and macOS it leads a process
|
|
30
|
+
group of its own and a process whose parent ended is still found through the
|
|
31
|
+
group.
|
|
32
|
+
|
|
33
|
+
`endAsync` finds the tree from the root: every process whose parent is in the
|
|
34
|
+
tree and that started no earlier than its parent, and, on Linux and macOS,
|
|
35
|
+
every process in the root's group. It stops each with `SIGSTOP` and lists the
|
|
36
|
+
tree again until nothing new appears, then ends them, with `SIGTERM` and the
|
|
37
|
+
grace first when one is given, and lists the tree every 50 ms until none of it
|
|
38
|
+
is left. It throws `ProcessTreeEndException`, naming what is left, when the
|
|
39
|
+
limit passes; the limit is 10 seconds by default.
|
|
40
|
+
A process is a member's child only when no process listed took the member's id
|
|
41
|
+
between the two starts, so an id passed on doesn't bring in a stranger's children.
|
|
42
|
+
|
|
43
|
+
`endDescendantsAsync` ends the same tree but leaves its root running, so a
|
|
44
|
+
process can end what it started without ending itself:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const own = await ProcessIdentity.readAsync(process.pid);
|
|
48
|
+
|
|
49
|
+
if (!Object.isUndefined(own))
|
|
50
|
+
await ProcessTree.endDescendantsAsync(own);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The processes are listed through one lister process, started on the first call
|
|
54
|
+
and kept for the rest of the process's life. The limit starts once it is ready,
|
|
55
|
+
so the first call in a process may first wait up to 60 seconds for it, which a
|
|
56
|
+
fresh Windows machine can take; a lister not ready by then throws
|
|
57
|
+
`ProcessesException`.
|
|
58
|
+
|
|
59
|
+
On Windows a process can't be stopped and has no polite request: the tree is
|
|
60
|
+
listed once more before the end, the grace is only waited out, each process is
|
|
61
|
+
ended with `TerminateProcess`, and a process that keeps starting others is
|
|
62
|
+
ended over several rounds. Windows has no process group, so a process whose parent ended before
|
|
63
|
+
the call is not found there.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT; see `LICENSE`.
|
package/api/index.d.ts
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { IReadOnlyList } from "@noldova/context.collections";
|
|
10
|
+
import { Exception, type ExceptionOptions } from "@noldova/context.exceptions";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Raised when processes can't be listed or ended: on a system other than Linux, macOS or
|
|
14
|
+
* Windows, when the process lister ends before it answers, or when it answers a line that is not
|
|
15
|
+
* a process.
|
|
16
|
+
*/
|
|
17
|
+
export declare class ProcessesException extends Exception {
|
|
18
|
+
/**
|
|
19
|
+
* Initializes the exception.
|
|
20
|
+
*
|
|
21
|
+
* @param message What failed, normally a `Resources` message.
|
|
22
|
+
* @param options Optional options carrying the cause.
|
|
23
|
+
*/
|
|
24
|
+
public constructor(message: string, options?: ExceptionOptions);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Raised when a process tree still has processes when its limit passes.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ProcessTreeEndException extends ProcessesException {
|
|
31
|
+
/**
|
|
32
|
+
* The processes still listed when the limit passed, each ended once more just before the
|
|
33
|
+
* exception.
|
|
34
|
+
*/
|
|
35
|
+
public readonly survivors: IReadOnlyList<ProcessIdentity>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Initializes the exception. The message counts and names the survivors and gives the limit.
|
|
39
|
+
*
|
|
40
|
+
* @param survivors The processes still listed when the limit passed.
|
|
41
|
+
* @param limitMilliseconds The limit that passed.
|
|
42
|
+
*/
|
|
43
|
+
public constructor(survivors: IReadOnlyList<ProcessIdentity>, limitMilliseconds: number);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A process, told apart from any later process with the same id by the time it started.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* An operating system reuses a process id once its process has ended, so an id alone can name
|
|
51
|
+
* another process later. Read an identity with `readAsync` while you own the process, such as
|
|
52
|
+
* right after starting it, and pass it to `ProcessTree.endAsync`.
|
|
53
|
+
* The start time is in milliseconds since the epoch: to the millisecond on Windows, and to the
|
|
54
|
+
* second on Linux and macOS, as `ps` reports it.
|
|
55
|
+
*/
|
|
56
|
+
export declare class ProcessIdentity {
|
|
57
|
+
/**
|
|
58
|
+
* The process id.
|
|
59
|
+
*/
|
|
60
|
+
public readonly processId: number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* When the process started, in milliseconds since the epoch.
|
|
64
|
+
*/
|
|
65
|
+
public readonly startTime: number;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Initializes the identity.
|
|
69
|
+
*
|
|
70
|
+
* @param processId The process id, a positive integer.
|
|
71
|
+
* @param startTime When the process started, in milliseconds since the epoch, as `readAsync`
|
|
72
|
+
* reads it.
|
|
73
|
+
* @throws ArgumentOutOfRangeException When the id is not a positive integer or the start time is
|
|
74
|
+
* not an integer from 0.
|
|
75
|
+
*/
|
|
76
|
+
public constructor(processId: number, startTime: number);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Reads the identity of a running process.
|
|
80
|
+
*
|
|
81
|
+
* @param processId The process id, a positive integer.
|
|
82
|
+
* @returns The identity, or `undefined` when no process has the id or it has ended and waits
|
|
83
|
+
* for its parent to collect it.
|
|
84
|
+
* @throws ArgumentOutOfRangeException When the id is not a positive integer.
|
|
85
|
+
* @throws ProcessesException When processes can't be listed on this system, or the process lister
|
|
86
|
+
* answers a line that is not a process.
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { spawn } from "node:child_process";
|
|
90
|
+
*
|
|
91
|
+
* import { ProcessIdentity } from "@noldova/context.processes";
|
|
92
|
+
*
|
|
93
|
+
* const child = spawn(process.execPath, ["server.js"], { detached: true });
|
|
94
|
+
* const identity = await ProcessIdentity.readAsync(child.pid ?? 0);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
public static readAsync(processId: number): Promise<ProcessIdentity | undefined>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* How `ProcessTree.endAsync` and `ProcessTree.endDescendantsAsync` end a tree.
|
|
102
|
+
*/
|
|
103
|
+
export declare class ProcessTreeEndOptions {
|
|
104
|
+
/**
|
|
105
|
+
* How long the tree's processes may take to end after a polite request, in milliseconds;
|
|
106
|
+
* `0`, the default, ends them at once.
|
|
107
|
+
*/
|
|
108
|
+
public readonly graceMilliseconds: number;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* How long the end may take once the process lister is ready, in milliseconds; `10000` by
|
|
112
|
+
* default. The first end in a process first waits up to 60 seconds for its lister to start.
|
|
113
|
+
*/
|
|
114
|
+
public readonly limitMilliseconds: number;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Initializes the options.
|
|
118
|
+
*
|
|
119
|
+
* @param graceMilliseconds How long the processes may take to end after a polite request; `0`
|
|
120
|
+
* by default.
|
|
121
|
+
* @param limitMilliseconds How long the end may take once the process lister is ready; `10000`
|
|
122
|
+
* by default.
|
|
123
|
+
* @throws ArgumentOutOfRangeException When either is not an integer from 0.
|
|
124
|
+
*/
|
|
125
|
+
public constructor(graceMilliseconds?: number, limitMilliseconds?: number);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Ends a process and every process it started, or only the processes it started, and returns
|
|
130
|
+
* only once they are gone.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* The tree is the root, every process whose parent is in the tree and that started no earlier
|
|
134
|
+
* than its parent, and, on Linux and macOS, every process in the process group the root leads
|
|
135
|
+
* that started no earlier than the root. A process that left the group with `setsid` is still
|
|
136
|
+
* found through its parent while its parent lives, and a process whose parent ended is still
|
|
137
|
+
* found through the group while it stays in it; start the root with `detached: true` so it leads
|
|
138
|
+
* a group of its own. A process that both left the group and lost its parent before the call
|
|
139
|
+
* can't be found. A process is a member's child only when no process listed took the member's id
|
|
140
|
+
* between the two starts, so a member's id passed to another process doesn't bring in that
|
|
141
|
+
* process's children.
|
|
142
|
+
*
|
|
143
|
+
* The processes are listed through one lister process, started on the first call and kept for
|
|
144
|
+
* the rest of the process's life; it may take up to 60 seconds to start, and the call's limit
|
|
145
|
+
* starts once it is ready.
|
|
146
|
+
*
|
|
147
|
+
* On Linux and macOS each process is stopped with `SIGSTOP` before it is ended, and the tree is
|
|
148
|
+
* listed again until no new process appears, at most ten times, so no process starts or leaves
|
|
149
|
+
* between the listing and the end. With a grace, the processes get `SIGTERM` and `SIGCONT` and
|
|
150
|
+
* the grace to end; whatever is left gets `SIGKILL`. Windows can't stop a process and has no
|
|
151
|
+
* polite request, so there the tree is listed once more before the end, the grace is only waited
|
|
152
|
+
* out, and each process is ended with `TerminateProcess`.
|
|
153
|
+
* A process is signalled only when the round's listing shows it with its identity; on Linux and
|
|
154
|
+
* macOS it is stopped by then, so its id can't pass to another process before the signal.
|
|
155
|
+
*
|
|
156
|
+
* After ending, the tree is listed every 50 ms until none of its processes is listed; a process
|
|
157
|
+
* that has ended and waits for its parent to collect it counts as gone. Each round ends again
|
|
158
|
+
* whatever is still listed.
|
|
159
|
+
*/
|
|
160
|
+
export declare class ProcessTree {
|
|
161
|
+
private constructor();
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Ends a process tree and waits until it is gone.
|
|
165
|
+
*
|
|
166
|
+
* @param root The identity of the tree's root, read while the caller owned the process.
|
|
167
|
+
* @param options How to end the tree; no grace and a 10-second limit by default.
|
|
168
|
+
* @returns The tree's processes, all gone; empty when another process now has the root's id,
|
|
169
|
+
* so nothing was ended.
|
|
170
|
+
* @throws ProcessTreeEndException When processes of the tree are still listed when the limit
|
|
171
|
+
* passes.
|
|
172
|
+
* @throws ProcessesException When processes can't be listed on this system, the process lister
|
|
173
|
+
* is not ready within 60 seconds, ends before it answers, which it does when it takes 5 seconds
|
|
174
|
+
* past the limit, or answers a line that is not a process.
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* import { spawn } from "node:child_process";
|
|
178
|
+
*
|
|
179
|
+
* import "@noldova/context.core";
|
|
180
|
+
* import { ProcessIdentity, ProcessTree, ProcessTreeEndOptions } from "@noldova/context.processes";
|
|
181
|
+
*
|
|
182
|
+
* const child = spawn(process.execPath, ["server.js"], { detached: true });
|
|
183
|
+
* const identity = await ProcessIdentity.readAsync(child.pid ?? 0);
|
|
184
|
+
* if (!Object.isUndefined(identity))
|
|
185
|
+
* await ProcessTree.endAsync(identity, new ProcessTreeEndOptions(2000));
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
public static endAsync(root: ProcessIdentity, options?: ProcessTreeEndOptions): Promise<IReadOnlyList<ProcessIdentity>>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Ends every process a process started, and waits until they are gone, leaving the process
|
|
192
|
+
* itself running; a process ends the processes it left this way with its own identity.
|
|
193
|
+
*
|
|
194
|
+
* @param root The identity of the tree's root, which is never signalled.
|
|
195
|
+
* @param options How to end the root's descendants; no grace and a 10-second limit by default.
|
|
196
|
+
* @returns The root's descendants, all gone; empty when the root has none, or when another
|
|
197
|
+
* process now has the root's id, so nothing was ended.
|
|
198
|
+
* @throws ProcessTreeEndException When descendants of the root are still listed when the limit
|
|
199
|
+
* passes.
|
|
200
|
+
* @throws ProcessesException When processes can't be listed on this system, the process lister
|
|
201
|
+
* is not ready within 60 seconds, ends before it answers, which it does when it takes 5 seconds
|
|
202
|
+
* past the limit, or answers a line that is not a process.
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* import "@noldova/context.core";
|
|
206
|
+
* import { ProcessIdentity, ProcessTree } from "@noldova/context.processes";
|
|
207
|
+
*
|
|
208
|
+
* const identity = await ProcessIdentity.readAsync(process.pid);
|
|
209
|
+
* if (!Object.isUndefined(identity))
|
|
210
|
+
* await ProcessTree.endDescendantsAsync(identity);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
public static endDescendantsAsync(root: ProcessIdentity, options?: ProcessTreeEndOptions): Promise<IReadOnlyList<ProcessIdentity>>;
|
|
214
|
+
}
|
package/api/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export { ProcessTreeEndException } from "../exceptions/process-tree-end.exception.js";
|
|
9
|
+
export { ProcessesException } from "../exceptions/processes.exception.js";
|
|
10
|
+
export { ProcessIdentity } from "../models/process-identity.js";
|
|
11
|
+
export { ProcessTreeEndOptions } from "../models/process-tree-end-options.js";
|
|
12
|
+
export { ProcessTree } from "../services/process-tree.js";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
package/api/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAC;AACtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources } from "../resources.js";
|
|
9
|
+
import { ProcessesException } from "./processes.exception.js";
|
|
10
|
+
export class ProcessTreeEndException extends ProcessesException {
|
|
11
|
+
survivors;
|
|
12
|
+
constructor(survivors, limitMilliseconds) {
|
|
13
|
+
super(Resources.formatTreeNotEnded(survivors.select(t => Resources.formatSurvivor(t.processId, t.startTime)).toList(), limitMilliseconds));
|
|
14
|
+
this.survivors = survivors;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=process-tree-end.exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-tree-end.exception.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/exceptions/process-tree-end.exception.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAE9D,MAAM,OAAO,uBAAwB,SAAQ,kBAAkB;IAC7C,SAAS,CAAiC;IAE1D,YAAmB,SAAyC,EAAE,iBAAyB;QACrF,KAAK,CAAC,SAAS,CAAC,kBAAkB,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,iBAAiB,CAAC,CAAC,CAAC;QAE3I,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Exception } from "@noldova/context.exceptions";
|
|
9
|
+
export class ProcessesException extends Exception {
|
|
10
|
+
constructor(message, options) {
|
|
11
|
+
super(message, options);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=processes.exception.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processes.exception.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/exceptions/processes.exception.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAyB,MAAM,6BAA6B,CAAC;AAE/E,MAAM,OAAO,kBAAmB,SAAQ,SAAS;IAC/C,YAAmB,OAAe,EAAE,OAA0B;QAC5D,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC1B,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i-process-identity.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/interfaces/i-process-identity.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { ProcessesException } from "../exceptions/processes.exception.js";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
import { ProcessIdentity } from "./process-identity.js";
|
|
11
|
+
export class ProcessEntry {
|
|
12
|
+
processId;
|
|
13
|
+
parentId;
|
|
14
|
+
groupId;
|
|
15
|
+
isZombie;
|
|
16
|
+
startTime;
|
|
17
|
+
constructor(processId, parentId, groupId, isZombie, startTime) {
|
|
18
|
+
this.processId = processId;
|
|
19
|
+
this.parentId = parentId;
|
|
20
|
+
this.groupId = groupId;
|
|
21
|
+
this.isZombie = isZombie;
|
|
22
|
+
this.startTime = startTime;
|
|
23
|
+
}
|
|
24
|
+
get identity() {
|
|
25
|
+
return new ProcessIdentity(this.processId, this.startTime);
|
|
26
|
+
}
|
|
27
|
+
static parse(line) {
|
|
28
|
+
const fields = Resources.entryPattern.exec(line)?.groups;
|
|
29
|
+
if (Object.isUndefined(fields))
|
|
30
|
+
throw new ProcessesException(Resources.formatEntryInvalid(line));
|
|
31
|
+
return new ProcessEntry(Number(fields[Resources.processIdGroup]), Number(fields[Resources.parentIdGroup]), Number(fields[Resources.groupIdGroup]), String(fields[Resources.stateGroup]).startsWith(Resources.zombieState), Number(fields[Resources.startTimeGroup]));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=process-entry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-entry.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-entry.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAC;AAE1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,MAAM,OAAO,YAAY;IACP,SAAS,CAAS;IAClB,QAAQ,CAAS;IACjB,OAAO,CAAS;IAChB,QAAQ,CAAU;IAClB,SAAS,CAAS;IAElC,YAAoB,SAAiB,EAAE,QAAgB,EAAE,OAAe,EAAE,QAAiB,EAAE,SAAiB;QAC5G,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,eAAe,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IAC7D,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,IAAY;QAC9B,MAAM,MAAM,GAAG,SAAS,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QACzD,IAAI,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC;YAC5B,MAAM,IAAI,kBAAkB,CAAC,SAAS,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC;QAEnE,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,EAC/I,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,SAAS,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IACtH,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export class ProcessIdentityComparer {
|
|
9
|
+
static instance = new ProcessIdentityComparer();
|
|
10
|
+
equals(left, right) {
|
|
11
|
+
return left.processId === right.processId && left.startTime === right.startTime;
|
|
12
|
+
}
|
|
13
|
+
hash(value) {
|
|
14
|
+
return value.processId;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=process-identity-comparer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-identity-comparer.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-identity-comparer.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,MAAM,OAAO,uBAAuB;IAC3B,MAAM,CAAU,QAAQ,GAA4B,IAAI,uBAAuB,EAAE,CAAC;IAElF,MAAM,CAAC,IAAsB,EAAE,KAAuB;QAC3D,OAAO,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,IAAI,IAAI,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS,CAAC;IAClF,CAAC;IAEM,IAAI,CAAC,KAAuB;QACjC,OAAO,KAAK,CAAC,SAAS,CAAC;IACzB,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { ArgumentOutOfRangeException } from "@noldova/context.exceptions";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
import { ProcessLister } from "../services/process-lister.js";
|
|
11
|
+
export class ProcessIdentity {
|
|
12
|
+
processId;
|
|
13
|
+
startTime;
|
|
14
|
+
constructor(processId, startTime) {
|
|
15
|
+
ArgumentOutOfRangeException.throwIfNotPositiveInteger(processId, Resources.processIdParameterName);
|
|
16
|
+
ArgumentOutOfRangeException.throwIfNotInteger(startTime, Resources.startTimeParameterName);
|
|
17
|
+
ArgumentOutOfRangeException.throwIfNegative(startTime, Resources.startTimeParameterName);
|
|
18
|
+
this.processId = processId;
|
|
19
|
+
this.startTime = startTime;
|
|
20
|
+
}
|
|
21
|
+
static async readAsync(processId) {
|
|
22
|
+
ArgumentOutOfRangeException.throwIfNotPositiveInteger(processId, Resources.processIdParameterName);
|
|
23
|
+
const entries = await ProcessLister.useAsync(Resources.readLimitMilliseconds, t => t.listAsync());
|
|
24
|
+
return entries.firstOrDefault(t => t.processId === processId && !t.isZombie)?.identity;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=process-identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-identity.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-identity.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAC;AAG1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAE9D,MAAM,OAAO,eAAe;IACV,SAAS,CAAS;IAClB,SAAS,CAAS;IAElC,YAAmB,SAAiB,EAAE,SAAiB;QACrD,2BAA2B,CAAC,yBAAyB,CAAC,SAAS,EAAE,SAAS,CAAC,sBAAsB,CAAC,CAAC;QACnG,2BAA2B,CAAC,iBAAiB,CAAC,SAAS,EAAE,SAAS,CAAC,sBAAsB,CAAC,CAAC;QAC3F,2BAA2B,CAAC,eAAe,CAAC,SAAS,EAAE,SAAS,CAAC,sBAAsB,CAAC,CAAC;QAEzF,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,SAAiB;QAC7C,2BAA2B,CAAC,yBAAyB,CAAC,SAAS,EAAE,SAAS,CAAC,sBAAsB,CAAC,CAAC;QAEnG,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,QAAQ,CAAC,SAAS,CAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;QAClG,OAAO,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACzF,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { ProcessSignals } from "./process-signals.js";
|
|
9
|
+
export class ProcessListing {
|
|
10
|
+
file;
|
|
11
|
+
arguments;
|
|
12
|
+
scriptFileName;
|
|
13
|
+
script;
|
|
14
|
+
signals;
|
|
15
|
+
freezeRounds;
|
|
16
|
+
readyMilliseconds;
|
|
17
|
+
constructor(file, arguments_, scriptFileName, script, signals, freezeRounds, readyMilliseconds) {
|
|
18
|
+
this.file = file;
|
|
19
|
+
this.arguments = arguments_;
|
|
20
|
+
this.scriptFileName = scriptFileName;
|
|
21
|
+
this.script = script;
|
|
22
|
+
this.signals = signals;
|
|
23
|
+
this.freezeRounds = freezeRounds;
|
|
24
|
+
this.readyMilliseconds = readyMilliseconds;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=process-listing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-listing.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-listing.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,MAAM,OAAO,cAAc;IACT,IAAI,CAAS;IACb,SAAS,CAAoB;IAC7B,cAAc,CAAS;IACvB,MAAM,CAAS;IACf,OAAO,CAAiB;IACxB,YAAY,CAAS;IACrB,iBAAiB,CAAS;IAE1C,YACE,IAAY,EACZ,UAA6B,EAC7B,cAAsB,EACtB,MAAc,EACd,OAAuB,EACvB,YAAoB,EACpB,iBAAyB;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,UAAU,CAAC;QAC5B,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;IAC7C,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export class ProcessSignals {
|
|
9
|
+
stop;
|
|
10
|
+
resume;
|
|
11
|
+
terminate;
|
|
12
|
+
kill;
|
|
13
|
+
constructor(stop, resume, terminate, kill) {
|
|
14
|
+
this.stop = stop;
|
|
15
|
+
this.resume = resume;
|
|
16
|
+
this.terminate = terminate;
|
|
17
|
+
this.kill = kill;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=process-signals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-signals.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-signals.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,OAAO,cAAc;IACT,IAAI,CAA0B;IAC9B,MAAM,CAA0B;IAChC,SAAS,CAA0B;IACnC,IAAI,CAA0B;IAE9C,YAAmB,IAA6B,EAAE,MAA+B,EAAE,SAAkC,EAAE,IAA6B;QAClJ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { ArgumentOutOfRangeException } from "@noldova/context.exceptions";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
export class ProcessTreeEndOptions {
|
|
11
|
+
graceMilliseconds;
|
|
12
|
+
limitMilliseconds;
|
|
13
|
+
constructor(graceMilliseconds = Resources.defaultGraceMilliseconds, limitMilliseconds = Resources.defaultLimitMilliseconds) {
|
|
14
|
+
ProcessTreeEndOptions.throwIfNotMilliseconds(graceMilliseconds, Resources.graceMillisecondsParameterName);
|
|
15
|
+
ProcessTreeEndOptions.throwIfNotMilliseconds(limitMilliseconds, Resources.limitMillisecondsParameterName);
|
|
16
|
+
this.graceMilliseconds = graceMilliseconds;
|
|
17
|
+
this.limitMilliseconds = limitMilliseconds;
|
|
18
|
+
}
|
|
19
|
+
static throwIfNotMilliseconds(value, parameterName) {
|
|
20
|
+
ArgumentOutOfRangeException.throwIfNotInteger(value, parameterName);
|
|
21
|
+
ArgumentOutOfRangeException.throwIfNegative(value, parameterName);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=process-tree-end-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-tree-end-options.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-tree-end-options.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,OAAO,qBAAqB;IAChB,iBAAiB,CAAS;IAC1B,iBAAiB,CAAS;IAE1C,YAAmB,iBAAiB,GAAW,SAAS,CAAC,wBAAwB,EAAE,iBAAiB,GAAW,SAAS,CAAC,wBAAwB;QAC/I,qBAAqB,CAAC,sBAAsB,CAAC,iBAAiB,EAAE,SAAS,CAAC,8BAA8B,CAAC,CAAC;QAC1G,qBAAqB,CAAC,sBAAsB,CAAC,iBAAiB,EAAE,SAAS,CAAC,8BAA8B,CAAC,CAAC;QAE1G,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;IAC7C,CAAC;IAEO,MAAM,CAAC,sBAAsB,CAAC,KAAa,EAAE,aAAqB;QACxE,2BAA2B,CAAC,iBAAiB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;QACpE,2BAA2B,CAAC,eAAe,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Dictionary } from "@noldova/context.collections";
|
|
9
|
+
import { ProcessIdentityComparer } from "./process-identity-comparer.js";
|
|
10
|
+
export class ProcessTreeMembers {
|
|
11
|
+
root;
|
|
12
|
+
members;
|
|
13
|
+
constructor(root) {
|
|
14
|
+
this.root = root;
|
|
15
|
+
this.members = new Dictionary([], ProcessIdentityComparer.instance);
|
|
16
|
+
this.members.set(root, root);
|
|
17
|
+
}
|
|
18
|
+
get size() {
|
|
19
|
+
return this.members.count();
|
|
20
|
+
}
|
|
21
|
+
get identities() {
|
|
22
|
+
return this.members.values();
|
|
23
|
+
}
|
|
24
|
+
has(entry) {
|
|
25
|
+
return this.members.containsKey(entry);
|
|
26
|
+
}
|
|
27
|
+
collect(entries) {
|
|
28
|
+
for (const entry of entries.orderBy(t => t.startTime))
|
|
29
|
+
if (this.belongs(entry, entries))
|
|
30
|
+
this.members.set(entry, entry.identity);
|
|
31
|
+
}
|
|
32
|
+
belongs(entry, entries) {
|
|
33
|
+
return entry.groupId === this.root.processId ||
|
|
34
|
+
this.members.values().any(t => t.processId === entry.parentId && t.startTime <= entry.startTime && !ProcessTreeMembers.isReused(t, entry, entries));
|
|
35
|
+
}
|
|
36
|
+
static isReused(member, entry, entries) {
|
|
37
|
+
return entries.any(t => t.processId === member.processId && t.startTime > member.startTime && t.startTime <= entry.startTime);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=process-tree-members.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-tree-members.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/models/process-tree-members.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAuC,MAAM,8BAA8B,CAAC;AAK/F,OAAO,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,MAAM,OAAO,kBAAkB;IACZ,IAAI,CAAkB;IACtB,OAAO,CAAgD;IAExE,YAAmB,IAAqB;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,UAAU,CAAoC,EAAE,EAAE,uBAAuB,CAAC,QAAQ,CAAC,CAAC;QACvG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED,IAAW,IAAI;QACb,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;IAED,IAAW,UAAU;QACnB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;IAC/B,CAAC;IAEM,GAAG,CAAC,KAAmB;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IACzC,CAAC;IAEM,OAAO,CAAC,OAAoC;QACjD,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;YACnD,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC;gBAC9B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC9C,CAAC;IAEO,OAAO,CAAC,KAAmB,EAAE,OAAoC;QACvE,OAAO,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC,IAAI,CAAC,SAAS;YAC1C,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,KAAK,CAAC,QAAQ,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IACxJ,CAAC;IAEO,MAAM,CAAC,QAAQ,CAAC,MAAuB,EAAE,KAAmB,EAAE,OAAoC;QACxG,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,CAAC,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;IAChI,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noldova/context.processes",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "api/index.js",
|
|
6
|
+
"types": "api/index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/noldova-com/context.git",
|
|
11
|
+
"directory": "src/noldova.context.processes"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/noldova-com/context#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/noldova-com/context/issues"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"registry": "https://registry.npmjs.org/"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@noldova/context.collections": ">=0.0.14 <0.1.0",
|
|
23
|
+
"@noldova/context.core": ">=0.0.14 <0.1.0",
|
|
24
|
+
"@noldova/context.exceptions": ">=0.0.14 <0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"gitHead": "d6670c582cf942a35496df9bed0170c854f63d29"
|
|
27
|
+
}
|
package/resources.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Dictionary, KeyValuePair } from "@noldova/context.collections";
|
|
9
|
+
import { ProcessListing } from "./models/process-listing.js";
|
|
10
|
+
import { ProcessSignals } from "./models/process-signals.js";
|
|
11
|
+
export class Resources {
|
|
12
|
+
static endLine = "end";
|
|
13
|
+
static refusedPrefix = "refused ";
|
|
14
|
+
static lineSeparator = "\n";
|
|
15
|
+
static entryPattern = /^(?<processId>\d+)\s+(?<parentId>\d+)\s+(?<groupId>\d+)\s+(?<state>\S+)\s+(?<startTime>\d+)$/u;
|
|
16
|
+
static processIdGroup = "processId";
|
|
17
|
+
static parentIdGroup = "parentId";
|
|
18
|
+
static groupIdGroup = "groupId";
|
|
19
|
+
static stateGroup = "state";
|
|
20
|
+
static startTimeGroup = "startTime";
|
|
21
|
+
static zombieState = "Z";
|
|
22
|
+
static listSeparator = ", ";
|
|
23
|
+
static textEncoding = "utf8";
|
|
24
|
+
static closeEvent = "close";
|
|
25
|
+
static errorEvent = "error";
|
|
26
|
+
static lineEvent = "line";
|
|
27
|
+
static listerFolderPrefix = "noldova-process-lister-";
|
|
28
|
+
static removalRetries = 5;
|
|
29
|
+
static roundMilliseconds = 50;
|
|
30
|
+
static defaultGraceMilliseconds = 0;
|
|
31
|
+
static defaultLimitMilliseconds = 10_000;
|
|
32
|
+
static readLimitMilliseconds = 10_000;
|
|
33
|
+
static listerMarginMilliseconds = 5_000;
|
|
34
|
+
static listerReadyMilliseconds = 60_000;
|
|
35
|
+
static listingTries = 3;
|
|
36
|
+
static processIdParameterName = "processId";
|
|
37
|
+
static startTimeParameterName = "startTime";
|
|
38
|
+
static graceMillisecondsParameterName = "graceMilliseconds";
|
|
39
|
+
static limitMillisecondsParameterName = "limitMilliseconds";
|
|
40
|
+
static listingEnvironment = { LC_ALL: "C", TZ: "UTC0" };
|
|
41
|
+
static posixListingScript = [
|
|
42
|
+
`echo ${Resources.endLine}`,
|
|
43
|
+
"while read -r line; do",
|
|
44
|
+
" ps -A -o pid=,ppid=,pgid=,stat=,lstart= | awk 'BEGIN { months = \"JanFebMarAprMayJunJulAugSepOctNovDec\" } " +
|
|
45
|
+
"{ month = (index(months, $6) + 2) / 3; year = $9; if (month <= 2) { year -= 1; month += 12 } split($8, time, \":\"); " +
|
|
46
|
+
"days = 365 * year + int(year / 4) - int(year / 100) + int(year / 400) + int((153 * (month - 3) + 2) / 5) + $7 - 719469; " +
|
|
47
|
+
"printf \"%s %s %s %s %.0f\\n\", $1, $2, $3, $4, (((days * 24 + time[1]) * 60 + time[2]) * 60 + time[3]) * 1000 }'",
|
|
48
|
+
` echo ${Resources.endLine}`,
|
|
49
|
+
"done",
|
|
50
|
+
""
|
|
51
|
+
].join(Resources.lineSeparator);
|
|
52
|
+
static windowsListingScript = [
|
|
53
|
+
"$ErrorActionPreference = 'Stop'",
|
|
54
|
+
"try {",
|
|
55
|
+
" $module = [AppDomain]::CurrentDomain.DefineDynamicAssembly((New-Object Reflection.AssemblyName 'NoldovaProcessListing'), 'Run').DefineDynamicModule('NoldovaProcessListing')",
|
|
56
|
+
" $type = $module.DefineType('NoldovaProcessListing', 'Public, Abstract, Sealed')",
|
|
57
|
+
" $method = $type.DefinePInvokeMethod('NtQuerySystemInformation', 'ntdll.dll', 'Public, Static, PinvokeImpl', 'Standard', [int],",
|
|
58
|
+
" [Type[]] @([int], [IntPtr], [int], [int].MakeByRefType()), 'Winapi', 'Unicode')",
|
|
59
|
+
" $method.SetImplementationFlags('PreserveSig')",
|
|
60
|
+
" $native = $type.CreateType()",
|
|
61
|
+
"}",
|
|
62
|
+
`catch { Write-Output ('${Resources.refusedPrefix}' + ($_.Exception.Message -replace '\\s+', ' ')); exit 1 }`,
|
|
63
|
+
"$length = 1048576",
|
|
64
|
+
"function Get-Listing {",
|
|
65
|
+
" for (;;) {",
|
|
66
|
+
" $buffer = [Runtime.InteropServices.Marshal]::AllocHGlobal($script:length)",
|
|
67
|
+
" try {",
|
|
68
|
+
" $needed = 0",
|
|
69
|
+
" $status = $native::NtQuerySystemInformation(5, $buffer, $script:length, [ref] $needed)",
|
|
70
|
+
" if ($status -eq -1073741820) { $script:length = [Math]::Max($needed, $script:length * 2); continue }",
|
|
71
|
+
" if ($status -ne 0) { throw \"NtQuerySystemInformation returned $status\" }",
|
|
72
|
+
" $bytes = New-Object byte[] $script:length",
|
|
73
|
+
" [Runtime.InteropServices.Marshal]::Copy($buffer, $bytes, 0, $script:length)",
|
|
74
|
+
" }",
|
|
75
|
+
" finally { [Runtime.InteropServices.Marshal]::FreeHGlobal($buffer) }",
|
|
76
|
+
" $lines = New-Object Text.StringBuilder",
|
|
77
|
+
" $remainder = [long] 0",
|
|
78
|
+
" for ($offset = 0; ; $offset += $next) {",
|
|
79
|
+
" $created = [BitConverter]::ToInt64($bytes, $offset + 32)",
|
|
80
|
+
" if ([IntPtr]::Size -eq 8) { $id = [BitConverter]::ToInt64($bytes, $offset + 80); $parent = [BitConverter]::ToInt64($bytes, $offset + 88) }",
|
|
81
|
+
" else { $id = [BitConverter]::ToInt32($bytes, $offset + 68); $parent = [BitConverter]::ToInt32($bytes, $offset + 72) }",
|
|
82
|
+
" $start = if ($created -eq 0) { 0 } else { [Math]::DivRem($created - 116444736000000000, [long] 10000, [ref] $remainder) }",
|
|
83
|
+
" $null = $lines.Append($id).Append(' ').Append($parent).Append(' 0 R ').Append($start).Append(\"`n\")",
|
|
84
|
+
" $next = [BitConverter]::ToInt32($bytes, $offset)",
|
|
85
|
+
" if ($next -eq 0) { return $lines.ToString() }",
|
|
86
|
+
" }",
|
|
87
|
+
" }",
|
|
88
|
+
"}",
|
|
89
|
+
`[Console]::Out.WriteLine('${Resources.endLine}')`,
|
|
90
|
+
`while ($null -ne [Console]::In.ReadLine()) { [Console]::Out.Write((Get-Listing)); [Console]::Out.WriteLine('${Resources.endLine}') }`,
|
|
91
|
+
""
|
|
92
|
+
].join(Resources.lineSeparator);
|
|
93
|
+
static listings = new Dictionary([
|
|
94
|
+
new KeyValuePair("linux", new ProcessListing("sh", [], "list-processes.sh", Resources.posixListingScript, new ProcessSignals("SIGSTOP", "SIGCONT", "SIGTERM", "SIGKILL"), 10, Resources.listerReadyMilliseconds)),
|
|
95
|
+
new KeyValuePair("darwin", new ProcessListing("sh", [], "list-processes.sh", Resources.posixListingScript, new ProcessSignals("SIGSTOP", "SIGCONT", "SIGTERM", "SIGKILL"), 10, Resources.listerReadyMilliseconds)),
|
|
96
|
+
new KeyValuePair("win32", new ProcessListing("powershell.exe", ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File"], "list-processes.ps1", Resources.windowsListingScript, new ProcessSignals(0, 0, 0, "SIGKILL"), 1, Resources.listerReadyMilliseconds))
|
|
97
|
+
]);
|
|
98
|
+
static formatPlatformUnsupported(platform) {
|
|
99
|
+
return `Processes can't be listed on "${platform}"; Linux, macOS and Windows are supported.`;
|
|
100
|
+
}
|
|
101
|
+
static formatListerEnded(file) {
|
|
102
|
+
return `The process lister "${file}" ended before it answered.`;
|
|
103
|
+
}
|
|
104
|
+
static formatListerNotReady(file, limitMilliseconds) {
|
|
105
|
+
return `The process lister "${file}" was not ready within ${limitMilliseconds} ms.`;
|
|
106
|
+
}
|
|
107
|
+
static formatListerRefused(file, reason) {
|
|
108
|
+
return `The process lister "${file}" could not set up its listing, as the machine refused it: ${reason}`;
|
|
109
|
+
}
|
|
110
|
+
static formatListingIncomplete(file, tries) {
|
|
111
|
+
return `The process lister "${file}" left the process that called it out of every listing, so each was incomplete; listings made: ${tries}.`;
|
|
112
|
+
}
|
|
113
|
+
static formatEntryInvalid(line) {
|
|
114
|
+
return `The process lister answered "${line}", which is not a process id, parent id, group id, state and start time.`;
|
|
115
|
+
}
|
|
116
|
+
static formatTreeNotEnded(survivors, limitMilliseconds) {
|
|
117
|
+
return `The process tree still had ${survivors.count()} processes when its ${limitMilliseconds} ms limit passed: ${survivors.toArray().join(Resources.listSeparator)}.`;
|
|
118
|
+
}
|
|
119
|
+
static formatSurvivor(processId, startTime) {
|
|
120
|
+
return `${processId} started at ${new Date(startTime).toISOString()}`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=resources.js.map
|
package/resources.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resources.js","sourceRoot":"","sources":["../../../src/noldova.context.processes/src/resources.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAgD,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAEtH,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAE7D,MAAM,OAAO,SAAS;IACb,MAAM,CAAU,OAAO,GAAW,KAAK,CAAC;IACxC,MAAM,CAAU,aAAa,GAAW,UA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CAAC;IAEM,MAAM,CAAC,kBAAkB,CAAC,SAAgC,EAAE,iBAAyB;QAC1F,OAAO,8BAA8B,SAAS,CAAC,KAAK,EAAE,uBAAuB,iBAAiB,qBAAqB,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,CAAC;IAC1K,CAAC;IAEM,MAAM,CAAC,cAAc,CAAC,SAAiB,EAAE,SAAiB;QAC/D,OAAO,GAAG,SAAS,eAAe,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC;IACxE,CAAC;CACF"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
// This file uses Node APIs and will change when we replace Node.
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { once } from "node:events";
|
|
11
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
12
|
+
import { Socket } from "node:net";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { createInterface } from "node:readline";
|
|
16
|
+
import "@noldova/context.core";
|
|
17
|
+
import { Enumerable } from "@noldova/context.collections";
|
|
18
|
+
import { ProcessesException } from "../exceptions/processes.exception.js";
|
|
19
|
+
import { ProcessEntry } from "../models/process-entry.js";
|
|
20
|
+
import { Resources } from "../resources.js";
|
|
21
|
+
export class ListerProcess {
|
|
22
|
+
listing;
|
|
23
|
+
child;
|
|
24
|
+
ended;
|
|
25
|
+
answer;
|
|
26
|
+
lines;
|
|
27
|
+
turn;
|
|
28
|
+
constructor(child, listing, onEnded) {
|
|
29
|
+
this.listing = listing;
|
|
30
|
+
this.child = child;
|
|
31
|
+
this.answer = Promise.withResolvers();
|
|
32
|
+
this.lines = [];
|
|
33
|
+
this.turn = Promise.resolve();
|
|
34
|
+
this.ended = once(child, Resources.closeEvent).then(() => {
|
|
35
|
+
onEnded();
|
|
36
|
+
throw new ProcessesException(Resources.formatListerEnded(listing.file));
|
|
37
|
+
});
|
|
38
|
+
this.ended.catch(() => undefined);
|
|
39
|
+
void once(child.stdin, Resources.errorEvent);
|
|
40
|
+
createInterface({ input: child.stdout }).on(Resources.lineEvent, (line) => this.receive(line));
|
|
41
|
+
}
|
|
42
|
+
static async startAsync(listing, onEnded) {
|
|
43
|
+
const folder = await mkdtemp(join(tmpdir(), Resources.listerFolderPrefix));
|
|
44
|
+
try {
|
|
45
|
+
const script = join(folder, listing.scriptFileName);
|
|
46
|
+
await writeFile(script, listing.script, Resources.textEncoding);
|
|
47
|
+
const child = spawn(listing.file, [...listing.arguments, script], { env: { ...process.env, ...Resources.listingEnvironment }, stdio: ["pipe", "pipe", "ignore"], windowsHide: true });
|
|
48
|
+
const lister = new ListerProcess(child, listing, onEnded);
|
|
49
|
+
try {
|
|
50
|
+
await lister.waitReadyAsync();
|
|
51
|
+
}
|
|
52
|
+
catch (exception) {
|
|
53
|
+
lister.stop();
|
|
54
|
+
await Promise.allSettled([lister.ended]);
|
|
55
|
+
throw exception;
|
|
56
|
+
}
|
|
57
|
+
child.unref();
|
|
58
|
+
for (const stream of [child.stdin, child.stdout])
|
|
59
|
+
if (stream instanceof Socket)
|
|
60
|
+
stream.unref();
|
|
61
|
+
return lister;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
await rm(folder, { recursive: true, force: true, maxRetries: Resources.removalRetries });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
listAsync() {
|
|
68
|
+
const listed = this.turn.then(() => this.listOnceAsync());
|
|
69
|
+
this.turn = listed.catch(() => undefined);
|
|
70
|
+
return listed;
|
|
71
|
+
}
|
|
72
|
+
stop() {
|
|
73
|
+
this.child.kill();
|
|
74
|
+
}
|
|
75
|
+
async waitReadyAsync() {
|
|
76
|
+
const late = Promise.withResolvers();
|
|
77
|
+
const timer = setTimeout(() => late.resolve(false), this.listing.readyMilliseconds);
|
|
78
|
+
const isReady = await Promise.race([this.waitAsync().then(() => true), late.promise]).finally(() => clearTimeout(timer));
|
|
79
|
+
if (!isReady)
|
|
80
|
+
throw new ProcessesException(Resources.formatListerNotReady(this.listing.file, this.listing.readyMilliseconds));
|
|
81
|
+
}
|
|
82
|
+
async listOnceAsync() {
|
|
83
|
+
this.answer = Promise.withResolvers();
|
|
84
|
+
this.child.stdin.write(Resources.lineSeparator);
|
|
85
|
+
const lines = await this.waitAsync();
|
|
86
|
+
return Enumerable.from(lines).select(t => ProcessEntry.parse(t)).where(t => t.processId !== this.child.pid && t.parentId !== this.child.pid).toList();
|
|
87
|
+
}
|
|
88
|
+
waitAsync() {
|
|
89
|
+
return Promise.race([this.answer.promise, this.ended]);
|
|
90
|
+
}
|
|
91
|
+
receive(line) {
|
|
92
|
+
const text = line.trim();
|
|
93
|
+
if (text.startsWith(Resources.refusedPrefix)) {
|
|
94
|
+
this.answer.reject(new ProcessesException(Resources.formatListerRefused(this.listing.file, text.slice(Resources.refusedPrefix.length))));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (text !== Resources.endLine) {
|
|
98
|
+
this.lines.push(text);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const lines = this.lines;
|
|
102
|
+
this.lines = [];
|
|
103
|
+
this.answer.resolve(lines);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=lister-process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lister-process.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/services/lister-process.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,iEAAiE;AAEjE,OAAO,EAA4B,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,C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SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;YACzI,OAAO;QACT,CAAC;QAED,IAAI,IAAI,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC;YAC/B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACtB,OAAO;QACT,CAAC;QAED,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
// This file uses Node APIs and will change when we replace Node.
|
|
9
|
+
import "@noldova/context.core";
|
|
10
|
+
import { Dictionary } from "@noldova/context.collections";
|
|
11
|
+
import { ProcessesException } from "../exceptions/processes.exception.js";
|
|
12
|
+
import { ProcessEntry } from "../models/process-entry.js";
|
|
13
|
+
import { Resources } from "../resources.js";
|
|
14
|
+
import { ListerProcess } from "./lister-process.js";
|
|
15
|
+
export class ProcessLister {
|
|
16
|
+
static started = new Dictionary();
|
|
17
|
+
listing;
|
|
18
|
+
lister;
|
|
19
|
+
deadline;
|
|
20
|
+
constructor(lister, listing, deadline) {
|
|
21
|
+
this.lister = lister;
|
|
22
|
+
this.listing = listing;
|
|
23
|
+
this.deadline = deadline;
|
|
24
|
+
}
|
|
25
|
+
static async useAsync(limitMilliseconds, action, listings = Resources.listings) {
|
|
26
|
+
const listing = listings.tryGet(process.platform);
|
|
27
|
+
if (Object.isUndefined(listing))
|
|
28
|
+
throw new ProcessesException(Resources.formatPlatformUnsupported(process.platform));
|
|
29
|
+
const lister = await ProcessLister.startAsync(listing);
|
|
30
|
+
const deadline = performance.now() + limitMilliseconds;
|
|
31
|
+
const timer = setTimeout(lister.stop.bind(lister), limitMilliseconds + Resources.listerMarginMilliseconds);
|
|
32
|
+
try {
|
|
33
|
+
return await action(new ProcessLister(lister, listing, deadline));
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async listAsync() {
|
|
40
|
+
for (let tries = 1;; tries++) {
|
|
41
|
+
const entries = await this.lister.listAsync();
|
|
42
|
+
if (entries.any(t => t.processId === process.pid))
|
|
43
|
+
return entries;
|
|
44
|
+
if (tries === Resources.listingTries || performance.now() >= this.deadline)
|
|
45
|
+
throw new ProcessesException(Resources.formatListingIncomplete(this.listing.file, tries));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
static startAsync(listing) {
|
|
49
|
+
const started = ProcessLister.started.tryGet(listing);
|
|
50
|
+
if (!Object.isUndefined(started))
|
|
51
|
+
return started;
|
|
52
|
+
const starting = ListerProcess.startAsync(listing, () => ProcessLister.forget(listing, starting));
|
|
53
|
+
ProcessLister.started.set(listing, starting);
|
|
54
|
+
starting.catch(() => ProcessLister.forget(listing, starting));
|
|
55
|
+
return starting;
|
|
56
|
+
}
|
|
57
|
+
static forget(listing, starting) {
|
|
58
|
+
if (ProcessLister.started.tryGet(listing) === starting)
|
|
59
|
+
ProcessLister.started.remove(listing);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=process-lister.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-lister.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/services/process-lister.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,iEAAiE;AAEjE,OAAO,uBAAuB,CAAC;AAE/B,OAAO,EAAE,UAAU,EAAgD,MAAM,8BAA8B,CAAC;AAExG,OAAO,EAAE,kBAAkB,EAAE,MAAM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;QAC7E,IAAI,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,QAAQ;YACpD,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
// This file uses Node APIs and will change when we replace Node.
|
|
9
|
+
import { setTimeout } from "node:timers/promises";
|
|
10
|
+
import { List } from "@noldova/context.collections";
|
|
11
|
+
import { ProcessTreeEndException } from "../exceptions/process-tree-end.exception.js";
|
|
12
|
+
import { ProcessTreeEndOptions } from "../models/process-tree-end-options.js";
|
|
13
|
+
import { ProcessTreeMembers } from "../models/process-tree-members.js";
|
|
14
|
+
import { Resources } from "../resources.js";
|
|
15
|
+
import { ProcessLister } from "./process-lister.js";
|
|
16
|
+
export class ProcessTree {
|
|
17
|
+
lister;
|
|
18
|
+
root;
|
|
19
|
+
members;
|
|
20
|
+
isRootKept;
|
|
21
|
+
entries;
|
|
22
|
+
constructor(lister, root, isRootKept) {
|
|
23
|
+
this.lister = lister;
|
|
24
|
+
this.root = root;
|
|
25
|
+
this.members = new ProcessTreeMembers(root);
|
|
26
|
+
this.isRootKept = isRootKept;
|
|
27
|
+
this.entries = new List();
|
|
28
|
+
}
|
|
29
|
+
static endAsync(root, options = new ProcessTreeEndOptions()) {
|
|
30
|
+
return ProcessTree.endMembersAsync(root, options, false);
|
|
31
|
+
}
|
|
32
|
+
static endDescendantsAsync(root, options = new ProcessTreeEndOptions()) {
|
|
33
|
+
return ProcessTree.endMembersAsync(root, options, true);
|
|
34
|
+
}
|
|
35
|
+
static endMembersAsync(root, options, isRootKept) {
|
|
36
|
+
return ProcessLister.useAsync(options.limitMilliseconds, t => new ProcessTree(t, root, isRootKept).endTreeAsync(options, performance.now() + options.limitMilliseconds));
|
|
37
|
+
}
|
|
38
|
+
async endTreeAsync(options, deadline) {
|
|
39
|
+
this.entries = await this.lister.listAsync();
|
|
40
|
+
if (this.entries.any(t => t.processId === this.root.processId && t.startTime !== this.root.startTime))
|
|
41
|
+
return new List();
|
|
42
|
+
this.members.collect(this.entries);
|
|
43
|
+
for (let round = 0, count = 0; round < this.lister.listing.freezeRounds && this.members.size > count; round++) {
|
|
44
|
+
count = this.members.size;
|
|
45
|
+
await ProcessTree.signalAsync(this.lister.listing.signals.stop, this.alive());
|
|
46
|
+
await this.listAsync();
|
|
47
|
+
}
|
|
48
|
+
if (options.graceMilliseconds > 0)
|
|
49
|
+
await this.waitOutGraceAsync(performance.now() + options.graceMilliseconds);
|
|
50
|
+
for (;;) {
|
|
51
|
+
const alive = this.alive();
|
|
52
|
+
if (!alive.any())
|
|
53
|
+
return this.members.identities.where(t => !this.isKept(t.processId, t.startTime)).toList();
|
|
54
|
+
await ProcessTree.signalAsync(this.lister.listing.signals.kill, alive);
|
|
55
|
+
if (performance.now() >= deadline)
|
|
56
|
+
throw new ProcessTreeEndException(alive.select(t => t.identity).toList(), options.limitMilliseconds);
|
|
57
|
+
await setTimeout(Resources.roundMilliseconds);
|
|
58
|
+
await this.listAsync();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async waitOutGraceAsync(graceEnd) {
|
|
62
|
+
await ProcessTree.signalAsync(this.lister.listing.signals.terminate, this.alive());
|
|
63
|
+
await ProcessTree.signalAsync(this.lister.listing.signals.resume, this.alive());
|
|
64
|
+
while (this.alive().any() && performance.now() < graceEnd) {
|
|
65
|
+
await setTimeout(Resources.roundMilliseconds);
|
|
66
|
+
await this.listAsync();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
static async signalAsync(signal, entries) {
|
|
70
|
+
await Promise.allSettled(entries.select(async (t) => process.kill(t.processId, signal)));
|
|
71
|
+
}
|
|
72
|
+
async listAsync() {
|
|
73
|
+
this.entries = await this.lister.listAsync();
|
|
74
|
+
this.members.collect(this.entries);
|
|
75
|
+
}
|
|
76
|
+
alive() {
|
|
77
|
+
return this.entries.where(t => !t.isZombie && this.members.has(t) && !this.isKept(t.processId, t.startTime)).toList();
|
|
78
|
+
}
|
|
79
|
+
isKept(processId, startTime) {
|
|
80
|
+
return this.isRootKept && processId === this.root.processId && startTime === this.root.startTime;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=process-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-tree.js","sourceRoot":"","sources":["../../../../src/noldova.context.processes/src/services/process-tree.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,iEAAiE;AAEjE,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAuC,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAEzF,OAAO,EAAE,uBAAuB,EAAE,MAAM,6CAA6C,CAAC;AAGtF,OAAO,EAAE,qBAAqB,EAAE,MAAM,uCAAuC,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,MAAM,OAAO,WAAW;IACL,MAAM,CAAgB;IACtB,IAAI,CAAkB;IACtB,OAAO,CAAqB;IAC5B,UAAU,CAAU;IAC7B,OAAO,CAA8B;IAE7C,YAAoB,MAAqB,EAAE,IAAqB,EAAE,UAAmB;QACnF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAC;QAC5C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAA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