-
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.
Hotfix automatic model score sync (#1849)
* add user mbdscore sync workers and cronjob * add active env var for syncing score * add tests to the user sync worker and cronjob
- Loading branch information
Showing
7 changed files
with
257 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
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,85 @@ | ||
import { assert } from 'chai'; | ||
import moment from 'moment'; | ||
import { | ||
createDonationData, | ||
createProjectData, | ||
generateRandomEtheriumAddress, | ||
saveDonationDirectlyToDb, | ||
saveProjectDirectlyToDb, | ||
saveUserDirectlyToDb, | ||
} from '../../../test/testUtils'; | ||
import { QfRound } from '../../entities/qfRound'; | ||
import { updateUsersWithoutMBDScoreInRound } from './syncUsersModelScore'; | ||
import { UserQfRoundModelScore } from '../../entities/userQfRoundModelScore'; | ||
|
||
describe( | ||
'updateUsersWithoutMBDScoreInRound() test cases', | ||
updateUsersWithoutMBDScoreInRoundTestCases, | ||
); | ||
|
||
function updateUsersWithoutMBDScoreInRoundTestCases() { | ||
// for tests it return 1, useful to test cronjob logic and worker | ||
it('should save the score for users that donated in the round', async () => { | ||
await QfRound.update({}, { isActive: false }); | ||
const qfRound = QfRound.create({ | ||
isActive: true, | ||
name: 'test', | ||
allocatedFund: 100, | ||
minimumPassportScore: 8, | ||
slug: new Date().getTime().toString(), | ||
beginDate: new Date(), | ||
endDate: moment().add(10, 'days').toDate(), | ||
}); | ||
await qfRound.save(); | ||
const project = await saveProjectDirectlyToDb(createProjectData()); | ||
project.qfRounds = [qfRound]; | ||
await project.save(); | ||
|
||
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); | ||
const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); | ||
await saveDonationDirectlyToDb( | ||
{ | ||
...createDonationData(), | ||
segmentNotified: false, | ||
qfRoundId: qfRound.id, | ||
status: 'verified', | ||
}, | ||
user.id, | ||
project.id, | ||
); | ||
|
||
await saveDonationDirectlyToDb( | ||
{ | ||
...createDonationData(), | ||
segmentNotified: false, | ||
qfRoundId: qfRound.id, | ||
status: 'verified', | ||
}, | ||
user2.id, | ||
project.id, | ||
); | ||
|
||
await updateUsersWithoutMBDScoreInRound(); | ||
|
||
const user1ModelScore = await UserQfRoundModelScore.createQueryBuilder( | ||
'score', | ||
) | ||
.where('score."userId" = :userId', { userId: user.id }) | ||
.andWhere('score."qfRoundId" = :qfRoundId', { qfRoundId: qfRound.id }) | ||
.getOne(); | ||
|
||
const user2ModelScore = await UserQfRoundModelScore.createQueryBuilder( | ||
'score', | ||
) | ||
.where('score."userId" = :userId', { userId: user2.id }) | ||
.andWhere('score."qfRoundId" = :qfRoundId', { qfRoundId: qfRound.id }) | ||
.getOne(); | ||
|
||
// base values for mocks | ||
assert.equal(user1ModelScore?.score, 1); | ||
assert.equal(user2ModelScore?.score, 1); | ||
|
||
qfRound.isActive = false; | ||
await qfRound.save(); | ||
}); | ||
} |
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,63 @@ | ||
import { schedule } from 'node-cron'; | ||
import { spawn, Worker, Thread } from 'threads'; | ||
import config from '../../config'; | ||
import { logger } from '../../utils/logger'; | ||
import { | ||
findActiveQfRound, | ||
findUsersWithoutMBDScoreInActiveAround, | ||
} from '../../repositories/qfRoundRepository'; | ||
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, | ||
); | ||
|
||
export const runCheckPendingUserModelScoreCronjob = () => { | ||
logger.debug( | ||
'runCheckPendingUserModelScoreCronjob() has been called, cronJobTime', | ||
cronJobTime, | ||
); | ||
schedule(cronJobTime, async () => { | ||
await updateUsersWithoutMBDScoreInRound(); | ||
}); | ||
}; | ||
|
||
export const updateUsersWithoutMBDScoreInRound = async () => { | ||
const worker = await spawn( | ||
new Worker('../../workers/userMBDScoreSyncWorker'), | ||
); | ||
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 user = await findUserById(userId); | ||
if (!user) continue; | ||
|
||
const userScore = await worker.syncUserScore({ | ||
userWallet: user?.walletAddress, | ||
}); | ||
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`); | ||
} | ||
} | ||
await Thread.terminate(worker); | ||
}; |
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,17 @@ | ||
// 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 UserMBDScoreSyncWorker = | ||
WorkerModule<UsersMBDScoreSyncWorkerFunctions>; | ||
|
||
const worker: UserMBDScoreSyncWorker = { | ||
async syncUserScore(args: { userWallet: string }) { | ||
return await getGitcoinAdapter().getUserAnalysisScore(args.userWallet); | ||
}, | ||
}; | ||
|
||
expose(worker); |