Skip to content

Commit a63cfc2

Browse files
authored
Fix/timeout progress combo (#900)
Add support for disabling the use of data being received on the stdOut, stdErr or both streams of the underlying git process as a reason to reset the block timeout of the timeout plugin.
1 parent c5ac28a commit a63cfc2

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

.changeset/eight-apes-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'simple-git': minor
3+
---
4+
5+
Timeout plugin can now be configured to ignore data on either stdOut or stdErr in the git process when determining whether to kill the spawned process.

docs/PLUGIN-TIMEOUT.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ To handle the case where the underlying `git` processes appear to hang, configur
88
import { simpleGit, GitPluginError, SimpleGit, SimpleGitProgressEvent } from 'simple-git';
99

1010
const git: SimpleGit = simpleGit({
11-
baseDir: '/some/path',
1211
timeout: {
1312
block: 2000,
1413
},
@@ -25,3 +24,55 @@ catch (err) {
2524
}
2625
}
2726
```
27+
28+
## Task Timeouts and Progress Events
29+
30+
The default behaviour of the timeout plugin is to listen for data being received on both the
31+
`stdOut` and `stdErr` streams from the `git` child process.
32+
33+
When using the `progress` plugin, `git` will be streaming regular progress updates to `stdErr`,
34+
so you may see that the timeout is never reached and `simple-git` patiently waits for `git` to
35+
finish whatever it is doing.
36+
37+
Configure this with the optional `stdOut` and `stdErr` properties of the `timeout` plugin
38+
configuration:
39+
40+
```typescript
41+
import { simpleGit, SimpleGit } from "simple-git";
42+
43+
const git: SimpleGit = simpleGit({
44+
progress({method, stage, progress}) {
45+
console.log(`git.${method} ${stage} stage ${progress}% complete`);
46+
},
47+
timeout: {
48+
block: 2000,
49+
stdOut: true, // default behaviour, resets the 2s timer every time data arrives on stdOut
50+
stdErr: false // custom behaviour, ignore the progress events being written to stdErr
51+
}
52+
});
53+
54+
```
55+
56+
## Absolute or Block Timeouts
57+
58+
The timeout plugin will reset its timers whenever data is received meaning the plugin will
59+
only kill processes that appear to be hanging and allow intentionally long-running processes
60+
to continue uninterrupted.
61+
62+
To change this default behaviour so that the plugin kills all processes after the supplied
63+
timeout, configure it so the plugin doesn't listen for data updates by supplying the optional
64+
`stdOut` and `stdErr` properties of the `timeout` plugin configuration:
65+
66+
```typescript
67+
import { simpleGit, SimpleGit } from "simple-git";
68+
69+
// create a simple-git instance that kills any process after 5s
70+
// whether it's still receiving data or not:
71+
const git: SimpleGit = simpleGit({
72+
timeout: {
73+
block: 5000,
74+
stdOut: false,
75+
stdErr: false
76+
}
77+
});
78+
```

simple-git/src/lib/plugins/timout-plugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { GitPluginError } from '../errors/git-plugin-error';
55

66
export function timeoutPlugin({
77
block,
8+
stdErr = true,
9+
stdOut = true,
810
}: Exclude<SimpleGitOptions['timeout'], undefined>): SimpleGitPlugin<'spawn.after'> | void {
911
if (block > 0) {
1012
return {
@@ -30,8 +32,8 @@ export function timeoutPlugin({
3032
context.kill(new GitPluginError(undefined, 'timeout', `block timeout reached`));
3133
}
3234

33-
context.spawned.stdout?.on('data', wait);
34-
context.spawned.stderr?.on('data', wait);
35+
stdOut && context.spawned.stdout?.on('data', wait);
36+
stdErr && context.spawned.stderr?.on('data', wait);
3537
context.spawned.on('exit', stop);
3638
context.spawned.on('close', stop);
3739

simple-git/src/lib/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ export interface SimpleGitPluginConfig {
105105
* content on the stdOut/stdErr streams before forcibly closing the git process.
106106
*/
107107
block: number;
108+
109+
/**
110+
* Reset timeout plugin after receiving data on `stdErr` - set to `false` to ignore
111+
* `stdErr` content when determining whether to kill the process (defaults to `true`).
112+
*/
113+
stdErr?: boolean;
114+
115+
/**
116+
* Reset timeout plugin after receiving data on `stdOut` - set to `false` to ignore
117+
* `stdOut` content when determining whether to kill the process (defaults to `true`).
118+
*/
119+
stdOut?: boolean;
108120
};
109121

110122
spawnOptions: Pick<SpawnOptions, 'uid' | 'gid'>;

0 commit comments

Comments
 (0)