Skip to content

Commit de71ecf

Browse files
author
Nicolas Dorseuil
committed
introduce a 3rd worker that'll handle all DO
1 parent 832ff5b commit de71ecf

File tree

6 files changed

+252
-34
lines changed

6 files changed

+252
-34
lines changed

.github/composite/deploy-cloudflare/action.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ runs:
6464
GITBOOK_RUNTIME: cloudflare
6565
shell: bash
6666

67+
- name: Upload the DO worker
68+
uses: cloudflare/wrangler-action@v3.14.0
69+
with:
70+
apiToken: ${{ inputs.apiToken }}
71+
accountId: ${{ inputs.accountId }}
72+
workingDirectory: ./
73+
wranglerVersion: '4.10.0'
74+
environment: ${{ inputs.environment }}
75+
command: deploy --config ./packages/gitbook-v2/openNext/customWorkers/doWrangler.jsonc
76+
6777
- id: upload_server
6878
name: Upload server to Cloudflare
6979
uses: cloudflare/wrangler-action@v3.14.0

packages/gitbook-v2/openNext/customWorkers/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { runWithCloudflareRequestContext } from '../../.open-next/cloudflare/ini
22

33
import { DurableObject } from 'cloudflare:workers';
44

5+
// Only needed to run locally, in prod we'll use the one from do.js
56
export class R2WriteBuffer extends DurableObject {
67
writePromise;
78

packages/gitbook-v2/openNext/customWorkers/defaultWrangler.jsonc

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,26 @@
6262
"binding": "WORKER_SELF_REFERENCE",
6363
"service": "gitbook-open-v2-server-preview"
6464
}
65-
]
66-
// No durable objects on preview, as they block the generation of preview URLs
67-
// and we don't need tags invalidation on preview
65+
],
66+
"durable_objects": {
67+
"bindings": [
68+
{
69+
"name": "WRITE_BUFFER",
70+
"class_name": "R2WriteBuffer",
71+
"script_name": "gitbook-open-v2-do-preview"
72+
},
73+
{
74+
"name": "NEXT_TAG_CACHE_DO_SHARDED",
75+
"class_name": "DOShardedTagCache",
76+
"script_name": "gitbook-open-v2-do-preview"
77+
},
78+
{
79+
"name": "NEXT_CACHE_DO_QUEUE",
80+
"class_name": "DOQueueHandler",
81+
"script_name": "gitbook-open-v2-do-preview"
82+
}
83+
]
84+
}
6885
},
6986
"staging": {
7087
"r2_buckets": [
@@ -81,25 +98,23 @@
8198
],
8299
"durable_objects": {
83100
"bindings": [
84-
// We do not need to define migrations for external DOs,
85-
// In fact, defining migrations for external DOs will crash
101+
{
102+
"name": "WRITE_BUFFER",
103+
"class_name": "R2WriteBuffer",
104+
"script_name": "gitbook-open-v2-do-staging"
105+
},
86106
{
87107
"name": "NEXT_TAG_CACHE_DO_SHARDED",
88108
"class_name": "DOShardedTagCache",
89-
"script_name": "gitbook-open-v2-staging"
109+
"script_name": "gitbook-open-v2-do-staging"
90110
},
91111
{
92-
"name": "WRITE_BUFFER",
93-
"class_name": "R2WriteBuffer"
112+
"name": "NEXT_CACHE_DO_QUEUE",
113+
"class_name": "DOQueueHandler",
114+
"script_name": "gitbook-open-v2-do-staging"
94115
}
95116
]
96117
},
97-
"migrations": [
98-
{
99-
"tag": "v1",
100-
"new_sqlite_classes": ["R2WriteBuffer"]
101-
}
102-
],
103118
"tail_consumers": [
104119
{
105120
"service": "gitbook-x-staging-tail"
@@ -122,24 +137,22 @@
122137
"durable_objects": {
123138
"bindings": [
124139
{
125-
// We do not need to define migrations for external DOs,
126-
// In fact, defining migrations for external DOs will crash
140+
"name": "WRITE_BUFFER",
141+
"class_name": "R2WriteBuffer",
142+
"script_name": "gitbook-open-v2-do-production"
143+
},
144+
{
127145
"name": "NEXT_TAG_CACHE_DO_SHARDED",
128146
"class_name": "DOShardedTagCache",
129-
"script_name": "gitbook-open-v2-production"
147+
"script_name": "gitbook-open-v2-do-production"
130148
},
131149
{
132-
"name": "WRITE_BUFFER",
133-
"class_name": "R2WriteBuffer"
150+
"name": "NEXT_CACHE_DO_QUEUE",
151+
"class_name": "DOQueueHandler",
152+
"script_name": "gitbook-open-v2-do-production"
134153
}
135154
]
136155
},
137-
"migrations": [
138-
{
139-
"tag": "v1",
140-
"new_sqlite_classes": ["R2WriteBuffer"]
141-
}
142-
],
143156
"tail_consumers": [
144157
{
145158
"service": "gitbook-x-prod-tail"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This worker only purposes it to host the different DO that we will need in the other workers.
2+
import { DurableObject } from 'cloudflare:workers';
3+
4+
export class R2WriteBuffer extends DurableObject {
5+
writePromise;
6+
7+
async write(cacheKey, value) {
8+
// We are already writing to this key
9+
if (this.writePromise) {
10+
return;
11+
}
12+
13+
this.writePromise = this.env.NEXT_INC_CACHE_R2_BUCKET.put(cacheKey, value);
14+
this.ctx.waitUntil(
15+
this.writePromise.finally(() => {
16+
this.writePromise = undefined;
17+
})
18+
);
19+
}
20+
}
21+
22+
export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js';
23+
24+
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js';
25+
26+
export default {
27+
async fetch() {
28+
// This worker does not handle any requests, it only provides Durable Objects
29+
return new Response('This worker is not meant to handle requests directly', {
30+
status: 400,
31+
headers: {
32+
'Content-Type': 'text/plain',
33+
},
34+
});
35+
},
36+
};
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"main": "do.js",
3+
"name": "gitbook-open-v2-do",
4+
"compatibility_date": "2025-04-14",
5+
"compatibility_flags": [
6+
"nodejs_compat",
7+
"allow_importable_env",
8+
"global_fetch_strictly_public"
9+
],
10+
"observability": {
11+
"enabled": true
12+
},
13+
"env": {
14+
"preview": {
15+
"vars": {
16+
"STAGE": "preview",
17+
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true"
18+
},
19+
"r2_buckets": [
20+
{
21+
"binding": "NEXT_INC_CACHE_R2_BUCKET",
22+
"bucket_name": "gitbook-open-v2-cache-preview"
23+
}
24+
],
25+
"durable_objects": {
26+
"bindings": [
27+
{
28+
"name": "NEXT_CACHE_DO_QUEUE",
29+
"class_name": "DOQueueHandler"
30+
},
31+
{
32+
"name": "NEXT_TAG_CACHE_DO_SHARDED",
33+
"class_name": "DOShardedTagCache"
34+
},
35+
{
36+
"name": "WRITE_BUFFER",
37+
"class_name": "R2WriteBuffer"
38+
}
39+
]
40+
},
41+
"migrations": [
42+
{
43+
"tag": "v1",
44+
"new_sqlite_classes": ["DOQueueHandler", "DOShardedTagCache", "R2WriteBuffer"]
45+
}
46+
]
47+
},
48+
"staging": {
49+
"vars": {
50+
"STAGE": "staging",
51+
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true"
52+
},
53+
"r2_buckets": [
54+
{
55+
"binding": "NEXT_INC_CACHE_R2_BUCKET",
56+
"bucket_name": "gitbook-open-v2-cache-staging"
57+
}
58+
],
59+
"tail_consumers": [
60+
{
61+
"service": "gitbook-x-staging-tail"
62+
}
63+
],
64+
"durable_objects": {
65+
"bindings": [
66+
{
67+
"name": "NEXT_CACHE_DO_QUEUE",
68+
"class_name": "DOQueueHandler"
69+
},
70+
{
71+
"name": "NEXT_TAG_CACHE_DO_SHARDED",
72+
"class_name": "DOShardedTagCache"
73+
},
74+
{
75+
"name": "WRITE_BUFFER",
76+
"class_name": "R2WriteBuffer"
77+
}
78+
]
79+
},
80+
"migrations": [
81+
{
82+
"tag": "v1",
83+
"new_sqlite_classes": ["DOQueueHandler", "DOShardedTagCache", "R2WriteBuffer"]
84+
}
85+
]
86+
},
87+
"production": {
88+
"vars": {
89+
"NEXT_CACHE_DO_QUEUE_DISABLE_SQLITE": "true",
90+
"STAGE": "production"
91+
},
92+
"r2_buckets": [
93+
{
94+
"binding": "NEXT_INC_CACHE_R2_BUCKET",
95+
"bucket_name": "gitbook-open-v2-cache-production"
96+
}
97+
],
98+
"tail_consumers": [
99+
{
100+
"service": "gitbook-x-prod-tail"
101+
}
102+
],
103+
"durable_objects": {
104+
"bindings": [
105+
{
106+
"name": "NEXT_CACHE_DO_QUEUE",
107+
"class_name": "DOQueueHandler"
108+
},
109+
{
110+
"name": "NEXT_TAG_CACHE_DO_SHARDED",
111+
"class_name": "DOShardedTagCache"
112+
},
113+
{
114+
"name": "WRITE_BUFFER",
115+
"class_name": "R2WriteBuffer"
116+
}
117+
]
118+
},
119+
"migrations": [
120+
{
121+
"tag": "v1",
122+
"new_sqlite_classes": ["DOQueueHandler", "DOShardedTagCache", "R2WriteBuffer"]
123+
}
124+
]
125+
}
126+
}
127+
}

packages/gitbook-v2/openNext/customWorkers/middlewareWrangler.jsonc

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,26 @@
6161
"binding": "DEFAULT_WORKER",
6262
"service": "gitbook-open-v2-server-preview"
6363
}
64-
]
65-
// No durable objects on preview, as they block the generation of preview URLs
66-
// and we don't need tags invalidation on preview
64+
],
65+
"durable_objects": {
66+
"bindings": [
67+
{
68+
"name": "WRITE_BUFFER",
69+
"class_name": "R2WriteBuffer",
70+
"script_name": "gitbook-open-v2-do-preview"
71+
},
72+
{
73+
"name": "NEXT_TAG_CACHE_DO_SHARDED",
74+
"class_name": "DOShardedTagCache",
75+
"script_name": "gitbook-open-v2-do-preview"
76+
},
77+
{
78+
"name": "NEXT_CACHE_DO_QUEUE",
79+
"class_name": "DOQueueHandler",
80+
"script_name": "gitbook-open-v2-do-preview"
81+
}
82+
]
83+
}
6784
},
6885
"staging": {
6986
"vars": {
@@ -104,12 +121,19 @@
104121
"durable_objects": {
105122
"bindings": [
106123
{
107-
"name": "NEXT_CACHE_DO_QUEUE",
108-
"class_name": "DOQueueHandler"
124+
"name": "WRITE_BUFFER",
125+
"class_name": "R2WriteBuffer",
126+
"script_name": "gitbook-open-v2-do-staging"
109127
},
110128
{
111129
"name": "NEXT_TAG_CACHE_DO_SHARDED",
112-
"class_name": "DOShardedTagCache"
130+
"class_name": "DOShardedTagCache",
131+
"script_name": "gitbook-open-v2-do-staging"
132+
},
133+
{
134+
"name": "NEXT_CACHE_DO_QUEUE",
135+
"class_name": "DOQueueHandler",
136+
"script_name": "gitbook-open-v2-do-staging"
113137
}
114138
]
115139
},
@@ -165,12 +189,19 @@
165189
"durable_objects": {
166190
"bindings": [
167191
{
168-
"name": "NEXT_CACHE_DO_QUEUE",
169-
"class_name": "DOQueueHandler"
192+
"name": "WRITE_BUFFER",
193+
"class_name": "R2WriteBuffer",
194+
"script_name": "gitbook-open-v2-do-production"
170195
},
171196
{
172197
"name": "NEXT_TAG_CACHE_DO_SHARDED",
173-
"class_name": "DOShardedTagCache"
198+
"class_name": "DOShardedTagCache",
199+
"script_name": "gitbook-open-v2-do-production"
200+
},
201+
{
202+
"name": "NEXT_CACHE_DO_QUEUE",
203+
"class_name": "DOQueueHandler",
204+
"script_name": "gitbook-open-v2-do-production"
174205
}
175206
]
176207
},

0 commit comments

Comments
 (0)