Skip to content

Commit a965066

Browse files
committed
chore: cleanup enricher config
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent af3a4e5 commit a965066

5 files changed

Lines changed: 22 additions & 40 deletions

File tree

backend/.env.dist.local

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,9 @@ CROWD_PACKAGES_DB_PORT=5434
174174
CROWD_PACKAGES_DB_USERNAME=postgres
175175
CROWD_PACKAGES_DB_PASSWORD=example
176176
CROWD_PACKAGES_DB_DATABASE=packages-db
177+
178+
# github-repos-enricher
179+
ENRICHER_GITHUB_TOKENS=
180+
ENRICHER_BATCH_SIZE=100
181+
ENRICHER_REPO_UPDATE_INTERVAL_HOURS=24
182+
ENRICHER_IDLE_SLEEP_SEC=60

scripts/services/github-repos-enricher.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ x-env-args: &env-args
66
SERVICE: github-repos-enricher
77
SHELL: /bin/sh
88
SUPPRESS_NO_CONFIG_WARNING: 'true'
9-
PAGE_SIZE: '200'
10-
BATCH_SIZE: '50'
11-
MAX_RETRIES: '3'
12-
UPDATE_INTERVAL_HOURS: '24'
13-
IDLE_SLEEP_SEC: '60'
9+
ENRICHER_BATCH_SIZE: '100'
10+
ENRICHER_REPO_UPDATE_INTERVAL_HOURS: '24'
11+
ENRICHER_IDLE_SLEEP_SEC: '60'
1412

1513
services:
1614
github-repos-enricher:

services/apps/packages_worker/src/bin/github-repos-enricher.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import fs from 'fs'
2-
import path from 'path'
3-
41
import { getServiceLogger } from '@crowd/logging'
52

63
import { getEnricherConfig } from '../config'
@@ -9,9 +6,6 @@ import { runEnrichmentLoop } from '../enricher/runEnrichmentLoop'
96

107
const log = getServiceLogger()
118

12-
const liveFilePath = path.join(__dirname, '../tmp/github-repos-enricher-live.tmp')
13-
const readyFilePath = path.join(__dirname, '../tmp/github-repos-enricher-ready.tmp')
14-
159
let shuttingDown = false
1610

1711
const shutdown = async () => {
@@ -37,28 +31,13 @@ const main = async () => {
3731
await qx.selectOne('SELECT 1')
3832
log.info('Connected to packages-db.')
3933

40-
fs.mkdirSync(path.dirname(liveFilePath), { recursive: true })
41-
42-
const healthInterval = setInterval(async () => {
43-
if (shuttingDown) return
44-
try {
45-
await Promise.all([
46-
fs.promises.open(liveFilePath, 'a').then((f) => f.close()),
47-
fs.promises.open(readyFilePath, 'a').then((f) => f.close()),
48-
])
49-
} catch (err) {
50-
log.warn({ err }, 'Failed to write health probe files')
51-
}
52-
}, 5000)
53-
5434
log.info(
5535
{ tokens: config.tokens.length, pageSize: config.pageSize, batchSize: config.batchSize },
5636
'Starting enrichment loop',
5737
)
5838

5939
await runEnrichmentLoop(qx, config, () => shuttingDown)
6040

61-
clearInterval(healthInterval)
6241
log.info('github-repos-enricher stopped.')
6342
process.exit(0)
6443
}

services/apps/packages_worker/src/config.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ export function getEnricherConfig() {
2424

2525
return {
2626
tokens,
27-
pageSize: requireEnvInt('PAGE_SIZE'),
28-
batchSize: requireEnvInt('BATCH_SIZE'),
29-
maxRetries: requireEnvInt('MAX_RETRIES'),
30-
updateIntervalHours: requireEnvInt('UPDATE_INTERVAL_HOURS'),
31-
idleSleepSec: requireEnvInt('IDLE_SLEEP_SEC'),
27+
batchSize: requireEnvInt('ENRICHER_BATCH_SIZE'),
28+
updateIntervalHours: requireEnvInt('ENRICHER_REPO_UPDATE_INTERVAL_HOURS'),
29+
idleSleepSec: requireEnvInt('ENRICHER_IDLE_SLEEP_SEC'),
3230
}
3331
}

services/apps/packages_worker/src/enricher/runEnrichmentLoop.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import { updateEnrichedRepos } from './updateEnrichedRepos'
88

99
const log = getServiceChildLogger('github-repos-enricher')
1010

11+
const MAX_RETRIES = 3
12+
1113
async function fetchWithRetries(
1214
url: string,
1315
token: string,
14-
maxRetries: number,
1516
): Promise<LightRepoResult | null> {
16-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
17+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
1718
try {
1819
return await fetchLightRepo(url, token)
1920
} catch (err) {
@@ -26,12 +27,12 @@ async function fetchWithRetries(
2627

2728
if (err.kind === 'RATE_LIMIT') throw err
2829

29-
if (attempt < maxRetries) {
30+
if (attempt < MAX_RETRIES) {
3031
const backoffMs = 1000 * 2 ** attempt
3132
log.warn({ url, attempt, backoffMs }, `Transient error, retrying: ${err.message}`)
3233
await new Promise((r) => setTimeout(r, backoffMs))
3334
} else {
34-
log.error({ url }, `Gave up after ${maxRetries} retries: ${err.message}`)
35+
log.error({ url }, `Gave up after ${MAX_RETRIES} retries: ${err.message}`)
3536
return null
3637
}
3738
}
@@ -42,7 +43,7 @@ async function fetchWithRetries(
4243
async function fetchPage(
4344
qx: QueryExecutor,
4445
cursor: string | null,
45-
pageSize: number,
46+
batchSize: number,
4647
updateIntervalHours: number,
4748
): Promise<{ rows: Array<{ id: string; url: string }>; urls: string[] }> {
4849
const rows = await qx.select(
@@ -53,9 +54,9 @@ async function fetchPage(
5354
AND (last_synced_at IS NULL OR last_synced_at < NOW() - INTERVAL '$(updateIntervalHours) hours')
5455
AND ($(cursor) IS NULL OR id > $(cursor))
5556
ORDER BY id
56-
LIMIT $(pageSize)
57+
LIMIT $(batchSize)
5758
`,
58-
{ cursor, pageSize, updateIntervalHours },
59+
{ cursor, batchSize, updateIntervalHours },
5960
)
6061
return {
6162
rows,
@@ -103,7 +104,7 @@ async function processPage(
103104
const url = validUrls[idx]
104105

105106
try {
106-
const result = await fetchWithRetries(url, token, config.maxRetries)
107+
const result = await fetchWithRetries(url, token)
107108
if (result) {
108109
buffer.push(result)
109110
if (buffer.length >= config.batchSize) {
@@ -161,7 +162,7 @@ export async function runEnrichmentLoop(
161162
while (!isShuttingDown()) {
162163
pageNum++
163164

164-
const { rows, urls } = await fetchPage(qx, cursor, config.pageSize, config.updateIntervalHours)
165+
const { rows, urls } = await fetchPage(qx, cursor, config.batchSize, config.updateIntervalHours)
165166

166167
if (urls.length === 0) {
167168
log.info('No more repos to process, sleeping')

0 commit comments

Comments
 (0)