1
+ const net = require('net');
1
2
const chalk = require('chalk');
2
3
const childProcess = require('child_process');
3
4
const { localConfig } = require("../config");
4
5
const path = require('path');
5
6
const fs = require('fs');
6
- const { log, success, hint } = require("../parser");
7
+ const { log, error, success, hint } = require("../parser");
7
8
const { openRuntimesVersion, systemTools, Queue } = require("./utils");
8
9
9
10
async function dockerStop(id) {
@@ -168,6 +169,13 @@ async function dockerStart(func, variables, port) {
168
169
process.stdout.write(chalk.blackBright(data));
169
170
});
170
171
172
+ try {
173
+ await waitUntilPortOpen(port);
174
+ } catch(err) {
175
+ error("Failed to start function with error: " + err.message ? err.message : err.toString());
176
+ return;
177
+ }
178
+
171
179
success(`Visit http://localhost:${port}/ to execute your function.`);
172
180
}
173
181
@@ -186,6 +194,39 @@ async function dockerCleanup(functionId) {
186
194
}
187
195
}
188
196
197
+ function waitUntilPortOpen(port, iteration = 0) {
198
+ return new Promise((resolve, reject) => {
199
+ const client = new net.Socket();
200
+
201
+ client.once('connect', () => {
202
+ client.removeAllListeners('connect');
203
+ client.removeAllListeners('error');
204
+ client.end();
205
+ client.destroy();
206
+ client.unref();
207
+
208
+ resolve();
209
+ });
210
+
211
+ client.once('error', async (err) => {
212
+ client.removeAllListeners('connect');
213
+ client.removeAllListeners('error');
214
+ client.end();
215
+ client.destroy();
216
+ client.unref();
217
+
218
+ if(iteration > 100) {
219
+ reject(err);
220
+ } else {
221
+ await new Promise((res) => setTimeout(res, 100));
222
+ waitUntilPortOpen(port, iteration + 1).then(resolve).catch(reject);
223
+ }
224
+ });
225
+
226
+ client.connect({port, host: '127.0.0.1'}, function() {});
227
+ });
228
+ }
229
+
189
230
module.exports = {
190
231
dockerPull,
191
232
dockerBuild,
0 commit comments