Does ctx.waitUntil work with hyperdrive?

Hello I have the following worker that listens for notifications from a service.
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
if (request.method !== "POST") {
return new Response(null, {
status: 405,
statusText: "Method Not Allowed",
});
}

const userAgent = request.headers.get("user-agent");
const data = await request.json();
const connectionString = env.staging.connectionString;

console.log(userAgent, data, connectionString);

if (userAgent?.includes("<example>")) {
console.log("correct agent")

const sql = postgres(connectionString, { prepare: false });
ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (999);`);

return new Response(undefined, { status: 200 });
}

return new Response(undefined, { status: 405 });
},
};
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext,
): Promise<Response> {
if (request.method !== "POST") {
return new Response(null, {
status: 405,
statusText: "Method Not Allowed",
});
}

const userAgent = request.headers.get("user-agent");
const data = await request.json();
const connectionString = env.staging.connectionString;

console.log(userAgent, data, connectionString);

if (userAgent?.includes("<example>")) {
console.log("correct agent")

const sql = postgres(connectionString, { prepare: false });
ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (999);`);

return new Response(undefined, { status: 200 });
}

return new Response(undefined, { status: 405 });
},
};
The service disconnects immediately on making the connection and delivering the notification and ctx.waitUntil is not functioning and the database is never updated. Is this a problem with using hyperdrive here? Am I doing something else wrong? When I test this api endpoint from my machine it works but I do not disconnect immediately like the service
1 Reply
ink pot monkey ✒🐒
Actually further testing shows that even testing locally any database updates I try to do with ctx.waitUntil never complete. Success:
await sql`INSERT INTO payment_logs(user_id) VALUES (1111);`;
await sql`INSERT INTO payment_logs(user_id) VALUES (1111);`;
Failure:
ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (1111);`);
ctx.waitUntil(sql`INSERT INTO payment_logs(user_id) VALUES (1111);`);
clarification: by testing locally I mean hitting the worker url from my machine
Want results from more Discord servers?
Add your server