Thanks @FlikTeoh, totally agree the docs

Thanks @FlikTeoh, totally agree the docs are impressive, I have read the RPC documentation and implemented it across other workers, this scenario is slightly different from documented. Or at least I couldn't work out from the docs how to implement my scenario... I have a worker with the following simplified code:
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Cloudflare Workers!'))
app.get('/analyse', (c) => c.text('Hello Analyse endpoint!'))

export default app
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Cloudflare Workers!'))
app.get('/analyse', (c) => c.text('Hello Analyse endpoint!'))

export default app
In order to do a service to service binding I have the RPC or fetch option. With the RPC approach I have to export a class that extends WorkerEntrypoint , in which case how do I also export the endpoints? If I want to use the HTTP fetch interface how would I export a fetch handler and also the other endpoints via Hono? I'm starting to wonder if there is a problem with my solution design, might rethink it, in the meantime any examples or pointers appreciated.
7 Replies
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
jason
jasonOP3mo ago
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.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
jason
jasonOP3mo ago
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.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
jason
jasonOP3mo ago
Compatibility date change worked, now getting the object back from the RPC call. Thanks for your help, it's much appreciated.
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server