下午好,我正在开发一个基于 Deno Workers 的 javascript 代码执行环境。我还有一个 Oak Web 服务器,用于处理脚本中的代码更改请求以及这些请求的编译和执行。
当我请求执行脚本(例如console.log())时问题开始了,后来我修改了所述代码,当尝试再次执行此脚本时,Deno 在更改之前执行 Worker,并且仅在我考虑更改时重新启动 Oak 服务器。
export class Runner {
private task: Task;
constructor(task: Task) {
this.task = task;
}
async run() {
new Worker(
new URL(await joinPath(`tasks/${this.task.id}/output.js`), import.meta.url).href,
{ type: "module" }
);
}
}
Runner 类负责初始化 Worker,对于每个执行请求,都会生成一个新的 Runner 实例,因此也会生成 Worker。
// oak router
router.get("/api/tasks/:id/run", async ctx => {
const id: any = ctx.params.id;
if (!id) ctx.throw(500);
const task: Task = await get(id);
const compiler: Compiler = new Compiler(task);
const runner: Runner = new Runner(task);
await compiler.compile();
await runner.run();
ctx.response.body = 'ok';
});
这是处理请求的函数,它反过来实例化 Runner 类。
非常感谢您提前。
最佳答案
有一个开放的issue为此,将来动态加载时缓存将失效。
现在的解决方法是在 URL 中添加查询字符串。并在每次调用 new Worker
时更改它.
let v = 0;
export class Runner {
private task: Task;
constructor(task: Task) {
this.task = task;
}
async run() {
new Worker(
new URL(await joinPath(`tasks/${this.task.id}/output.js?v=${v++}`), import.meta.url).href,
{ type: "module" }
);
}
}
关于javascript - 如何避免在 Deno Workers 中缓存源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62415371/