Skip to content

Commit 6aaa7eb

Browse files
committed
feat(worker): add X-Entity-Id header to job dispatch pipeline
- Add entity_id to JobRow interface (worker reads from app_jobs.jobs) - Add entityId to RequestOptions and emit X-Entity-Id header on dispatch - Add entityId to FunctionContext.job (fn-types) - Read X-Entity-Id header in fn-runtime context and server - Add entityId to JobContext in fn-app, echo on responses, forward in callbacks This completes the entity_id promotion from DB column to HTTP header, making entity context available to cloud functions for billing/metering.
1 parent dad7293 commit 6aaa7eb

6 files changed

Lines changed: 19 additions & 4 deletions

File tree

job/worker/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface JobRow {
1212
payload?: unknown;
1313
database_id?: string;
1414
actor_id?: string;
15+
entity_id?: string;
1516
}
1617

1718
const log = new Logger('jobs:worker');
@@ -119,6 +120,7 @@ export default class Worker {
119120
body: payload,
120121
databaseId: job.database_id,
121122
actorId: job.actor_id,
123+
entityId: job.entity_id,
122124
workerId: this.workerId,
123125
jobId: job.id
124126
});

job/worker/src/req.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ interface RequestOptions {
3333
body: unknown;
3434
databaseId?: string;
3535
actorId?: string;
36+
entityId?: string;
3637
workerId: string;
3738
jobId: string | number;
3839
}
3940

4041
const request = (
4142
fn: string,
42-
{ body, databaseId, actorId, workerId, jobId }: RequestOptions
43+
{ body, databaseId, actorId, entityId, workerId, jobId }: RequestOptions
4344
) => {
4445
const url = getFunctionUrl(fn);
4546
log.info(`dispatching job`, {
@@ -77,6 +78,7 @@ const request = (
7778
'X-Job-Id': String(jobId),
7879
...(databaseId ? { 'X-Database-Id': databaseId } : {}),
7980
...(actorId ? { 'X-Actor-Id': actorId } : {}),
81+
...(entityId ? { 'X-Entity-Id': entityId } : {}),
8082

8183
// async HTTP completion callback
8284
'X-Callback-Url': completeUrl

packages/fn-app/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type JobContext = {
1414
jobId: string | undefined;
1515
databaseId: string | undefined;
1616
actorId: string | undefined;
17+
entityId: string | undefined;
1718
};
1819

1920
function getHeaders(req: any) {
@@ -22,6 +23,7 @@ function getHeaders(req: any) {
2223
'x-job-id': req.get('X-Job-Id'),
2324
'x-database-id': req.get('X-Database-Id'),
2425
'x-actor-id': req.get('X-Actor-Id'),
26+
'x-entity-id': req.get('X-Entity-Id'),
2527
'x-callback-url': req.get('X-Callback-Url')
2628
};
2729
}
@@ -105,6 +107,10 @@ const sendJobCallback = async (
105107
headers['X-Actor-Id'] = ctx.actorId;
106108
}
107109

110+
if (ctx.entityId) {
111+
headers['X-Entity-Id'] = ctx.entityId;
112+
}
113+
108114
const body: Record<string, unknown> = {
109115
status
110116
};
@@ -175,6 +181,7 @@ const createJobApp = () => {
175181
'X-Worker-Id': req.get('X-Worker-Id'),
176182
'X-Database-Id': req.get('X-Database-Id'),
177183
'X-Actor-Id': req.get('X-Actor-Id'),
184+
'X-Entity-Id': req.get('X-Entity-Id'),
178185
'X-Job-Id': req.get('X-Job-Id')
179186
});
180187
next();
@@ -187,7 +194,8 @@ const createJobApp = () => {
187194
workerId: req.get('X-Worker-Id'),
188195
jobId: req.get('X-Job-Id'),
189196
databaseId: req.get('X-Database-Id'),
190-
actorId: req.get('X-Actor-Id')
197+
actorId: req.get('X-Actor-Id'),
198+
entityId: req.get('X-Entity-Id')
191199
};
192200

193201
// Store on res.locals so the error middleware can also mark callbacks as sent.

packages/fn-runtime/src/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createClients } from './graphql';
55
type RequestHeaders = {
66
databaseId?: string;
77
actorId?: string;
8+
entityId?: string;
89
workerId?: string;
910
jobId?: string;
1011
};
@@ -16,7 +17,7 @@ export const buildContext = (
1617
const env = process.env as Record<string, string | undefined>;
1718
const log = createLogger(options.name || 'fn-runtime');
1819

19-
const { databaseId, actorId, workerId, jobId } = headers;
20+
const { databaseId, actorId, entityId, workerId, jobId } = headers;
2021

2122
// Create GraphQL clients if databaseId is available and GRAPHQL_URL is set
2223
let client: FunctionContext['client'];
@@ -46,7 +47,7 @@ export const buildContext = (
4647
}
4748

4849
return {
49-
job: { jobId, workerId, databaseId, actorId },
50+
job: { jobId, workerId, databaseId, actorId, entityId },
5051
client,
5152
meta,
5253
log,

packages/fn-runtime/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const createFunctionServer = (
1414
{
1515
databaseId: req.get('X-Database-Id') || req.get('x-database-id') || process.env.DEFAULT_DATABASE_ID,
1616
actorId: req.get('X-Actor-Id') || req.get('x-actor-id'),
17+
entityId: req.get('X-Entity-Id') || req.get('x-entity-id'),
1718
workerId: req.get('X-Worker-Id') || req.get('x-worker-id'),
1819
jobId: req.get('X-Job-Id') || req.get('x-job-id')
1920
},

packages/fn-types/src/runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type FunctionContext = {
1717
workerId?: string;
1818
databaseId?: string;
1919
actorId?: string;
20+
entityId?: string;
2021
};
2122
client: GraphQLClient;
2223
meta: GraphQLClient;

0 commit comments

Comments
 (0)