-
Notifications
You must be signed in to change notification settings - Fork 729
Activities sync fixes #2685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Activities sync fixes #2685
Changes from all commits
498f12d
107c2e7
20e9761
d949ae5
6d78c98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ import { PlatformType } from '@crowd/types' | |||||||||||||||||||||||||||||||||||||||||||||||||||||
import { DB_CONFIG } from '@/conf' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { CrowdJob } from '../../types/jobTypes' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { retryBackoff } from '../../utils/backoff' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
async function decideUpdatedAt(pgQx: QueryExecutor, maxUpdatedAt?: string): Promise<string> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!maxUpdatedAt) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -32,10 +33,15 @@ function createWhereClause(updatedAt: string): string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
return formatQuery('"updatedAt" > $(updatedAt)', { updatedAt }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
async function syncActivitiesBatch( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activityRepo: ActivityRepository, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activities: IDbActivityCreateData[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
async function syncActivitiesBatch({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activityRepo, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activities, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger: Logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activityRepo: ActivityRepository | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activities: IDbActivityCreateData[] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const result = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
inserted: 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
updated: 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -44,15 +50,19 @@ async function syncActivitiesBatch( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
for (const activity of activities) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const existingActivity = await activityRepo.existsWithId(activity.id) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (existingActivity) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
await activityRepo.rawUpdate(activity.id, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
...activity, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
platform: activity.platform as PlatformType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.updated++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
await activityRepo.rawInsert(activity) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.inserted++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (existingActivity) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
await activityRepo.rawUpdate(activity.id, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
...activity, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
platform: activity.platform as PlatformType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.updated++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
await activityRepo.rawInsert(activity) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
result.inserted++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.error(`Error syncing activity ${activity.id}: ${error}`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -97,15 +107,17 @@ export async function syncActivities(logger: Logger, maxUpdatedAt?: string) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
const result = await logExecutionTimeV2( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// eslint-disable-next-line @typescript-eslint/no-loop-func | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
() => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
qdbQx.select( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
retryBackoff(() => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
qdbQx.select( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
SELECT * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
FROM activities | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
WHERE "updatedAt" > $(updatedAt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ORDER BY "updatedAt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
LIMIT 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ updatedAt }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ updatedAt }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+110
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add explicit error handling for retry failures The integration of retryBackoff is good for handling transient failures, but we should handle the case when all retries are exhausted. Consider adding explicit error handling: retryBackoff(() =>
qdbQx.select(
`
SELECT *
FROM activities
WHERE "updatedAt" > $(updatedAt)
ORDER BY "updatedAt"
LIMIT 1000;
`,
{ updatedAt },
- ),
+ )).catch(error => {
+ logger.error(`Failed to fetch activities after all retries: ${error}`);
+ throw new Error(`Activity fetch failed: ${error.message}`);
+ }), 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
`getting activities with updatedAt > ${updatedAt}`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -116,7 +128,11 @@ export async function syncActivities(logger: Logger, maxUpdatedAt?: string) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
const t = timer(logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { inserted, updated } = await syncActivitiesBatch(activityRepo, result) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { inserted, updated } = await syncActivitiesBatch({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activityRepo, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
activities: result, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
t.end(`Inserting ${inserted} and updating ${updated} activities`) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
counter += inserted + updated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { timeout } from '@crowd/common' | ||
|
||
export async function retryBackoff<T>(fn: () => Promise<T>, maxRetries: number = 3): Promise<T> { | ||
let retries = 0 | ||
while (retries < maxRetries) { | ||
try { | ||
return await fn() | ||
} catch (error) { | ||
retries++ | ||
// Exponential backoff with base of 2 seconds | ||
// 1st retry: 2s, 2nd: 4s, 3rd: 8s, etc | ||
const backoffMs = 2000 * 2 ** (retries - 1) | ||
await timeout(backoffMs) | ||
} | ||
} | ||
|
||
throw new Error('Max retries reached') | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling strategy
While adding error handling is good, the current implementation has some potential issues:
Consider implementing more robust error handling: