-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple workers in a single Miniflare instance (#7251)
- Loading branch information
Showing
33 changed files
with
1,131 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
Improve Wrangler's multiworker support to allow running multiple workers at once with one command. To try it out, pass multiple `-c` flags to Wrangler: i.e. `wrangler dev -c wrangler.toml -c ../other-worker/wrangler.toml`. The first config will be treated as the _primary_ worker and will be exposed over HTTP as usual (localhost:8787) while the rest will be treated as _secondary_ and will only be accessible via a service binding from the primary worker. Notably, these workers all run in the same runtime instance, which should improve reliability of multiworker dev and fix some bugs (RPC to cross worker Durable Objects, for instance). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
async fetch(req, env) { | ||
return new Response( | ||
"hello from a & " + (await env.WORKER.fetch(req).then((r) => r.text())) | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
async fetch() { | ||
return new Response("hello from b"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name = "a" | ||
main = "src/worker-a.mjs" | ||
compatibility_date = "2023-12-01" | ||
|
||
[[services]] | ||
binding = "WORKER" | ||
service = "b" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name = "b" | ||
main = "src/worker-b.mjs" | ||
compatibility_date = "2023-12-01" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import childProcess from "node:child_process"; | ||
|
||
interface Process { | ||
pid: string; | ||
cmd: string; | ||
} | ||
|
||
function getProcesses(): Process[] { | ||
return childProcess | ||
.execSync("ps -e | awk '{print $1,$4}'", { encoding: "utf8" }) | ||
.trim() | ||
.split("\n") | ||
.map((line) => { | ||
const [pid, cmd] = line.split(" "); | ||
return { pid, cmd }; | ||
}); | ||
} | ||
|
||
function getProcessCwd(pid: string | number) { | ||
return childProcess | ||
.execSync(`lsof -p ${pid} | awk '$4=="cwd" {print $9}'`, { | ||
encoding: "utf8", | ||
}) | ||
.trim(); | ||
} | ||
export function getStartedWorkerdProcesses(cwd: string): Process[] { | ||
return getProcesses() | ||
.filter(({ cmd }) => cmd.includes("workerd")) | ||
.filter((c) => getProcessCwd(c.pid).includes(cwd)); | ||
} |
Oops, something went wrong.