jason
jason
CDCloudflare Developers
Created by jason on 9/6/2024 in #workers-discussions
Thanks @FlikTeoh, totally agree the docs
Compatibility date change worked, now getting the object back from the RPC call. Thanks for your help, it's much appreciated.
10 replies
CDCloudflare Developers
Created by jason on 9/6/2024 in #workers-discussions
Thanks @FlikTeoh, totally agree the docs
The env is being accessed in a CF worker that is a queue consumer, here's the code:
export default {
async fetch(request, env, ctx) {

return new Response(null, {
status: 202,
statusText: "Target Role Analysis Accepted",
headers: {
location: `job-hunt/target-role-analysis/${backgroundTaskId}/${jobSearchInfo.jobSearchTerm}`
}
});
},
async queue(batch, env) {
const messages = batch.messages;
const targetRoleAnalysisInfo = messages[0].body.payload;
console.log("in the target role queue consumer, env is: ", env);
const aiGatewayResponse = await env.jobsAiGateway.getTargetRoleAnalyser();
console.log("in target role analysis worker, the response is: ", aiGatewayResponse);

// Process the response
// Write the analysis to the database
},
};
export default {
async fetch(request, env, ctx) {

return new Response(null, {
status: 202,
statusText: "Target Role Analysis Accepted",
headers: {
location: `job-hunt/target-role-analysis/${backgroundTaskId}/${jobSearchInfo.jobSearchTerm}`
}
});
},
async queue(batch, env) {
const messages = batch.messages;
const targetRoleAnalysisInfo = messages[0].body.payload;
console.log("in the target role queue consumer, env is: ", env);
const aiGatewayResponse = await env.jobsAiGateway.getTargetRoleAnalyser();
console.log("in target role analysis worker, the response is: ", aiGatewayResponse);

// Process the response
// Write the analysis to the database
},
};
As you can see the worker has a fetch handler and a Queue consumer handler, which is passed env from the Queue. The output of the console.log line for env is:
in the target role queue consumer, env is: {
DB: {
alwaysPrimarySession: { fetcher: {}, commitTokenOrConstraint: 'first-primary' },
fetcher: {}
},
jobsAiGateway: {}
}
in the target role queue consumer, env is: {
DB: {
alwaysPrimarySession: { fetcher: {}, commitTokenOrConstraint: 'first-primary' },
fetcher: {}
},
jobsAiGateway: {}
}
Thanks for your help so far, it's much appreciated.
10 replies
CDCloudflare Developers
Created by jason on 9/6/2024 in #workers-discussions
Thanks @FlikTeoh, totally agree the docs
Thanks for the response @FlikTeoh, here's what I tried... here's the code I'm using to invoke the worker via RPC:
const aiGatewayResponse = await env.jobsAiGateway.getTargetRoleAnalyser();
const aiGatewayResponse = await env.jobsAiGateway.getTargetRoleAnalyser();
The service binding definition:
services = [
{ binding = "jobsAiGateway", service = "jobs-ai-gateway", entrypoint = "TargetRoleService" }
]
services = [
{ binding = "jobsAiGateway", service = "jobs-ai-gateway", entrypoint = "TargetRoleService" }
]
but I get this error: TypeError: env.jobsAiGateway.getTargetRoleAnalyser is not a function And the code in the jobsAiGateway worker is:
app.get('/profile', async (ctx) => {
// do some profile handling
return httpResponse;
});

class TargetRoleAnalysis extends RpcTarget {
analyse(targetRoleDescription) {
console.log(
"in target role analysis Service, analysing...", targetRoleDescription);
}
}

export class TargetRoleService extends WorkerEntrypoint {
async getTargetRoleAnalyser() {
return new TargetRoleAnalysis();
}
}

export default app
app.get('/profile', async (ctx) => {
// do some profile handling
return httpResponse;
});

class TargetRoleAnalysis extends RpcTarget {
analyse(targetRoleDescription) {
console.log(
"in target role analysis Service, analysing...", targetRoleDescription);
}
}

export class TargetRoleService extends WorkerEntrypoint {
async getTargetRoleAnalyser() {
return new TargetRoleAnalysis();
}
}

export default app
Not sure what I'm doing wrong, any pointers would be greatly appreciated.
10 replies