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

Run on Cloudflare Workers #69

Open
1 task
batonac opened this issue Nov 14, 2022 · 8 comments
Open
1 task

Run on Cloudflare Workers #69

batonac opened this issue Nov 14, 2022 · 8 comments
Assignees
Labels
Blocked [Type] Enhancement New feature or request

Comments

@batonac
Copy link

batonac commented Nov 14, 2022

This might be far out there, but here goes...

Since WordPress is working with WebAssembly and SQLite based on this project, how about expanding the scope from testing to production with Cloudflare Workers + CloudFlare D1 (SQLite database)?

Related issues

@adamziel
Copy link
Collaborator

+1, I'd love this to happen!

@artemiomorales artemiomorales added the [Type] Enhancement New feature or request label Feb 24, 2023
@adamziel
Copy link
Collaborator

adamziel commented Apr 14, 2023

I got PHP.wasm working in the local worker dev env provided by CloudFlare, I shared the setup below.

The bad news: I couldn't deploy it because of the 5MB size limitation – php_8_0.wasm is around 5.8MB. Maybe there's a way to get under 5MB 🤔 I'm rebuilding PHP with no extensions and more aggressive optimizations to find out.

Edit: I got it down to 4.4MB!

src/index.js:

import { PHP } from './web/index-213897b2.js';
import wasm from './web/php_7_3.wasm'; 
const phpPromise = PHP.load('7.3', {
	emscriptenOptions: {
		instantiateWasm(info, receive) {
			phpError = JSON.stringify(Object.keys(info.a));
			let instance;
			try {
				instance = new WebAssembly.Instance(wasm, info)
			} catch (e) {
				phpError = e + 'thrown!';
				throw e;
			}
			phpError = instance+'';
			receive(instance)
			return instance.exports
		},
	}
}).then(loaded => {
	phpError = 'loaded in theory';
	php = loaded;
}).catch(e => {
	phpError = e;
});

let php;
let phpError;

export default {
	async fetch() {
		if (php) {
			const r = await php.run({
				code: `<?php echo "Hello World from PHP!"; ?>`
			})
			return new Response(
				r.text
			);
		} else {
			return new Response(
				phpError+'',
				{
					status: 500
				}
			);
		}
	}
}

wrangler.toml:

name = "playground-worker"
main = "src/index.js"
node_compat = true
compatibility_date = "2020-04-04"

src/web/php_7_3.js:

export const dependenciesTotalSize = 4428017; 
-import dependencyFilename from './php_7_3.wasm'; 
+const dependencyFilename = './php_7_3.wasm'; 

@adamziel
Copy link
Collaborator

adamziel commented Apr 14, 2023

Running WordPress on CloudFlare is not viable yet. Loading PHP alone exceeds CloudFlare 128MB memory limit by a tiny margin, and that doesn't even include loading WordPress files.

This issue is blocked until either PHP.wasm can run with much less memory or CloudFlare doubles the memory limit.

Longer version below:


I managed to deploy PHP into CloudFlare and now am running into this error when the worker was starting:

Error: Script startup exceeded memory limits. [code: 10021]

Then I've built PHP with a different set of options and the worker started, but the first request resulted in this error:

RangeError: WebAssembly.Instance(): Out of memory: Cannot allocate Wasm memory for new instance

CloudFlare platform limits for paid users are, as of today:

  • 5MB for worker files
  • 128MB memory

Measuring the memory required by the node.js build tells me it requires roughly 132MB – very narrow margin:

/usr/bin/time -l npx @php-wasm/cli -r 'echo "Hello, world";'
139237696 peak memory footprint

I've tried building PHP with the following options:

    -s INITIAL_MEMORY=128MB \
    -s ALLOW_MEMORY_GROWTH=0         \

But that didn't help.

@adamziel
Copy link
Collaborator

adamziel commented Apr 15, 2023

Here's a bit more info:

; /usr/bin/time -l wrangler dev  --experimental-local
(...)
            15798208  peak memory footprint

According to time, the web build running in a worker seems to peak around 15MB, far from the 128MB required for the node.js-based CLI.

However, maybe it's somehow off by one digit. Mac process monitor says 154.8 MB of real memory consumption.

@jdevalk
Copy link
Contributor

jdevalk commented Aug 3, 2023

Seems the max size for a worker is now 10MB, so that bit’s solved. Memory limit still seems to be 128MB though.

eruizdechavez pushed a commit to eruizdechavez/wordpress-playground that referenced this issue Sep 20, 2023
## What?

Copies CI tooling improvements from WordPress Playground, specifically:
* WordPress#494
* WordPress#496

## Why?

* We should keep CI as consistent as possible between the two
repositories.
* It's nice to have linting and testing split into separate jobs, so
it's easier to see what fails.
* We'll benefit from a slight performance boost.

## Testing Instructions

1. Verify that all tests are run and pass as expected.
@adamziel adamziel changed the title Support for CloudFlare Workers and D1 CloudFlare Workers and D1 Nov 22, 2023
@adamziel adamziel changed the title CloudFlare Workers and D1 Run on CloudFlare Workers Nov 22, 2023
@adamziel adamziel mentioned this issue Nov 25, 2023
@bdurette
Copy link

lowercase-f-dangit 😉

@adamziel adamziel changed the title Run on CloudFlare Workers Run on Cloudflare Workers Jan 11, 2024
@brandonpayton brandonpayton self-assigned this Mar 28, 2024
@fernandodilland
Copy link

@adamziel
Copy link
Collaborator

This project uses babel-preset-php to convert PHP to JavaScript.

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked [Type] Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants