-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user mbdscore sync workers and cronjob
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { schedule } from "node-cron"; | ||
import config from "../../config"; | ||
import { ModuleThread, Pool, spawn, Worker } from "threads"; | ||
Check failure on line 3 in src/services/cronJobs/syncUsersModelScore.ts GitHub Actions / test
|
||
import { logger } from "../../utils/logger"; | ||
import { findActiveQfRound, findUsersWithoutMBDScoreInActiveAround } from "../../repositories/qfRoundRepository"; | ||
import { UserMDBScoreSyncWorker } from "../../workers/usersMBDScoreSyncWorker"; | ||
import { findUserById } from "../../repositories/userRepository"; | ||
import { UserQfRoundModelScore } from "../../entities/userQfRoundModelScore"; | ||
|
||
const cronJobTime = | ||
(config.get('MAKE_UNREVIEWED_PROJECT_LISTED_CRONJOB_EXPRESSION') as string) || | ||
'0 0 * * *'; | ||
|
||
const qfRoundUsersMissedMBDScore = Number( | ||
process.env.QF_ROUND_USERS_MISSED_SCORE || 0 | ||
); | ||
|
||
const workerOptions = { | ||
concurrency: Number( | ||
process.env.USER_SCORE_SYNC_THREADS_POOL_CONCURRENCY || 1, | ||
), | ||
name: | ||
process.env.USER_SCORE_SYNC_THREADS_POOL_NAME || 'ProjectFiltersThreadPool', | ||
size: Number(process.env.USER_SCORE_SYNC_THREADS_POOL_SIZE || 4), | ||
}; | ||
|
||
export const runCheckPendingUserModelScoreCronjob = () => { | ||
logger.debug( | ||
'runCheckPendingUserModelScoreCronjob() has been called, cronJobTime', | ||
cronJobTime, | ||
); | ||
schedule(cronJobTime, async () => { | ||
await updateUsersWithoutMBDScoreInRound(); | ||
}); | ||
}; | ||
|
||
export const updateUsersWithoutMBDScoreInRound = async () => { | ||
const usersMDBScoreSyncThreadPool: Pool<ModuleThread<UserMDBScoreSyncWorker>> = | ||
Pool( | ||
() => spawn(new Worker('../workers/usersMBDScoreSyncWoker')), | ||
workerOptions, | ||
); | ||
|
||
const userIds = await findUsersWithoutMBDScoreInActiveAround(); | ||
const activeQfRoundId = ((await findActiveQfRound())?.id || qfRoundUsersMissedMBDScore); | ||
if (!activeQfRoundId || activeQfRoundId === 0) return; | ||
|
||
if (userIds.length === 0) return; | ||
|
||
for (const userId of userIds) { | ||
try { | ||
const userWallet = await findUserById(userId); | ||
const userScore = await usersMDBScoreSyncThreadPool.queue(worker => | ||
worker.syncUserScore({ | ||
userWallet, | ||
}), | ||
); | ||
if (userScore) { | ||
const userScoreInRound = UserQfRoundModelScore.create({ | ||
userId, | ||
qfRoundId: activeQfRoundId, | ||
score: userScore, | ||
}); | ||
|
||
await userScoreInRound.save(); | ||
} | ||
} catch (e) { | ||
logger.info(`User with Id ${userId} did not sync MBD score this batch`); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// workers/auth.js | ||
import { expose } from 'threads/worker'; | ||
import { WorkerModule } from 'threads/dist/types/worker'; | ||
import { getGitcoinAdapter } from '../adapters/adaptersFactory'; | ||
|
||
type UsersMBDScoreSyncWorkerFunctions = | ||
| 'syncUserScore'; | ||
|
||
export type UserMDBScoreSyncWorker = | ||
WorkerModule<UsersMBDScoreSyncWorkerFunctions>; | ||
|
||
const worker: UserMDBScoreSyncWorker = { | ||
async syncUserScore(args: { | ||
userWallet: string; | ||
}) { | ||
return await getGitcoinAdapter().getUserAnalysisScore(args.userWallet); | ||
}, | ||
}; | ||
|
||
expose(worker); |