magic-nix-cache no longer forks into the background

This commit is contained in:
Eelco Dolstra 2023-12-14 15:58:27 +01:00
parent 8a8688f336
commit 4a3e8b7ce3
3 changed files with 39 additions and 16 deletions

BIN
bun.lockb

Binary file not shown.

26
dist/index.js generated vendored
View file

@ -12181,10 +12181,11 @@ async function setUpAutoCache() {
else { else {
runEnv = process.env; runEnv = process.env;
} }
const outputPath = `${daemonDir}/parent.log`; // Start the server.
const outputPath = `${daemonDir}/daemon.log`;
const output = openSync(outputPath, 'a'); const output = openSync(outputPath, 'a');
const launch = spawn(daemonBin, [ const daemon = spawn(daemonBin, [
'--daemon-dir', daemonDir, '--notify-fd', '3',
'--listen', coreExports.getInput('listen'), '--listen', coreExports.getInput('listen'),
'--upstream', coreExports.getInput('upstream-cache'), '--upstream', coreExports.getInput('upstream-cache'),
'--diagnostic-endpoint', coreExports.getInput('diagnostic-endpoint'), '--diagnostic-endpoint', coreExports.getInput('diagnostic-endpoint'),
@ -12197,13 +12198,20 @@ async function setUpAutoCache() {
] : []).concat(coreExports.getInput('use-gha-cache') === 'true' ? [ ] : []).concat(coreExports.getInput('use-gha-cache') === 'true' ? [
'--use-gha-cache' '--use-gha-cache'
] : []), { ] : []), {
stdio: ['ignore', output, output], stdio: ['ignore', output, output, 'pipe'],
env: runEnv env: runEnv,
detached: true
}); });
const pidFile = path$1.join(daemonDir, 'daemon.pid');
await fs$2.writeFile(pidFile, `${daemon.pid}`);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
launch.on('exit', async (code, signal) => { daemon.stdio[3].on('data', (data) => {
if (data.toString().trim() == 'INIT') {
resolve();
}
});
daemon.on('exit', async (code, signal) => {
const log = await fs$2.readFile(outputPath, 'utf-8'); const log = await fs$2.readFile(outputPath, 'utf-8');
console.log(log);
if (signal) { if (signal) {
reject(new Error(`Daemon was killed by signal ${signal}: ${log}`)); reject(new Error(`Daemon was killed by signal ${signal}: ${log}`));
} }
@ -12211,10 +12219,11 @@ async function setUpAutoCache() {
reject(new Error(`Daemon exited with code ${code}: ${log}`)); reject(new Error(`Daemon exited with code ${code}: ${log}`));
} }
else { else {
resolve(); reject(new Error(`Daemon unexpectedly exited: ${log}`));
} }
}); });
}); });
daemon.unref();
coreExports.info('Launched Magic Nix Cache'); coreExports.info('Launched Magic Nix Cache');
coreExports.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir); coreExports.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
} }
@ -12224,6 +12233,7 @@ async function notifyAutoCache() {
return; return;
} }
try { try {
// FIXME: remove this
coreExports.debug(`Indicating workflow start`); coreExports.debug(`Indicating workflow start`);
const res = await gotClient.post(`http://${coreExports.getInput('listen')}/api/workflow-start`).json(); const res = await gotClient.post(`http://${coreExports.getInput('listen')}/api/workflow-start`).json();
coreExports.debug(`back from post`); coreExports.debug(`back from post`);

View file

@ -113,12 +113,13 @@ async function setUpAutoCache() {
runEnv = process.env; runEnv = process.env;
} }
const outputPath = `${daemonDir}/parent.log`; // Start the server.
const outputPath = `${daemonDir}/daemon.log`;
const output = openSync(outputPath, 'a'); const output = openSync(outputPath, 'a');
const launch = spawn( const daemon = spawn(
daemonBin, daemonBin,
[ [
'--daemon-dir', daemonDir, '--notify-fd', '3',
'--listen', core.getInput('listen'), '--listen', core.getInput('listen'),
'--upstream', core.getInput('upstream-cache'), '--upstream', core.getInput('upstream-cache'),
'--diagnostic-endpoint', core.getInput('diagnostic-endpoint'), '--diagnostic-endpoint', core.getInput('diagnostic-endpoint'),
@ -134,25 +135,36 @@ async function setUpAutoCache() {
'--use-gha-cache' '--use-gha-cache'
] : []), ] : []),
{ {
stdio: ['ignore', output, output], stdio: ['ignore', output, output, 'pipe'],
env: runEnv env: runEnv,
detached: true
} }
); );
const pidFile = path.join(daemonDir, 'daemon.pid');
await fs.writeFile(pidFile, `${daemon.pid}`);
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
launch.on('exit', async (code, signal) => { daemon.stdio[3].on('data', (data) => {
if (data.toString().trim() == 'INIT') {
resolve();
}
});
daemon.on('exit', async (code, signal) => {
const log: string = await fs.readFile(outputPath, 'utf-8'); const log: string = await fs.readFile(outputPath, 'utf-8');
console.log(log);
if (signal) { if (signal) {
reject(new Error(`Daemon was killed by signal ${signal}: ${log}`)); reject(new Error(`Daemon was killed by signal ${signal}: ${log}`));
} else if (code) { } else if (code) {
reject(new Error(`Daemon exited with code ${code}: ${log}`)); reject(new Error(`Daemon exited with code ${code}: ${log}`));
} else { } else {
resolve(); reject(new Error(`Daemon unexpectedly exited: ${log}`));
} }
}); });
}); });
daemon.unref();
core.info('Launched Magic Nix Cache'); core.info('Launched Magic Nix Cache');
core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir); core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir);
} }
@ -165,6 +177,7 @@ async function notifyAutoCache() {
} }
try { try {
// FIXME: remove this
core.debug(`Indicating workflow start`); core.debug(`Indicating workflow start`);
const res: any = await gotClient.post(`http://${core.getInput('listen')}/api/workflow-start`).json(); const res: any = await gotClient.post(`http://${core.getInput('listen')}/api/workflow-start`).json();
core.debug(`back from post`); core.debug(`back from post`);