Skip to content

Commit 3dd477c

Browse files
committed
feat(cli): changes to run to work better with detached stdin (no shell)
1 parent 1ac383d commit 3dd477c

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

.changeset/tame-cobras-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cartesi/cli": patch
3+
---
4+
5+
changes to run to work better with detached stdin (no shell)

apps/cli/src/commands/run.ts

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,17 @@ const commaSeparatedList = (value: string) => value.split(",");
3838

3939
const shell = async (options: {
4040
build?: CommandUnknownOpts;
41+
deployment?: RollupsDeployment;
4142
epochLength: number;
4243
log?: CommandUnknownOpts;
4344
projectName: string;
4445
prt?: boolean;
46+
salt: number;
4547
}) => {
4648
const { build, epochLength, log, projectName, prt } = options;
4749

48-
// keep track of last deployment
49-
let lastDeployment: RollupsDeployment | undefined;
50-
let salt = 0;
51-
52-
// deploy for the first time
53-
const hash = getMachineHash();
54-
if (hash) {
55-
lastDeployment = await deploy({
56-
epochLength,
57-
hash,
58-
projectName,
59-
prt,
60-
salt: numberToHex(salt++, { size: 32 }),
61-
});
62-
} else {
63-
console.warn(
64-
chalk.yellow("machine snapshot not found, waiting for build"),
65-
);
66-
}
50+
let lastDeployment = options.deployment;
51+
let salt = options.salt;
6752

6853
while (true) {
6954
try {
@@ -346,6 +331,27 @@ export const createRunCommand = () => {
346331
services,
347332
});
348333

334+
// deploy the application
335+
let deployment: RollupsDeployment | undefined;
336+
let salt = 0;
337+
const prt = !authority;
338+
const hash = getMachineHash();
339+
if (hash) {
340+
deployment = await deploy({
341+
epochLength,
342+
hash,
343+
projectName,
344+
prt,
345+
salt: numberToHex(salt++, { size: 32 }),
346+
});
347+
} else {
348+
console.warn(
349+
chalk.yellow(
350+
"machine snapshot not found, waiting for build",
351+
),
352+
);
353+
}
354+
349355
const shutdown = async () => {
350356
progress.start(`${chalk.cyan(projectName)} stopping...`);
351357
try {
@@ -359,23 +365,40 @@ export const createRunCommand = () => {
359365
process.exit(0);
360366
};
361367

362-
// inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
363-
process.on("SIGINT", () => {});
364-
process.on("SIGTERM", () => {});
368+
if (process.stdin.isTTY) {
369+
// inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
370+
process.on("SIGINT", () => {});
371+
process.on("SIGTERM", () => {});
365372

366-
const log = program.parent?.commands.find(
367-
(c) => c.name() === "logs",
368-
);
369-
const build = program.parent?.commands.find(
370-
(c) => c.name() === "build",
371-
);
372-
await shell({
373-
build,
374-
epochLength,
375-
log,
376-
projectName,
377-
prt: !authority,
378-
});
379-
await shutdown();
373+
const log = program.parent?.commands.find(
374+
(c) => c.name() === "logs",
375+
);
376+
const build = program.parent?.commands.find(
377+
(c) => c.name() === "build",
378+
);
379+
await shell({
380+
build,
381+
deployment,
382+
epochLength,
383+
log,
384+
projectName,
385+
prt,
386+
salt,
387+
});
388+
await shutdown();
389+
} else {
390+
// non-interactive mode: wait for SIGINT or SIGTERM to shutdown
391+
await new Promise<void>((resolve) => {
392+
// keep the event loop alive
393+
const keepAlive = setInterval(() => {}, 1 << 30);
394+
const handler = () => {
395+
clearInterval(keepAlive);
396+
resolve();
397+
};
398+
process.on("SIGINT", handler);
399+
process.on("SIGTERM", handler);
400+
});
401+
await shutdown();
402+
}
380403
});
381404
};

0 commit comments

Comments
 (0)