Skip to content

Commit ea932a5

Browse files
committed
fix: throttle how many times we update updatedAt
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 8a05d36 commit ea932a5

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

services/apps/data_sink_worker/src/service/organization.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class OrganizationService extends LoggerBase {
2828
): Promise<string> {
2929
const id = await this.store.transactionally(async (txStore) => {
3030
const qe = dbStoreQx(txStore)
31-
const id = await findOrCreateOrganization(qe, source, data, integrationId)
31+
const id = await findOrCreateOrganization(qe, source, data, integrationId, true)
3232
return id
3333
})
3434

services/libs/data-access-layer/src/organizations/base.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ export async function updateOrganization(
367367
qe: QueryExecutor,
368368
organizationId: string,
369369
data: Partial<IDbOrganizationInput>,
370+
throttleUpdatedAt = false,
370371
): Promise<string | null> {
371372
const columns = Object.keys(data)
372373
if (columns.length === 0) {
@@ -376,9 +377,13 @@ export async function updateOrganization(
376377
const updatedAt = new Date()
377378
columns.push('updatedAt')
378379

380+
const updatedAtExpr = throttleUpdatedAt
381+
? `CASE WHEN "updatedAt" < now() - interval '30 minutes' THEN $(updatedAt) ELSE "updatedAt" END`
382+
: `$(updatedAt)`
383+
379384
const query = `
380385
update organizations set
381-
${columns.map((c) => `"${c}" = $(${c})`).join(',\n')}
386+
${columns.map((c) => `"${c}" = ${c === 'updatedAt' ? updatedAtExpr : `$(${c})`}`).join(',\n')}
382387
where id = $(organizationId)
383388
returning id;
384389
`
@@ -443,6 +448,7 @@ export async function findOrCreateOrganization(
443448
source: string,
444449
data: IOrganization,
445450
integrationId?: string,
451+
throttleUpdatedAt = false,
446452
): Promise<string | undefined> {
447453
const verifiedIdentities = data.identities ? data.identities.filter((i) => i.verified) : []
448454

@@ -524,7 +530,8 @@ export async function findOrCreateOrganization(
524530
if (Object.keys(processed.organization).length > 0) {
525531
log.info({ orgId: existing.id }, `Updating organization!`)
526532
await logExecutionTimeV2(
527-
async () => updateOrganization(qe, existing.id, processed.organization),
533+
async () =>
534+
updateOrganization(qe, existing.id, processed.organization, throttleUpdatedAt),
528535
log,
529536
'organizationService -> findOrCreateOrganization -> updateOrganization',
530537
)

0 commit comments

Comments
 (0)