Skip to content

Commit

Permalink
src: stop running process as nohup to avoid missing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityamaru committed Dec 19, 2024
1 parent e9a7d2c commit 4035032
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

37 changes: 33 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ actionsToolkit.run(
core.info('Shutdown buildkitd');
}
} catch (error) {
// No buildkitd process found, nothing to shutdown
core.debug('No buildkitd process found running');
if (error.code === 1) {
// pgrep returns non-zero if no processes found, which is fine
core.debug('No buildkitd process found running');
} else {
core.warning(`Error checking for buildkitd processes: ${error.message}`);
}
}
try {
const {stdout: mountOutput} = await execAsync(`mount | grep ${mountPoint}`);
Expand Down Expand Up @@ -398,8 +402,12 @@ actionsToolkit.run(
core.info('Shutdown buildkitd');
}
} catch (error) {
// pgrep returns non-zero if no processes found, which is fine
core.debug('No lingering buildkitd processes found');
if (error.code === 1) {
// pgrep returns non-zero if no processes found, which is fine
core.debug('No lingering buildkitd processes found');
} else {
core.warning(`Error checking for buildkitd processes: ${error.message}`);
}
}

try {
Expand Down Expand Up @@ -472,8 +480,29 @@ function buildSummaryEnabled(): boolean {
}

export async function shutdownBuildkitd(): Promise<void> {
const startTime = Date.now();
const timeout = 10000; // 10 seconds
const backoff = 300; // 300ms

try {
await execAsync(`sudo pkill -TERM buildkitd`);

// Wait for buildkitd to shutdown with backoff retry
while (Date.now() - startTime < timeout) {
try {
const {stderr} = await execAsync('pgrep -f buildkitd');
if (stderr) {
// Process not found, shutdown successful
return;
}
} catch (error) {
// pgrep returns non-zero if process not found, which means shutdown successful
return;
}
await new Promise(resolve => setTimeout(resolve, backoff));
}

throw new Error('Timed out waiting for buildkitd to shutdown after 10 seconds');
} catch (error) {
core.error('error shutting down buildkitd process:', error);
throw error;
Expand Down
50 changes: 38 additions & 12 deletions src/setup_builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as core from '@actions/core';
import {exec, execSync} from 'child_process';
import {exec, execSync, spawn} from 'child_process';
import {promisify} from 'util';
import * as TOML from '@iarna/toml';
import * as reporter from './reporter';
Expand Down Expand Up @@ -110,20 +110,46 @@ async function startBuildkitd(parallelism: number, device: string): Promise<stri
await execAsync('sudo mkdir -p /run/buildkit');
await execAsync('sudo chmod 755 /run/buildkit');
const addr = 'unix:///run/buildkit/buildkitd.sock';
const {stdout: startStdout, stderr: startStderr} = await execAsync(
`sudo nohup buildkitd --debug --addr ${addr} --allow-insecure-entitlement security.insecure --config=buildkitd.toml --allow-insecure-entitlement network.host > buildkitd.log 2>&1 &`
);

if (startStderr) {
throw new Error(`error starting buildkitd service: ${startStderr}`);
}
core.debug(`buildkitd daemon started successfully ${startStdout}`);
const logStream = fs.createWriteStream('buildkitd.log');
const buildkitd = spawn('sudo', [
'buildkitd',
'--debug',
'--addr', addr,
'--allow-insecure-entitlement', 'security.insecure',
'--config=buildkitd.toml',
'--allow-insecure-entitlement', 'network.host'
], {
stdio: ['ignore', 'pipe', 'pipe']
});

// Pipe stdout and stderr to log file
buildkitd.stdout.pipe(logStream);
buildkitd.stderr.pipe(logStream);

buildkitd.on('error', (error) => {
throw new Error(`Failed to start buildkitd: ${error.message}`);
});

// Wait for buildkitd PID to appear with backoff retry
const startTime = Date.now();
const timeout = 10000; // 10 seconds
const backoff = 300; // 300ms

const {stderr} = await execAsync(`pgrep -f buildkitd`);
if (stderr) {
throw new Error(`error finding buildkitd PID: ${stderr}`);
while (Date.now() - startTime < timeout) {
try {
const {stdout} = await execAsync('pgrep -f buildkitd');
if (stdout.trim()) {
core.debug('buildkitd daemon started successfully');
return addr;
}
} catch (error) {
// pgrep returns non-zero if process not found, which is expected while waiting
await new Promise(resolve => setTimeout(resolve, backoff));
}
}
return addr;

throw new Error('Timed out waiting for buildkitd to start after 10 seconds');
} catch (error) {
core.error('failed to start buildkitd daemon:', error);
throw error;
Expand Down

0 comments on commit 4035032

Please sign in to comment.