Skip to content

Commit c3c060a

Browse files
Merge pull request #902 from appwrite/fix-build-blocking-queue
Fix: Blocking build queue in local dev
2 parents 50ab002 + 057bc58 commit c3c060a

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], {
@@ -93,8 +93,25 @@ async function dockerBuild(func, variables) {
9393
process.stderr.write(chalk.blackBright(`${data}\n`));
9494
});
9595

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

109+
clearInterval(interval);
110+
111+
if(!Queue.isEmpty()) {
112+
return;
113+
}
114+
98115
const copyPath = path.join(process.cwd(), func.path, '.appwrite', 'build.tar.gz');
99116
const copyDir = path.dirname(copyPath);
100117
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
@@ -156,6 +156,9 @@ const Queue = {
156156
this.files = [];
157157
this.locked = true;
158158
},
159+
isEmpty() {
160+
return this.files.length === 0
161+
},
159162
unlock() {
160163
this.locked = false;
161164
if(this.files.length > 0) {

0 commit comments

Comments
 (0)