Skip to content

Commit c54ff44

Browse files
committed
Update README
document new warmUp options add more documentation
1 parent 1f2f308 commit c54ff44

1 file changed

Lines changed: 87 additions & 2 deletions

File tree

README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,64 @@ To read a detailed info please visit [windows-kill-library Readme](https://githu
2222

2323
### Limitations
2424
To send the signal, **windows-kill** at first send a same signal to the process that is calling it, to find a thread address. Then the founded address is used to send the real signal. Because of this, the process that is sending the signal will get the same signal too. But windows-kill register a signal handle during this procedure, so the process will not terminate. But if the process that is sending signal has child process, or is a child process of another process, sending signal will trigger the signal handles in other process in the same process group. And the default behavior of Windows console/application in case of getting a ```SIGINT``` or ```SIGBREAK```, is to terminate.
25+
2526
To **sum up**, If you are sending signal in node app that has child process (any kind of it), or is a child process of another process, the result is the termination of all the processes in the same process group, except the sender (well if it's a child process, because the master is terminated, it will terminate too).
2627

2728
**PS**: Currently there is no solution for this problem. But I'm working on it, to find a solution.
2829

30+
**PS-1**: A solution for parent processes that wants to send signal (no way for child processes currently), is added. It's setting the ```warmUp: true``` option when first calling the windows-kill in parent, before any child processes creation.
31+
2932
## Usage
3033
```windows-kill``` expose a function. Simply run the exposed function with/without the options. Thats it. It should be called before any usage of ```process.kill```.
3134

3235
By default, ```windows-kill``` will enhance the node's process.kill in a way, that no code changes are needed in your codebase. Enhance means that ```windows-kill``` will replace the node's ```process.kill``` with a custom function with the same arguments and functionality. Just some changes to achieve signaling in Windows.
3336

3437
### Simplest usage
38+
The returned function from calling the exported function, could be used to send signal, just like the way you call ```process.kill```. It will accept two argument. a ```PID``` and a ```SIGNAL```.
39+
3540
```javascript
41+
/*
42+
Require and call the function that's exported.
43+
The returned function can be used to send signal.
44+
*/
3645
var windowsKill = require('windows-kill')();
3746

47+
/*
48+
By default, process.kill is enhanced (only in Windows OS),
49+
so you can either call process.kill, or the returned function.
50+
*/
51+
process.kill(PID, SIGNAL);
52+
windowsKill(PID, SIGNAL);
53+
```
54+
55+
```javascript
56+
/*
57+
Just call the function, and no need to change any code.
58+
Every process.kill calls in your code, will now enhanced.
59+
Just need to call windows-kill, before any call of
60+
process.kill.
61+
*/
62+
require('windows-kill')();
63+
3864
process.kill(PID, SIGNAL);
3965
```
4066

41-
### Using Options
67+
### Options
68+
Options and default values are:
69+
70+
```javascript
71+
const defaultOptions = {
72+
replaceNodeKill: true,
73+
warmUp: false
74+
};
75+
```
76+
77+
#### replaceNodeKill
78+
This option will tell the windows-kill that should it enhance/replace the nodes process.kill? By setting true (default value), the nodes process.kill will replaced by a custom function. This custom function will check the signal that is sending using process.kill. If the signal is a member of supported signals, which is ```SIGINT``` or ```SIGBREAK```, it will call the modules function that is responsible for sending signal. Otherwise, the signal and pid will be passed to the node's original process.kill.
79+
4280
```javascript
4381
const options = {
44-
replaceNodeKill: true // Should windows kill enhance/replace node's process.kill. Default: true
82+
replaceNodeKill: true, // Should windows-kill enhance/replace node's process.kill? Default: true
4583
};
4684

4785
require('windows-kill')(options);
@@ -59,6 +97,53 @@ var windowsKill = require('windows-kill')(options);
5997
windowsKill(PID, SIGNAL);
6098
```
6199

100+
#### warmUp
101+
By setting warmUp to ```true```, windows-kill will find and save the ```ctrl-routine``` addresses, without any need to send signal. By default, the address will find, when the first signal of that type is sending. Future call will use the founded address.
102+
103+
```javascript
104+
const options = {
105+
warmUp: true, // Should windows-kill warm-up by finding the ctrl-routines addresses? Default: false
106+
};
107+
108+
require('windows-kill')(options);
109+
110+
process.kill(PID, SIGNAL);
111+
```
112+
113+
Warm-up, is one way to overcome the limitations. Setting this option, will make the windows-kill to find needed address, before any signal sending. As stated in limitations section, finding address will cause the processes that have child process, or is a child process, trigger the ```ctr-routine``` of all process group members, which means termination of all of them. But warm-up mechanism can be used, to fix the issue in parent process. By setting it to ```true```, before any child process creation, sending signal in future will use the save addresses and no need to find addresses again.
114+
115+
To clear it up, see below example:
116+
117+
```javascript
118+
var options = {
119+
warmUp: false
120+
};
121+
122+
var cp = require('child_process');
123+
var windowsKill = require('windows-kill')(options);
124+
125+
var cp1 = cp.spawn('node', ['cp.js']);
126+
127+
windowsKill(PID, 'SIGINT');
128+
```
129+
130+
By running the above code, the cp1 child process will terminate. Because the ```SIGINT``` signal is sent for the first time, and the ```ctrl-routine``` address in not available. So windows-kill will try to find it, and trying to find it will trigger the ```SIGINT``` handler of cp1 child process, which lead to termination of cp1.
131+
132+
To solve the issue, we can set the ```warmUp``` option true. Just remember, the initialization of windows-kill with warmUp option should be done before any child process creation. Like below:
133+
134+
```javascript
135+
var options = {
136+
warmUp: true
137+
};
138+
139+
var cp = require('child_process');
140+
var windowsKill = require('windows-kill')(options);
141+
142+
var cp1 = cp.spawn('node', ['cp.js']);
143+
144+
windowsKill(PID, 'SIGINT');
145+
```
146+
62147
## Contributing
63148
We love contributions from everyone. Please read [Contributing guide](https://github.com/alirdn/node-windows-kill/blob/master/CONTRIBUTING.md).
64149

0 commit comments

Comments
 (0)