Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-cobras-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cartesi/cli": patch
---

changes to run to work better with detached stdin (no shell)
95 changes: 59 additions & 36 deletions apps/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,17 @@ const commaSeparatedList = (value: string) => value.split(",");

const shell = async (options: {
build?: CommandUnknownOpts;
deployment?: RollupsDeployment;
epochLength: number;
log?: CommandUnknownOpts;
projectName: string;
prt?: boolean;
salt: number;
}) => {
const { build, epochLength, log, projectName, prt } = options;

// keep track of last deployment
let lastDeployment: RollupsDeployment | undefined;
let salt = 0;

// deploy for the first time
const hash = getMachineHash();
if (hash) {
lastDeployment = await deploy({
epochLength,
hash,
projectName,
prt,
salt: numberToHex(salt++, { size: 32 }),
});
} else {
console.warn(
chalk.yellow("machine snapshot not found, waiting for build"),
);
}
let lastDeployment = options.deployment;
let salt = options.salt;

while (true) {
try {
Expand Down Expand Up @@ -346,6 +331,27 @@ export const createRunCommand = () => {
services,
});

// deploy the application
let deployment: RollupsDeployment | undefined;
let salt = 0;
const prt = !authority;
const hash = getMachineHash();
if (hash) {
deployment = await deploy({
epochLength,
hash,
projectName,
prt,
salt: numberToHex(salt++, { size: 32 }),
});
} else {
console.warn(
chalk.yellow(
"machine snapshot not found, waiting for build",
),
);
}

const shutdown = async () => {
progress.start(`${chalk.cyan(projectName)} stopping...`);
try {
Expand All @@ -359,23 +365,40 @@ export const createRunCommand = () => {
process.exit(0);
};

// inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
process.on("SIGINT", () => {});
process.on("SIGTERM", () => {});
if (process.stdin.isTTY) {
// inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
process.on("SIGINT", () => {});
process.on("SIGTERM", () => {});

const log = program.parent?.commands.find(
(c) => c.name() === "logs",
);
const build = program.parent?.commands.find(
(c) => c.name() === "build",
);
await shell({
build,
epochLength,
log,
projectName,
prt: !authority,
});
await shutdown();
const log = program.parent?.commands.find(
(c) => c.name() === "logs",
);
const build = program.parent?.commands.find(
(c) => c.name() === "build",
);
await shell({
build,
deployment,
epochLength,
log,
projectName,
prt,
salt,
});
await shutdown();
} else {
// non-interactive mode: wait for SIGINT or SIGTERM to shutdown
await new Promise<void>((resolve) => {
// keep the event loop alive
const keepAlive = setInterval(() => {}, 1 << 30);
const handler = () => {
clearInterval(keepAlive);
resolve();
};
process.on("SIGINT", handler);
process.on("SIGTERM", handler);
});
await shutdown();
}
});
};