Skip to content

Commit

Permalink
Only rotate PHP runtime when there has been a runtime crash
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton committed Jul 23, 2024
1 parent 4a4ae02 commit 86d618b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
27 changes: 26 additions & 1 deletion packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('rotatePHPRuntime()', () => {
expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2);
}, 30_000);

it('Should recreate the PHP runtime after PHP crash', async () => {
it('Should recreate the PHP runtime after a PHP runtime crash', async () => {
const recreateRuntimeSpy = vitest.fn(recreateRuntime);
const php = new PHP(await recreateRuntimeSpy());
rotatePHPRuntime({
Expand All @@ -101,10 +101,34 @@ describe('rotatePHPRuntime()', () => {
await php.dispatchEvent({
type: 'request.error',
error: new Error('mock error'),
source: 'php-wasm',
});
expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2);
}, 30_000);

it('Should not recreate the PHP runtime after a PHP fatal', async () => {
const recreateRuntimeSpy = vitest.fn(recreateRuntime);
const php = new PHP(await recreateRuntimeSpy());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime: recreateRuntimeSpy,
maxRequests: 1234,
});
// Trigger error with no `source`
await php.dispatchEvent({
type: 'request.error',
error: new Error('mock error'),
});
// Trigger error with request `source`
await php.dispatchEvent({
type: 'request.error',
error: new Error('mock error'),
source: 'request',
});
expect(recreateRuntimeSpy).toHaveBeenCalledTimes(1);
}, 30_000);

it('Should not rotate after the cleanup handler is called, even if there is a PHP runtime error', async () => {
const recreateRuntimeSpy = vitest.fn(recreateRuntime);
const php = new PHP(await recreateRuntimeSpy());
Expand All @@ -124,6 +148,7 @@ describe('rotatePHPRuntime()', () => {
php.dispatchEvent({
type: 'request.error',
error: new Error('mock error'),
source: 'php-wasm',
});

expect(recreateRuntimeSpy).toHaveBeenCalledTimes(2);
Expand Down
11 changes: 9 additions & 2 deletions packages/php-wasm/universal/src/lib/rotate-php-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PHP } from './php';
import { PHPEvent } from './universal-php';

export interface RotateOptions {
php: PHP;
Expand Down Expand Up @@ -57,11 +58,17 @@ export function rotatePHPRuntime({
await rotateRuntime();
}

php.addEventListener('request.error', rotateRuntime);
async function rotateRuntimeForPhpWasmError(event: PHPEvent) {
if (event.type === 'request.error' && event.source === 'php-wasm') {
await rotateRuntime();
}
}

php.addEventListener('request.error', rotateRuntimeForPhpWasmError);
php.addEventListener('request.end', rotateRuntimeAfterMaxRequests);

return function () {
php.removeEventListener('request.error', rotateRuntime);
php.removeEventListener('request.error', rotateRuntimeForPhpWasmError);
php.removeEventListener('request.end', rotateRuntimeAfterMaxRequests);
};
}

0 comments on commit 86d618b

Please sign in to comment.