Skip to content

Commit 02d538e

Browse files
committed
Fix blocking CLI queue for rebuilds
1 parent 00efba2 commit 02d538e

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

templates/cli/lib/commands/run.js.twig

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,6 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
150150

151151
await dockerPull(func);
152152

153-
log('Building function using Docker ...');
154-
await dockerBuild(func, variables);
155-
156-
log('Starting function using Docker ...');
157-
hint('Function automatically restarts when you edit your code.');
158-
159-
await dockerStart(func, variables, port);
160-
161153
new Tail(logsPath).on("line", function(data) {
162154
process.stdout.write(chalk.white(`${data}\n`));
163155
});
@@ -183,8 +175,14 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
183175

184176
const dependencyFile = files.find((filePath) => tool.dependencyFiles.includes(filePath));
185177
if(tool.isCompiled || dependencyFile) {
186-
log(`Rebuilding the function ...`);
178+
log(`Rebuilding the function due to file changes ...`);
187179
await dockerBuild(func, variables);
180+
181+
if(!Queue.isEmpty()) {
182+
Queue.unlock();
183+
return;
184+
}
185+
188186
await dockerStart(func, variables, port);
189187
} else {
190188
log('Hot-swapping function.. Files with change are ' + files.join(', '));
@@ -249,6 +247,22 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
249247
Queue.unlock();
250248
}
251249
});
250+
251+
Queue.lock();
252+
253+
log('Building function using Docker ...');
254+
await dockerBuild(func, variables);
255+
256+
if(!Queue.isEmpty()) {
257+
Queue.unlock();
258+
return;
259+
}
260+
261+
log('Starting function using Docker ...');
262+
hint('Function automatically restarts when you edit your code.');
263+
await dockerStart(func, variables, port);
264+
265+
Queue.unlock();
252266
}
253267

254268
const run = new Command("run")

templates/cli/lib/emulation/docker.js.twig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { localConfig } = require("../config");
44
const path = require('path');
55
const fs = require('fs');
66
const { log, success, hint } = require("../parser");
7-
const { openRuntimesVersion, systemTools } = require("./utils");
7+
const { openRuntimesVersion, systemTools, Queue } = require("./utils");
88

99
async function dockerStop(id) {
1010
const stopProcess = childProcess.spawn('docker', ['rm', '--force', id], {
@@ -94,8 +94,25 @@ async function dockerBuild(func, variables) {
9494
process.stderr.write(chalk.blackBright(`${data}\n`));
9595
});
9696

97+
const killInterval = setInterval(() => {
98+
if(!Queue.isEmpty()) {
99+
log('Cancelling build ...');
100+
buildProcess.stdout.destroy();
101+
buildProcess.stdin.destroy();
102+
buildProcess.stderr.destroy();
103+
buildProcess.kill("SIGKILL");
104+
clearInterval(killInterval);
105+
}
106+
}, 100);
107+
97108
await new Promise((res) => { buildProcess.on('close', res) });
98109

110+
clearInterval(interval);
111+
112+
if(!Queue.isEmpty()) {
113+
return;
114+
}
115+
99116
const copyPath = path.join(process.cwd(), func.path, '.appwrite', 'build.tar.gz');
100117
const copyDir = path.dirname(copyPath);
101118
if (!fs.existsSync(copyDir)) {

templates/cli/lib/emulation/utils.js.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ const Queue = {
157157
this.files = [];
158158
this.locked = true;
159159
},
160+
isEmpty() {
161+
return this.files.length == 0
162+
},
160163
unlock() {
161164
this.locked = false;
162165
if(this.files.length > 0) {

0 commit comments

Comments
 (0)