Skip to content

Commit

Permalink
WP-NOW: Use rotatePHPRuntime to avoid memory error (#152)
Browse files Browse the repository at this point in the history
* use the new function rotatePHPRuntime to mitigate the memory error.
  • Loading branch information
sejas authored Mar 21, 2024
1 parent a7292c9 commit 6f7c5db
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/wp-now/src/wp-now.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import { NodePHP, PHPLoaderOptions } from '@php-wasm/node';
import { NodePHP, PHPRequestHandlerConfiguration } from '@php-wasm/node';
import { rotatePHPRuntime } from '@php-wasm/universal';
import path from 'path';
import { SQLITE_FILENAME } from './constants';
import {
Expand Down Expand Up @@ -42,20 +43,23 @@ export default async function startWPNow(
options: Partial<WPNowOptions> = {}
): Promise<{ php: NodePHP; phpInstances: NodePHP[]; options: WPNowOptions }> {
const { documentRoot } = options;
const nodePHPOptions: PHPLoaderOptions = {
requestHandler: {
documentRoot,
absoluteUrl: options.absoluteUrl,
},
const nodePHPOptions: PHPRequestHandlerConfiguration = {
documentRoot,
absoluteUrl: options.absoluteUrl,
};

const phpInstances = [];
const loadRuntime = async () =>
await NodePHP.loadRuntime(options.phpVersion);
for (let i = 0; i < Math.max(options.numberOfPhpInstances, 1); i++) {
phpInstances.push(
await NodePHP.load(options.phpVersion, nodePHPOptions)
);
phpInstances.push(new NodePHP(await loadRuntime(), nodePHPOptions));
}
const php = phpInstances[0];
await rotatePHPRuntime({
php,
recreateRuntime: loadRuntime,
maxRequests: 100,
});

phpInstances.forEach((_php) => {
_php.mkdirTree(documentRoot);
Expand Down

0 comments on commit 6f7c5db

Please sign in to comment.