Skip to content

Commit

Permalink
Handle immediate crashes more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
trappar committed Feb 15, 2024
1 parent 49f741d commit 15c50fc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 36 deletions.
31 changes: 14 additions & 17 deletions dist/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2783,6 +2783,14 @@ var __webpack_exports__ = {};

// EXTERNAL MODULE: ./node_modules/.pnpm/@actions+core@1.10.1/node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(93);
;// CONCATENATED MODULE: ./src/indentMultiline.js
function indentMultiline(message, spaces = 2) {
const output = [];
message.split('\n').forEach((line) => {
output.push(' '.repeat(spaces) + line);
});
return output.join('\n');
}
;// CONCATENATED MODULE: external "node:fs"
const external_node_fs_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs");
;// CONCATENATED MODULE: external "node:fs/promises"
Expand Down Expand Up @@ -2816,6 +2824,7 @@ const readLog = async (name) => {




function pidIsRunning(pid) {
try {
process.kill(pid, 0);
Expand All @@ -2825,30 +2834,18 @@ function pidIsRunning(pid) {
}
}

function indentMultiline(message, spaces = 2) {
const output = [];
message.split('\n').forEach((line) => {
output.push(' '.repeat(spaces) + line);
});
return output.join('\n');
}

async function post() {
const pid = parseInt((0,core.getState)('pid'));

if (isNaN(pid)) return;

if (pidIsRunning(pid)) {
(0,core.info)(`Stopping Turbo Cache Server with PID ${pid}`);
process.kill(pid);
} else {
if (isNaN(pid)) {
(0,core.setFailed)(
'Turbo Cache Server was not running. This probably indicates that the server was unable to start.',
);
} else {
(0,core.setFailed)(
`Turbo Cache Server with PID ${pid} was not running. This may indicate a configuration or server crash.`,
);
}
(0,core.setFailed)(
`Turbo Cache Server with PID ${pid} was not running. This may indicate a configuration or server crash.`,
);
}

const [out, err] = await Promise.all([readLog('out'), readLog('err')]);
Expand Down
41 changes: 40 additions & 1 deletion dist/start/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6044,6 +6044,14 @@ const external_node_path_namespaceObject = __WEBPACK_EXTERNAL_createRequire(impo
var tcp_port_used = __nccwpck_require__(8351);
;// CONCATENATED MODULE: external "url"
const external_url_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("url");
;// CONCATENATED MODULE: ./src/indentMultiline.js
function indentMultiline(message, spaces = 2) {
const output = [];
message.split('\n').forEach((line) => {
output.push(' '.repeat(spaces) + line);
});
return output.join('\n');
}
// EXTERNAL MODULE: external "crypto"
var external_crypto_ = __nccwpck_require__(6113);
;// CONCATENATED MODULE: ./src/inputs.js
Expand All @@ -6063,6 +6071,33 @@ const token = (0,external_crypto_.randomBytes)(24).toString('hex');
const host = (0,core.getInput)('host', { trimWhitespace: true });
const port = parseInt((0,core.getInput)('port', { trimWhitespace: true }));

;// CONCATENATED MODULE: external "node:fs"
const external_node_fs_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs");
;// CONCATENATED MODULE: external "node:fs/promises"
const promises_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:fs/promises");
// EXTERNAL MODULE: external "os"
var external_os_ = __nccwpck_require__(2037);
;// CONCATENATED MODULE: ./src/logs.js





const logDir = external_node_path_namespaceObject.resolve(external_os_.tmpdir(), 'turborepo-remote-cache-gh-action');

if (!external_node_fs_namespaceObject.existsSync(logDir)) {
external_node_fs_namespaceObject.mkdirSync(logDir, { recursive: true });
}

const logFile = (name) => external_node_path_namespaceObject.resolve(logDir, name);

const readLog = async (name) => {
try {
return await promises_namespaceObject.readFile(logFile(name), 'utf8');
} catch (e) {
return '';
}
};
;// CONCATENATED MODULE: ./src/start.js


Expand All @@ -6072,6 +6107,8 @@ const port = parseInt((0,core.getInput)('port', { trimWhitespace: true }));





const start_dirname = (0,external_node_path_namespaceObject.dirname)((0,external_url_namespaceObject.fileURLToPath)(import.meta.url));

async function getPort() {
Expand Down Expand Up @@ -6127,7 +6164,9 @@ async function main() {

process.exit(0);
} catch (e) {
throw new Error(`Turbo server failed to start on port: ${port}\n${e}`);
const errors = await readLog('err');
const errorMessage = errors ? `\nErrors:\n${indentMultiline(errors)}` : '';
throw new Error(`Turbo server failed to start on port: ${port}\n${errorMessage}`);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/indentMultiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function indentMultiline(message, spaces = 2) {
const output = [];
message.split('\n').forEach((line) => {
output.push(' '.repeat(spaces) + line);
});
return output.join('\n');
}
23 changes: 6 additions & 17 deletions src/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { debug, getState, info, setFailed } from '@actions/core';
import { indentMultiline } from './indentMultiline.js';
import { readLog } from './logs.js';

function pidIsRunning(pid) {
Expand All @@ -10,30 +11,18 @@ function pidIsRunning(pid) {
}
}

function indentMultiline(message, spaces = 2) {
const output = [];
message.split('\n').forEach((line) => {
output.push(' '.repeat(spaces) + line);
});
return output.join('\n');
}

async function post() {
const pid = parseInt(getState('pid'));

if (isNaN(pid)) return;

if (pidIsRunning(pid)) {
info(`Stopping Turbo Cache Server with PID ${pid}`);
process.kill(pid);
} else {
if (isNaN(pid)) {
setFailed(
'Turbo Cache Server was not running. This probably indicates that the server was unable to start.',
);
} else {
setFailed(
`Turbo Cache Server with PID ${pid} was not running. This may indicate a configuration or server crash.`,
);
}
setFailed(
`Turbo Cache Server with PID ${pid} was not running. This may indicate a configuration or server crash.`,
);
}

const [out, err] = await Promise.all([readLog('out'), readLog('err')]);
Expand Down
6 changes: 5 additions & 1 deletion src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { spawn } from 'node:child_process';
import { dirname, resolve } from 'node:path';
import { waitUntilUsedOnHost } from 'tcp-port-used';
import { fileURLToPath } from 'url';
import { indentMultiline } from './indentMultiline.js';
import { host, port, storagePath, storageProvider, teamId, token } from './inputs.js';
import { readLog } from './logs.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -67,7 +69,9 @@ async function main() {

process.exit(0);
} catch (e) {
throw new Error(`Turbo server failed to start on port: ${port}\n${e}`);
const errors = await readLog('err');
const errorMessage = errors ? `\nErrors:\n${indentMultiline(errors)}` : '';
throw new Error(`Turbo server failed to start on port: ${port}\n${errorMessage}`);
}
}

Expand Down

0 comments on commit 15c50fc

Please sign in to comment.