diff --git a/action.yml b/action.yml index dc1e7a3..49bf257 100644 --- a/action.yml +++ b/action.yml @@ -23,7 +23,7 @@ inputs: source-pr: description: The PR of `magic-nix-cache` to use. Conflicts with all other `source-*` options. required: false - default: 1 + default: 3 source-revision: description: The revision of `nix-magic-nix-cache` to use. Conflicts with all other `source-*` options. required: false diff --git a/bun.lockb b/bun.lockb index 5f50faf..f3e6291 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/dist/index.js b/dist/index.js index 4256df1..9fe91b1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -12181,10 +12181,12 @@ async function setUpAutoCache() { else { runEnv = process.env; } - const outputPath = `${daemonDir}/parent.log`; + // Start the server. Once it is ready, it will notify us via file descriptor 3. + const outputPath = `${daemonDir}/daemon.log`; const output = openSync(outputPath, 'a'); - const launch = spawn(daemonBin, [ - '--daemon-dir', daemonDir, + const notifyFd = 3; + const daemon = spawn(daemonBin, [ + '--notify-fd', String(notifyFd), '--listen', coreExports.getInput('listen'), '--upstream', coreExports.getInput('upstream-cache'), '--diagnostic-endpoint', coreExports.getInput('diagnostic-endpoint'), @@ -12197,13 +12199,20 @@ async function setUpAutoCache() { ] : []).concat(coreExports.getInput('use-gha-cache') === 'true' ? [ '--use-gha-cache' ] : []), { - stdio: ['ignore', output, output], - env: runEnv + stdio: ['ignore', output, output, 'pipe'], + env: runEnv, + detached: true }); + const pidFile = path$1.join(daemonDir, 'daemon.pid'); + await fs$2.writeFile(pidFile, `${daemon.pid}`); await new Promise((resolve, reject) => { - launch.on('exit', async (code, signal) => { + daemon.stdio[notifyFd].on('data', (data) => { + if (data.toString().trim() == 'INIT') { + resolve(); + } + }); + daemon.on('exit', async (code, signal) => { const log = await fs$2.readFile(outputPath, 'utf-8'); - console.log(log); if (signal) { reject(new Error(`Daemon was killed by signal ${signal}: ${log}`)); } @@ -12211,10 +12220,11 @@ async function setUpAutoCache() { reject(new Error(`Daemon exited with code ${code}: ${log}`)); } else { - resolve(); + reject(new Error(`Daemon unexpectedly exited: ${log}`)); } }); }); + daemon.unref(); coreExports.info('Launched Magic Nix Cache'); coreExports.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir); } diff --git a/src/index.ts b/src/index.ts index 406f927..79ade61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,12 +113,14 @@ async function setUpAutoCache() { runEnv = process.env; } - const outputPath = `${daemonDir}/parent.log`; + // Start the server. Once it is ready, it will notify us via file descriptor 3. + const outputPath = `${daemonDir}/daemon.log`; const output = openSync(outputPath, 'a'); - const launch = spawn( + const notifyFd = 3; + const daemon = spawn( daemonBin, [ - '--daemon-dir', daemonDir, + '--notify-fd', String(notifyFd), '--listen', core.getInput('listen'), '--upstream', core.getInput('upstream-cache'), '--diagnostic-endpoint', core.getInput('diagnostic-endpoint'), @@ -134,25 +136,36 @@ async function setUpAutoCache() { '--use-gha-cache' ] : []), { - stdio: ['ignore', output, output], - env: runEnv + stdio: ['ignore', output, output, 'pipe'], + env: runEnv, + detached: true } ); + const pidFile = path.join(daemonDir, 'daemon.pid'); + await fs.writeFile(pidFile, `${daemon.pid}`); + await new Promise((resolve, reject) => { - launch.on('exit', async (code, signal) => { + daemon.stdio[notifyFd].on('data', (data) => { + if (data.toString().trim() == 'INIT') { + resolve(); + } + }); + + daemon.on('exit', async (code, signal) => { const log: string = await fs.readFile(outputPath, 'utf-8'); - console.log(log); if (signal) { reject(new Error(`Daemon was killed by signal ${signal}: ${log}`)); } else if (code) { reject(new Error(`Daemon exited with code ${code}: ${log}`)); } else { - resolve(); + reject(new Error(`Daemon unexpectedly exited: ${log}`)); } }); }); + daemon.unref(); + core.info('Launched Magic Nix Cache'); core.exportVariable(ENV_CACHE_DAEMONDIR, daemonDir); }