12
12
13
13
namespace workerd ::api::pyodide {
14
14
15
+ struct PythonConfig {
16
+ kj::Maybe<kj::Own<const kj::Directory>> diskCacheRoot;
17
+ bool createSnapshot;
18
+ bool createBaselineSnapshot;
19
+ };
20
+
15
21
// A function to read a segment of the tar file into a buffer
16
22
// Set up this way to avoid copying files that aren't accessed.
17
23
class PackagesTarReader : public jsg ::Object {
@@ -36,17 +42,20 @@ class PyodideMetadataReader : public jsg::Object {
36
42
kj::Array<kj::String> requirements;
37
43
bool isWorkerdFlag;
38
44
bool isTracingFlag;
45
+ bool snapshotToDisk;
39
46
bool createBaselineSnapshot;
40
47
kj::Maybe<kj::Array<kj::byte>> memorySnapshot;
41
48
42
49
public:
43
50
PyodideMetadataReader (kj::String mainModule, kj::Array<kj::String> names,
44
51
kj::Array<kj::Array<kj::byte>> contents, kj::Array<kj::String> requirements,
45
52
bool isWorkerd, bool isTracing,
53
+ bool snapshotToDisk,
46
54
bool createBaselineSnapshot,
47
55
kj::Maybe<kj::Array<kj::byte>> memorySnapshot)
48
56
: mainModule(kj::mv(mainModule)), names(kj::mv(names)), contents(kj::mv(contents)),
49
57
requirements (kj::mv(requirements)), isWorkerdFlag(isWorkerd), isTracingFlag(isTracing),
58
+ snapshotToDisk(snapshotToDisk),
50
59
createBaselineSnapshot(createBaselineSnapshot),
51
60
memorySnapshot(kj::mv(memorySnapshot)) {}
52
61
@@ -58,6 +67,10 @@ class PyodideMetadataReader : public jsg::Object {
58
67
return this ->isTracingFlag ;
59
68
}
60
69
70
+ bool shouldSnapshotToDisk () {
71
+ return snapshotToDisk;
72
+ }
73
+
61
74
bool isCreatingBaselineSnapshot () {
62
75
return createBaselineSnapshot;
63
76
}
@@ -101,6 +114,7 @@ class PyodideMetadataReader : public jsg::Object {
101
114
JSG_METHOD (getMemorySnapshotSize);
102
115
JSG_METHOD (readMemorySnapshot);
103
116
JSG_METHOD (disposeMemorySnapshot);
117
+ JSG_METHOD (shouldSnapshotToDisk);
104
118
JSG_METHOD (isCreatingBaselineSnapshot);
105
119
}
106
120
@@ -131,24 +145,21 @@ class ArtifactBundler : public jsg::Object {
131
145
existingSnapshot (kj::mv(existingSnapshot)),
132
146
uploadMemorySnapshotCb (kj::mv(uploadMemorySnapshotCb)),
133
147
hasUploaded (false ),
134
- isValidating (false ),
135
- createBaselineSnapshot (false ) {};
148
+ isValidating (false ) {};
136
149
137
150
ArtifactBundler (kj::Maybe<kj::Array<kj::byte>> existingSnapshot)
138
151
: storedSnapshot(kj::none),
139
152
existingSnapshot (kj::mv(existingSnapshot)),
140
153
uploadMemorySnapshotCb(kj::none),
141
154
hasUploaded(false ),
142
- isValidating(false ),
143
- createBaselineSnapshot(false ) {};
155
+ isValidating(false ){};
144
156
145
157
ArtifactBundler (bool isValidating = false )
146
158
: storedSnapshot(kj::none),
147
159
existingSnapshot(kj::none),
148
160
uploadMemorySnapshotCb(kj::none),
149
161
hasUploaded(false ),
150
- isValidating(isValidating),
151
- createBaselineSnapshot(false ) {};
162
+ isValidating(isValidating){};
152
163
153
164
jsg::Promise<bool > uploadMemorySnapshot (jsg::Lock& js, kj::Array<kj::byte> snapshot) {
154
165
// Prevent multiple uploads.
@@ -228,7 +239,6 @@ class ArtifactBundler : public jsg::Object {
228
239
kj::Maybe<kj::Function<kj::Promise<bool >(kj::Array<kj::byte> snapshot)>> uploadMemorySnapshotCb;
229
240
bool hasUploaded;
230
241
bool isValidating;
231
- bool createBaselineSnapshot;
232
242
};
233
243
234
244
@@ -243,13 +253,19 @@ class DisabledInternalJaeger : public jsg::Object {
243
253
244
254
// This cache is used by Pyodide to store wheels fetched over the internet across workerd restarts in local dev only
245
255
class DiskCache : public jsg ::Object {
256
+ private:
246
257
static const kj::Maybe<kj::Own<const kj::Directory>> NULL_CACHE_ROOT; // always set to kj::none
247
258
248
259
const kj::Maybe<kj::Own<const kj::Directory>> &cacheRoot;
260
+
249
261
public:
250
262
DiskCache (): cacheRoot(NULL_CACHE_ROOT) {}; // Disabled disk cache
251
263
DiskCache (const kj::Maybe<kj::Own<const kj::Directory>> &cacheRoot): cacheRoot(cacheRoot) {};
252
264
265
+ static jsg::Ref<DiskCache> makeDisabled () {
266
+ return jsg::alloc<DiskCache>();
267
+ }
268
+
253
269
jsg::Optional<kj::Array<kj::byte>> get (jsg::Lock& js, kj::String key);
254
270
void put (jsg::Lock& js, kj::String key, kj::Array<kj::byte> data);
255
271
@@ -275,14 +291,14 @@ class SimplePythonLimiter : public jsg::Object {
275
291
276
292
277
293
public:
278
- SimplePythonLimiter (int startupLimitMs, kj::Function<kj::TimePoint()> getTimeCb) :
279
- startupLimitMs(startupLimitMs),
280
- getTimeCb(kj::mv(getTimeCb)) {}
281
-
282
294
SimplePythonLimiter () :
283
295
startupLimitMs(0 ),
284
296
getTimeCb(kj::none) {}
285
297
298
+ SimplePythonLimiter (int startupLimitMs, kj::Function<kj::TimePoint()> getTimeCb) :
299
+ startupLimitMs(startupLimitMs),
300
+ getTimeCb(kj::mv(getTimeCb)) {}
301
+
286
302
static jsg::Ref<SimplePythonLimiter> makeDisabled () {
287
303
return jsg::alloc<SimplePythonLimiter>();
288
304
}
@@ -313,7 +329,7 @@ class SimplePythonLimiter : public jsg::Object {
313
329
314
330
using Worker = server::config::Worker;
315
331
316
- jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader (Worker::Reader conf);
332
+ jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader (Worker::Reader conf, const PythonConfig& pythonConfig );
317
333
318
334
bool hasPythonModules (capnp::List<server::config::Worker::Module>::Reader modules);
319
335
0 commit comments