Skip to content

Commit ba8b4c2

Browse files
committed
fix: small ones
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 969567e commit ba8b4c2

3 files changed

Lines changed: 54 additions & 44 deletions

File tree

services/apps/cron_service/src/jobs/incomingWebhooksCheck.job.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ const job: IJobDefinition = {
2323
const admin = kafkaClient.admin()
2424
await admin.connect()
2525

26-
const counts = await getKafkaMessageCounts(ctx.log, admin, TOPIC, GROUP_ID)
26+
let counts: { total: number; consumed: number; unconsumed: number }
27+
try {
28+
counts = await getKafkaMessageCounts(ctx.log, admin, TOPIC, GROUP_ID)
29+
} finally {
30+
await admin.disconnect()
31+
}
2732

2833
if (counts.unconsumed >= MAX_UNCONSUMED) {
2934
ctx.log.info(

services/apps/cron_service/src/jobs/integrationResultsCheck.job.ts

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,63 @@ const job: IJobDefinition = {
2222
const admin = kafkaClient.admin()
2323
await admin.connect()
2424

25-
const counts = await getKafkaMessageCounts(ctx.log, admin, topic, groupId)
25+
try {
26+
const counts = await getKafkaMessageCounts(ctx.log, admin, topic, groupId)
2627

27-
// if we have less than 50k messages in the queue we can trigger 50k oldest results (we process between 100k and 300k results per hour on average)
28-
if (counts.unconsumed < 50000) {
29-
const dbConnection = await getDbConnection(WRITE_DB_CONFIG())
28+
// if we have less than 50k messages in the queue we can trigger 50k oldest results (we process between 100k and 300k results per hour on average)
29+
if (counts.unconsumed < 50000) {
30+
const dbConnection = await getDbConnection(WRITE_DB_CONFIG())
3031

31-
// we check if we have more than unconsumed pending results so that we don't trigger just the ones in the queue :)
32-
const count = (
33-
await dbConnection.one(
34-
`select count(*) as count from integration.results where state = '${IntegrationResultState.PENDING}'`,
35-
)
36-
).count
32+
// we check if we have more than unconsumed pending results so that we don't trigger just the ones in the queue :)
33+
const count = (
34+
await dbConnection.one(
35+
`select count(*) as count from integration.results where state = '${IntegrationResultState.PENDING}'`,
36+
)
37+
).count
3738

38-
if (count > counts.unconsumed) {
39-
ctx.log.info(`We have ${count} pending results, triggering 100k oldest results!`)
39+
if (count > counts.unconsumed) {
40+
ctx.log.info(`We have ${count} pending results, triggering 100k oldest results!`)
4041

41-
const queueService = new KafkaQueueService(kafkaClient, ctx.log)
42-
const dswEmitter = new DataSinkWorkerEmitter(queueService, ctx.log)
43-
await dswEmitter.init()
42+
const queueService = new KafkaQueueService(kafkaClient, ctx.log)
43+
const dswEmitter = new DataSinkWorkerEmitter(queueService, ctx.log)
44+
await dswEmitter.init()
4445

45-
const resultIds = (
46-
await dbConnection.any(
47-
`select id from integration.results where state = 'pending' order by "createdAt" desc limit 100000`,
48-
)
49-
).map((r) => r.id)
50-
51-
let triggered = 0
52-
53-
for (const batch of partition(resultIds, 10)) {
54-
const messages = batch.map((resultId) => {
55-
return {
56-
payload: {
57-
type: DataSinkWorkerQueueMessageType.PROCESS_INTEGRATION_RESULT,
58-
resultId,
59-
},
60-
groupId: generateUUIDv1(),
61-
deduplicationId: resultId,
62-
}
63-
})
46+
const resultIds = (
47+
await dbConnection.any(
48+
`select id from integration.results where state = 'pending' order by "createdAt" desc limit 100000`,
49+
)
50+
).map((r) => r.id)
51+
52+
let triggered = 0
6453

65-
await dswEmitter.sendMessages(messages)
54+
for (const batch of partition(resultIds, 10)) {
55+
const messages = batch.map((resultId) => {
56+
return {
57+
payload: {
58+
type: DataSinkWorkerQueueMessageType.PROCESS_INTEGRATION_RESULT,
59+
resultId,
60+
},
61+
groupId: generateUUIDv1(),
62+
deduplicationId: resultId,
63+
}
64+
})
6665

67-
triggered += batch.length
66+
await dswEmitter.sendMessages(messages)
6867

69-
if (triggered % 1000 === 0) {
70-
ctx.log.info(`Triggered ${triggered} results!`)
68+
triggered += batch.length
69+
70+
if (triggered % 1000 === 0) {
71+
ctx.log.info(`Triggered ${triggered} results!`)
72+
}
7173
}
72-
}
7374

74-
ctx.log.info(`Triggered ${triggered} results in total!`)
75+
ctx.log.info(`Triggered ${triggered} results in total!`)
76+
}
77+
} else {
78+
ctx.log.info(`We have ${counts.unconsumed} unconsumed messages in the queue, skipping!`)
7579
}
76-
} else {
77-
ctx.log.info(`We have ${counts.unconsumed} unconsumed messages in the queue, skipping!`)
80+
} finally {
81+
await admin.disconnect()
7882
}
7983
},
8084
}

services/apps/cron_service/src/jobs/integrationResultsReporting.job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import CronTime from 'cron-time-generator'
22

3-
import { IS_DEV_ENV } from '@crowd/common'
3+
import { IS_DEV_ENV, IS_PROD_ENV } from '@crowd/common'
44
import { READ_DB_CONFIG, getDbConnection } from '@crowd/data-access-layer/src/database'
55
import {
66
SlackChannel,
@@ -33,6 +33,7 @@ const job: IJobDefinition = {
3333
name: 'integration-results-reporting',
3434
cronTime: IS_DEV_ENV ? CronTime.everyMinute() : CronTime.everyDayAt(8, 30),
3535
timeout: 10 * 60, // 10 minutes
36+
enabled: async () => IS_PROD_ENV,
3637
process: async (ctx) => {
3738
ctx.log.info('Running integration-results-reporting job...')
3839

0 commit comments

Comments
 (0)