Skip to content

Commit

Permalink
fix: import fixes from monorepo (#812)
Browse files Browse the repository at this point in the history
- [make Deno capture unhandled exceptions and rejections and report them to the server](RocketChat/Rocket.Chat#33997)
- [apps-engine timeout config](RocketChat/Rocket.Chat#33690)
  • Loading branch information
d-gubert authored Dec 5, 2024
2 parents ea47d3f + 556200c commit 3eef395
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 6 deletions.
33 changes: 33 additions & 0 deletions deno-runtime/error-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as Messenger from './lib/messenger.ts';

export function unhandledRejectionListener(event: PromiseRejectionEvent) {
event.preventDefault();

const { type, reason } = event;

Messenger.sendNotification({
method: 'unhandledRejection',
params: [
{
type,
reason: reason instanceof Error ? reason.message : reason,
timestamp: new Date(),
},
],
});
}

export function unhandledExceptionListener(event: ErrorEvent) {
event.preventDefault();

const { type, message, filename, lineno, colno } = event;
Messenger.sendNotification({
method: 'uncaughtException',
params: [{ type, message, filename, lineno, colno }],
});
}

export default function registerErrorListeners() {
addEventListener('unhandledrejection', unhandledRejectionListener);
addEventListener('error', unhandledExceptionListener);
}
3 changes: 3 additions & 0 deletions deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import videoConferenceHandler from './handlers/videoconference-handler.ts';
import apiHandler from './handlers/api-handler.ts';
import handleApp from './handlers/app/handler.ts';
import handleScheduler from './handlers/scheduler-handler.ts';
import registerErrorListeners from './error-handlers.ts';

type Handlers = {
app: typeof handleApp;
Expand Down Expand Up @@ -126,4 +127,6 @@ async function main() {
}
}

registerErrorListeners();

main();
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rocket.chat/apps-engine",
"version": "1.43.2",
"version": "1.43.3-rc.0",
"description": "The engine code for the Rocket.Chat Apps which manages, runs, translates, coordinates and all of that.",
"main": "index",
"typings": "index",
Expand Down
2 changes: 2 additions & 0 deletions src/definition/metadata/AppMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ export enum AppMethod {
EXECUTE_POST_USER_STATUS_CHANGED = 'executePostUserStatusChanged',
// Runtime specific methods
RUNTIME_RESTART = 'runtime:restart',
RUNTIME_UNCAUGHT_EXCEPTION = 'runtime:uncaughtException',
RUNTIME_UNHANDLED_REJECTION = 'runtime:unhandledRejection',
}
6 changes: 3 additions & 3 deletions src/server/ProxiedApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export class ProxiedApp {
let options;

// Pre events need to be fast as they block the user
if (method.startsWith('checkPre') || method.startsWith('executePre')) {
options = { timeout: 1000 };
}
// if (method.startsWith('checkPre') || method.startsWith('executePre')) {
// options = { timeout: 1000 };
// }

try {
return await this.appRuntime.sendRequest({ method: `app:${method}`, params: args }, options);
Expand Down
32 changes: 31 additions & 1 deletion src/server/runtime/deno/AppsEngineDenoRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { IParseAppPackageResult } from '../../compiler';
import { AppConsole, type ILoggerStorageEntry } from '../../logging';
import type { AppAccessorManager, AppApiManager } from '../../managers';
import { AppStatus } from '../../../definition/AppStatus';
import type { AppMethod } from '../../../definition/metadata';
import { bundleLegacyApp } from './bundler';
import { ProcessMessenger } from './ProcessMessenger';
import { LivenessManager } from './LivenessManager';
Expand Down Expand Up @@ -52,6 +53,18 @@ const COMMAND_PONG = '_zPONG';

export const JSONRPC_METHOD_NOT_FOUND = -32601;

export function getRuntimeTimeout() {
const defaultTimeout = 30000;
const envValue = isFinite(process.env.APPS_ENGINE_RUNTIME_TIMEOUT as any) ? Number(process.env.APPS_ENGINE_RUNTIME_TIMEOUT) : defaultTimeout;

if (envValue < 0) {
console.log('Environment variable APPS_ENGINE_RUNTIME_TIMEOUT has a negative value, ignoring...');
return defaultTimeout;
}

return envValue;
}

export function isValidOrigin(accessor: string): accessor is typeof ALLOWED_ACCESSOR_METHODS[number] {
return ALLOWED_ACCESSOR_METHODS.includes(accessor as any);
}
Expand Down Expand Up @@ -87,7 +100,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
private readonly debug: debug.Debugger;

private readonly options = {
timeout: 10000,
timeout: getRuntimeTimeout(),
};

private readonly accessors: AppAccessorManager;
Expand Down Expand Up @@ -535,12 +548,29 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
case 'log':
console.log('SUBPROCESS LOG', message);
break;
case 'unhandledRejection':
case 'uncaughtException':
await this.logUnhandledError(`runtime:${method}`, message);
break;

default:
console.warn('Unrecognized method from sub process');
break;
}
}

private async logUnhandledError(
method: `${AppMethod.RUNTIME_UNCAUGHT_EXCEPTION | AppMethod.RUNTIME_UNHANDLED_REJECTION}`,
message: jsonrpc.IParsedObjectRequest | jsonrpc.IParsedObjectNotification,
) {
this.debug('Unhandled error of type "%s" caught in subprocess', method);

const logger = new AppConsole(method);
logger.error(message.payload);

await this.logStorage.storeEntries(AppConsole.toStorageEntry(this.getAppId(), logger));
}

private async handleResultMessage(message: jsonrpc.IParsedObjectError | jsonrpc.IParsedObjectSuccess): Promise<void> {
const { id } = message.payload;

Expand Down

0 comments on commit 3eef395

Please sign in to comment.