Skip to content

Commit 471ad1d

Browse files
committed
fix: use packages temporal on api
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent a19af95 commit 471ad1d

6 files changed

Lines changed: 66 additions & 1 deletion

File tree

backend/config/custom-environment-variables.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@
180180
"certificate": "CROWD_TEMPORAL_CERTIFICATE",
181181
"privateKey": "CROWD_TEMPORAL_PRIVATE_KEY"
182182
},
183+
"packagesTemporal": {
184+
"serverUrl": "CROWD_TEMPORAL_SERVER_URL",
185+
"namespace": "CROWD_PACKAGES_TEMPORAL_NAMESPACE",
186+
"certificate": "CROWD_TEMPORAL_CERTIFICATE",
187+
"privateKey": "CROWD_TEMPORAL_PRIVATE_KEY"
188+
},
183189
"searchSyncApi": {
184190
"baseUrl": "CROWD_SEARCH_SYNC_API_URL"
185191
},

backend/config/default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"isEnabled": "false"
4444
},
4545
"temporal": {},
46+
"packagesTemporal": {},
4647
"searchSyncApi": {},
4748
"encryption": {},
4849
"openStatusApi": {},

backend/src/api/public/v1/packages/getPackage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { WorkflowIdConflictPolicy, WorkflowIdReusePolicy } from '@crowd/temporal'
1414

1515
import { getPackagesQx } from '@/db/packagesDb'
16+
import { getPackagesTemporalClient } from '@/db/packagesTemporal'
1617
import { ok } from '@/utils/api'
1718
import { validateOrThrow } from '@/utils/validation'
1819

@@ -54,7 +55,8 @@ export async function getPackage(req: Request, res: Response): Promise<void> {
5455

5556
if (pkg.contactsLastRefreshed == null) {
5657
try {
57-
await req.temporal.workflow.execute('ingestSecurityContactsForPurlWorkflow', {
58+
const packagesTemporal = await getPackagesTemporalClient()
59+
await packagesTemporal.workflow.execute('ingestSecurityContactsForPurlWorkflow', {
5860
taskQueue: 'packages-worker',
5961
workflowId: ondemandWorkflowId(purl),
6062
workflowIdReusePolicy: WorkflowIdReusePolicy.ALLOW_DUPLICATE,

backend/src/conf/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ export const PACKAGES_DB_CONFIG: IDatabaseConfig | undefined = config.has('packa
8686
? config.get<IDatabaseConfig>('packagesDb')
8787
: undefined
8888

89+
// packages_worker (npm/maven/pypi/osv/security-contacts/...) runs in its own Temporal
90+
// namespace, separate from the API's default namespace — see CROWD_PACKAGES_TEMPORAL_NAMESPACE.
91+
export const PACKAGES_TEMPORAL_CONFIG: ITemporalConfig | undefined = config.has(
92+
'packagesTemporal.namespace',
93+
)
94+
? config.get<ITemporalConfig>('packagesTemporal')
95+
: undefined
96+
8997
export const SEGMENT_CONFIG: SegmentConfiguration = config.get<SegmentConfiguration>('segment')
9098

9199
export const COMPREHEND_CONFIG: ComprehendConfiguration =

backend/src/db/packagesTemporal.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Client, Connection, getDataConverter } from '@crowd/temporal'
2+
import { IS_DEV_ENV, SERVICE } from '@crowd/common'
3+
4+
import { PACKAGES_TEMPORAL_CONFIG } from '@/conf'
5+
6+
let _init: Promise<Client> | undefined
7+
8+
// Separate connection from the API's default req.temporal client — packages_worker
9+
// (npm/maven/pypi/osv/security-contacts/...) polls task queues in its own Temporal
10+
// namespace (CROWD_PACKAGES_TEMPORAL_NAMESPACE), not the API's default namespace.
11+
export function getPackagesTemporalClient(): Promise<Client> {
12+
if (!_init) {
13+
if (!PACKAGES_TEMPORAL_CONFIG?.serverUrl) {
14+
throw new Error(
15+
'Packages Temporal is not configured — set CROWD_PACKAGES_TEMPORAL_NAMESPACE',
16+
)
17+
}
18+
19+
const cfg = PACKAGES_TEMPORAL_CONFIG
20+
_init = Connection.connect({
21+
address: cfg.serverUrl,
22+
tls:
23+
cfg.certificate && cfg.privateKey
24+
? {
25+
clientCertPair: {
26+
crt: Buffer.from(cfg.certificate, 'base64'),
27+
key: Buffer.from(cfg.privateKey, 'base64'),
28+
},
29+
}
30+
: undefined,
31+
})
32+
.then(
33+
async (connection) =>
34+
new Client({
35+
connection,
36+
namespace: cfg.namespace,
37+
identity: SERVICE,
38+
dataConverter: IS_DEV_ENV ? undefined : await getDataConverter(),
39+
}),
40+
)
41+
.catch((err) => {
42+
_init = undefined
43+
throw err
44+
})
45+
}
46+
return _init
47+
}

scripts/services/security-contacts-worker.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ x-env-args: &env-args
77
SHELL: /bin/sh
88
SUPPRESS_NO_CONFIG_WARNING: 'true'
99
CROWD_TEMPORAL_TASKQUEUE: packages-worker
10+
CROWD_TEMPORAL_NAMESPACE: ${CROWD_PACKAGES_TEMPORAL_NAMESPACE}
1011

1112
services:
1213
security-contacts-worker:

0 commit comments

Comments
 (0)