Skip to content

Commit

Permalink
feat(standalone-sqlite): better sqlite error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
karlprieb authored and djwhitt committed Nov 6, 2024
1 parent 595451d commit fc41626
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
9 changes: 1 addition & 8 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,4 @@ apolloServerInstanceGql.start().then(() => {
});
});

// Handle shutdown signals
process.on('SIGINT', async () => {
await system.shutdown(server);
});

process.on('SIGTERM', async () => {
await system.shutdown(server);
});
export { server };
34 changes: 28 additions & 6 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
TransactionAttributes,
} from '../types.js';
import * as config from '../config.js';
import { DetailedError } from '../lib/error.js';

const CPU_COUNT = os.cpus().length;
const MAX_WORKER_COUNT = 12;
Expand Down Expand Up @@ -2598,9 +2599,15 @@ export class StandaloneSqliteDatabase
self.workers[pool][role].push({ takeWork });
takeWork();
})
.on('message', (result) => {
if (result === '__ERROR__') {
job.reject(new Error('Worker error'));
.on('message', async (result) => {
if (result && result.stack) {
const { message, stack, workerMethod, workerArgs } = result;
const error = new DetailedError(message, {
stack,
workerMethod,
workerArgs,
});
job.reject(error);
} else {
job.resolve(result);
}
Expand Down Expand Up @@ -3204,14 +3211,29 @@ if (!isMainThread) {
parentPort?.postMessage(null);
process.exit(0);
}
} catch (error) {
} catch (e: any) {
if (errorCount > MAX_WORKER_ERRORS) {
log.error('Too many errors in StandaloneSqlite worker, exiting.');
process.exit(1);
}
log.error('Error in StandaloneSqlite worker:', error);

const error = new DetailedError('Error in StandaloneSqlite worker', {
stack: e.stack,
});

log.error(error.message, {
message: error.message,
stack: error.stack,
workerMethod: method,
workerArgs: args,
});
errorCount++;
parentPort?.postMessage('__ERROR__');
parentPort?.postMessage({
message: error.message,
stack: error.stack,
workerMethod: method,
workerArgs: args,
});
}
});
}
37 changes: 37 additions & 0 deletions src/lib/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* AR.IO Gateway
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

interface DetailedErrorOptions {
stack?: string;
[key: string]: any; // Allow any other properties with any value type
}

export class DetailedError extends Error {
constructor(message: string, options?: DetailedErrorOptions) {
super(message);
this.name = this.constructor.name;
Object.assign(this, options);
this.stack = options?.stack ?? new Error().stack;
}

toJSON() {
return {
...this,
};
}
}
17 changes: 13 additions & 4 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
import { default as Arweave } from 'arweave';
import EventEmitter from 'node:events';
import { Server } from 'node:http';
import fs from 'node:fs';
import { AOProcess, IO } from '@ar.io/sdk';
import awsLite from '@aws-lite/client';
Expand Down Expand Up @@ -83,6 +82,7 @@ import { SignatureFetcher } from './data/signature-fetcher.js';
import { SQLiteWalCleanupWorker } from './workers/sqlite-wal-cleanup-worker.js';
import { KvArnsStore } from './store/kv-arns-store.js';
import { parquetExporter } from './routes/ar-io.js';
import { server } from './app.js';

process.on('uncaughtException', (error) => {
metrics.uncaughtExceptionCounter.inc();
Expand Down Expand Up @@ -642,13 +642,13 @@ if (dataSqliteWalCleanupWorker !== undefined) {

let isShuttingDown = false;

export const shutdown = async (express: Server) => {
export const shutdown = async (exitCode = 0) => {
if (isShuttingDown) {
log.info('Shutdown already in progress');
} else {
isShuttingDown = true;
log.info('Shutting down...');
express.close(async () => {
server.close(async () => {
log.debug('Web server stopped successfully');
eventEmitter.removeAllListeners();
arIODataSource.stopUpdatingPeers();
Expand All @@ -672,7 +672,16 @@ export const shutdown = async (express: Server) => {
await headerFsCacheCleanupWorker?.stop();
await contiguousDataFsCacheCleanupWorker?.stop();

process.exit(0);
process.exit(exitCode);
});
}
};

// Handle shutdown signals
process.on('SIGINT', async () => {
await shutdown();
});

process.on('SIGTERM', async () => {
await shutdown();
});

0 comments on commit fc41626

Please sign in to comment.