This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathworker.js
163 lines (146 loc) · 5.03 KB
/
worker.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const qs = require('querystring');
const {remote, ipcRenderer: ipc} = require('electron');
const {GitProcess} = require('dugite');
class AverageTracker {
constructor({limit} = {limit: 10}) {
// for now this serves a dual purpose - # of values tracked AND # discarded prior to starting avg calculation
this.limit = limit;
this.sum = 0;
this.values = [];
}
addValue(value) {
if (this.values.length >= this.limit) {
const discardedValue = this.values.shift();
this.sum -= discardedValue;
}
this.values.push(value);
this.sum += value;
}
getAverage() {
if (this.enoughData()) {
return this.sum / this.limit;
} else {
return null;
}
}
getLimit() {
return this.limit;
}
enoughData() {
return this.values.length === this.limit;
}
}
const query = qs.parse(window.location.search.substr(1));
const sourceWebContentsId = remote.getCurrentWindow().webContents.id;
const operationCountLimit = parseInt(query.operationCountLimit, 10);
const averageTracker = new AverageTracker({limit: operationCountLimit});
const childPidsById = new Map();
const destroyRenderer = () => {
if (!managerWebContents.isDestroyed()) {
managerWebContents.removeListener('crashed', destroyRenderer);
managerWebContents.removeListener('destroyed', destroyRenderer);
}
const win = remote.BrowserWindow.fromWebContents(remote.getCurrentWebContents());
if (win && !win.isDestroyed()) {
win.destroy();
}
};
const managerWebContentsId = parseInt(query.managerWebContentsId, 10);
const managerWebContents = remote.webContents.fromId(managerWebContentsId);
if (managerWebContents && !managerWebContents.isDestroyed()) {
managerWebContents.on('crashed', destroyRenderer);
managerWebContents.on('destroyed', destroyRenderer);
window.onbeforeunload = () => {
managerWebContents.removeListener('crashed', destroyRenderer);
managerWebContents.removeListener('destroyed', destroyRenderer);
};
}
const channelName = query.channelName;
ipc.on(channelName, (event, {type, data}) => {
if (type === 'git-exec') {
const {args, workingDir, options, id} = data;
if (args) {
document.getElementById('command').textContent = `git ${args.join(' ')}`;
}
options.processCallback = child => {
childPidsById.set(id, child.pid);
child.on('error', err => {
event.sender.sendTo(managerWebContentsId, channelName, {
sourceWebContentsId,
type: 'git-spawn-error',
data: {id, err},
});
});
child.stdin.on('error', err => {
event.sender.sendTo(managerWebContentsId, channelName, {
sourceWebContentsId,
type: 'git-stdin-error',
data: {id, stdin: options.stdin, err},
});
});
};
const spawnStart = performance.now();
GitProcess.exec(args, workingDir, options)
.then(({stdout, stderr, exitCode}) => {
const timing = {
spawnTime: spawnEnd - spawnStart,
execTime: performance.now() - spawnEnd,
};
childPidsById.delete(id);
event.sender.sendTo(managerWebContentsId, channelName, {
sourceWebContentsId,
type: 'git-data',
data: {
id,
average: averageTracker.getAverage(),
results: {stdout, stderr, exitCode, timing},
},
});
}, err => {
const timing = {
spawnTime: spawnEnd - spawnStart,
execTime: performance.now() - spawnEnd,
};
childPidsById.delete(id);
event.sender.sendTo(managerWebContentsId, channelName, {
sourceWebContentsId,
type: 'git-data',
data: {
id,
average: averageTracker.getAverage(),
results: {
stdout: err.stdout,
stderr: err.stderr,
exitCode: err.code,
signal: err.signal,
timing,
},
},
});
});
const spawnEnd = performance.now();
averageTracker.addValue(spawnEnd - spawnStart);
// TODO: consider using this to avoid duplicate write operations upon crashing.
// For now we won't do this to avoid clogging up ipc channel
// event.sender.sendTo(managerWebContentsId, channelName, {sourceWebContentsId, type: 'exec-started', data: {id}});
if (averageTracker.enoughData() && averageTracker.getAverage() > 20) {
event.sender.sendTo(managerWebContentsId, channelName, {type: 'slow-spawns'});
}
} else if (type === 'git-cancel') {
const {id} = data;
const childPid = childPidsById.get(id);
if (childPid !== undefined) {
require('tree-kill')(childPid, 'SIGINT', () => {
event.sender.sendTo(managerWebContentsId, channelName, {
sourceWebContentsId,
type: 'git-cancelled',
data: {id, childPid},
});
});
childPidsById.delete(id);
}
} else {
throw new Error(`Could not identify type ${type}`);
}
});
ipc.sendTo(managerWebContentsId, channelName, {sourceWebContentsId, type: 'renderer-ready', data: {pid: process.pid}});