Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP: Add a cwd argument to hotSwapPHPRuntime() #1304

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions packages/php-wasm/node/src/test/rotate-php-runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ describe('rotatePHPRuntime()', () => {

const recreateRuntimeSpy = vitest.fn(recreateRuntime);
// Rotate the PHP runtime
const php = new NodePHP(await recreateRuntime(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntime());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime: recreateRuntimeSpy,
maxRequests: 1000,
});
Expand All @@ -53,11 +52,10 @@ describe('rotatePHPRuntime()', () => {

it('Should recreate the PHP runtime after maxRequests', async () => {
const recreateRuntimeSpy = vitest.fn(recreateRuntime);
const php = new NodePHP(await recreateRuntimeSpy(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntimeSpy());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime: recreateRuntimeSpy,
maxRequests: 1,
});
Expand All @@ -68,11 +66,10 @@ describe('rotatePHPRuntime()', () => {

it('Should stop rotating after the cleanup handler is called', async () => {
const recreateRuntimeSpy = vitest.fn(recreateRuntime);
const php = new NodePHP(await recreateRuntimeSpy(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntimeSpy());
const cleanup = rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime: recreateRuntimeSpy,
maxRequests: 1,
});
Expand All @@ -98,11 +95,10 @@ describe('rotatePHPRuntime()', () => {
}
return recreateRuntime('8.3');
});
const php = new NodePHP(await recreateRuntimeSpy(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntimeSpy());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime: recreateRuntimeSpy,
maxRequests: 1,
});
Expand All @@ -121,11 +117,10 @@ describe('rotatePHPRuntime()', () => {
}, 30_000);

it('Should preserve the custom SAPI name', async () => {
const php = new NodePHP(await recreateRuntime(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntime());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime,
maxRequests: 1,
});
Expand All @@ -140,11 +135,10 @@ describe('rotatePHPRuntime()', () => {
});

it('Should preserve the MEMFS files', async () => {
const php = new NodePHP(await recreateRuntime(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntime());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime,
maxRequests: 1,
});
Expand All @@ -165,11 +159,10 @@ describe('rotatePHPRuntime()', () => {
}, 30_000);

it('Should not overwrite the NODEFS files', async () => {
const php = new NodePHP(await recreateRuntime(), {
documentRoot: '/test-root',
});
const php = new NodePHP(await recreateRuntime());
rotatePHPRuntime({
php,
cwd: '/test-root',
recreateRuntime,
maxRequests: 1,
});
Expand Down
11 changes: 7 additions & 4 deletions packages/php-wasm/universal/src/lib/base-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,12 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
* interrupting the operations of this PHP instance.
*
* @param runtime
* @param cwd. Internal, the VFS path to recreate in the new runtime.
* This arg is temporary and will be removed once BasePHP
* is fully decoupled from the request handler and
* accepts a constructor-level cwd argument.
*/
hotSwapPHPRuntime(runtime: number) {
hotSwapPHPRuntime(runtime: number, cwd?: string) {
// Once we secure the lock and have the new runtime ready,
// the rest of the swap handler is synchronous to make sure
// no other operations acts on the old runtime or FS.
Expand Down Expand Up @@ -858,9 +862,8 @@ export abstract class BasePHP implements IsomorphicLocalPHP {
}

// Copy the MEMFS directory structure from the old FS to the new one
if (this.requestHandler) {
const docroot = this.documentRoot;
copyFS(oldFS, this[__private__dont__use].FS, docroot);
if (cwd) {
copyFS(oldFS, this[__private__dont__use].FS, cwd);
}
}

Expand Down
14 changes: 12 additions & 2 deletions packages/php-wasm/universal/src/lib/rotate-php-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BasePHP } from './base-php';

export interface RotateOptions<T extends BasePHP> {
php: T;
cwd: string;
recreateRuntime: () => Promise<number> | number;
maxRequests: number;
}
Expand All @@ -24,8 +25,17 @@ export interface RotateOptions<T extends BasePHP> {
*/
export function rotatePHPRuntime<T extends BasePHP>({
php,
cwd,
recreateRuntime,
maxRequests,
/*
* 400 is an arbitrary number that should trigger a rotation
* way before the memory gets too fragmented. If it doesn't,
* let's explore:
* * Rotating based on an actual memory usage and
* fragmentation.
* * Resetting HEAP to its initial value.
*/
maxRequests = 400,
}: RotateOptions<T>) {
let handledCalls = 0;
async function rotateRuntime() {
Expand All @@ -36,7 +46,7 @@ export function rotatePHPRuntime<T extends BasePHP>({

const release = await php.semaphore.acquire();
try {
php.hotSwapPHPRuntime(await recreateRuntime());
php.hotSwapPHPRuntime(await recreateRuntime(), cwd);
} finally {
release();
}
Expand Down
1 change: 1 addition & 0 deletions packages/playground/remote/src/lib/worker-thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const recreateRuntime = async () =>
// @see https://github.com/WordPress/wordpress-playground/pull/990 for more context
rotatePHPRuntime({
php,
cwd: DOCROOT,
recreateRuntime,
// 400 is an arbitrary number that should trigger a rotation
// way before the memory gets too fragmented. If the memory
Expand Down
Loading