Skip to content

Commit 2aa999d

Browse files
committed
fix: improve chrome restart routine
1 parent 8858ebd commit 2aa999d

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/utils/chrome.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Chrome {
3838

3939
// chrome instance
4040
this.launcher = null;
41+
this.restarting = null;
4142

4243
// settings
4344
this.settings = Object.assign({
@@ -62,6 +63,22 @@ class Chrome {
6263
process.on(_SIGINT, async () => this.kill());
6364
}
6465

66+
/**
67+
* Verifies that chrome has started
68+
* @return {Promise}
69+
*/
70+
async waitForStart() {
71+
if (this.starting == null) {
72+
return;
73+
}
74+
75+
try {
76+
await this.starting;
77+
} catch (e) {
78+
throw new HttpStatusError(502, 'chrome failed to start');
79+
}
80+
}
81+
6582
/**
6683
* Launches a debugging instance of Chrome on port 9222.
6784
* @return {Promise<Chrome>}
@@ -73,10 +90,13 @@ class Chrome {
7390
}
7491

7592
try {
76-
this.launcher = await Chrome.launch(this.settings);
93+
this.starting = Chrome.launch(this.settings);
94+
this.launcher = await this.starting;
7795
} catch (e) {
7896
this.launcher = null;
7997
throw e;
98+
} finally {
99+
this.starting = null;
80100
}
81101

82102
return this;
@@ -87,6 +107,12 @@ class Chrome {
87107
* @returns {Promise<null>}
88108
*/
89109
async kill() {
110+
try {
111+
await this.waitForStart();
112+
} catch (e) {
113+
return;
114+
}
115+
90116
const { launcher } = this;
91117

92118
// kill chrome instance if has been launched
@@ -97,11 +123,9 @@ class Chrome {
97123
}
98124

99125
async status(attempt = 0) {
100-
this.log.debug('evaluating chrome health');
126+
await this.waitForStart();
101127

102-
if (this.launcher == null) {
103-
throw new HttpStatusError(502, 'chrome not started');
104-
}
128+
this.log.debug('evaluating chrome health');
105129

106130
try {
107131
await this.printToPdf(kStatusCheckHTML);
@@ -129,7 +153,9 @@ class Chrome {
129153
* Opens a new Chrome tab
130154
* @return {Promise<ChromeDebugProtocol>}
131155
*/
132-
openTab() {
156+
async openTab() {
157+
await this.waitForStart();
158+
133159
return Promise
134160
.bind(ChromeRemote, this.launcher)
135161
.then(ChromeRemote.New)

0 commit comments

Comments
 (0)