Skip to content

Commit c4803e3

Browse files
committed
clear pending reward
1 parent 234ff50 commit c4803e3

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

src/db/cache/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,32 @@ function* getPendingClaimReward(model, username) {
367367
}
368368
}
369369

370+
function* clearPendingRewardCache(model, username) {
371+
if (env !== 'production') {
372+
log('clearPendingRewardCache', { msg: 'none_production' });
373+
return false;
374+
}
375+
376+
if (!username) {
377+
log('clearPendingRewardCache', { msg: 'username is empty.' });
378+
return false;
379+
}
380+
381+
const keyPrefix = model.getCachePrefix();
382+
const cacheKey = `${keyPrefix}${username}`;
383+
384+
let t1, t2, t3;
385+
try {
386+
if (env === 'production') {
387+
yield delAsync(cacheKey);
388+
}
389+
return true;
390+
} catch (e) {
391+
log('clearPendingRewardCache', { msg: e.message, cacheKey });
392+
return false;
393+
}
394+
}
395+
370396
module.exports = {
371397
getRecordCache,
372398
updateRecordCache,
@@ -376,4 +402,5 @@ module.exports = {
376402
updateRecordCache2,
377403
clearRecordCache2,
378404
getPendingClaimReward,
405+
clearPendingRewardCache,
379406
};

src/db/utils/user_utils.js

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable arrow-parens */
22
import config from 'config';
33
import models from 'db/models';
4-
import { updateRecordCache2 } from 'db/cache';
4+
import { clearPendingRewardCache } from 'db/cache';
55
import { Model } from 'sequelize';
66
import { log } from 'server/utils/loggers';
77

@@ -16,59 +16,24 @@ const clearPendingClaimTronReward = function*(username) {
1616
});
1717
t2 = process.uptime() * 1000;
1818
log('[timer] clearPendingClaimTronReward findOne', { t: t2 - t1 });
19-
if (user && user.getDataValue('pending_claim_tron_reward') > 0) {
20-
const pendingClaimTronReward =
21-
user.getDataValue('pending_claim_tron_reward') / 1e6;
22-
// transaction
19+
if (user) {
2320
t1 = process.uptime() * 1000;
24-
yield models.sequelize.transaction().then(transaction => {
25-
// clear pending_claim_tron_reward
26-
return user
27-
.update(
28-
{
29-
pending_claim_tron_reward: 0,
30-
},
31-
{
32-
transaction,
33-
}
34-
)
35-
.then(() =>
36-
models.TronReward.create(
37-
{
38-
username,
39-
tron_addr: user.get('tron_addr'),
40-
block_num: 0,
41-
steem_tx_id: '',
42-
reward_vests: `${pendingClaimTronReward *
43-
vestsPerTrx} VESTS`,
44-
reward_steem: '0 STEEM',
45-
reward_sbd: '0 SBD',
46-
vests_per_steem: 0,
47-
reward_type: 1,
48-
},
49-
{
50-
transaction,
51-
}
52-
)
53-
)
54-
.then(() => transaction.commit())
55-
.catch(err => {
56-
transaction.rollback();
57-
log('clear_pending_claim_tron_reward_error:', {
58-
msg: err.message,
59-
});
60-
});
61-
});
21+
yield models.TronReward.update(
22+
{
23+
tron_addr: user.tron_addr,
24+
},
25+
{
26+
where: {
27+
username,
28+
tron_addr: null,
29+
reward_type: 0,
30+
},
31+
}
32+
);
6233
t2 = process.uptime() * 1000;
34+
// clear redis cache
35+
yield clearPendingRewardCache(models.TronReward, username);
6336
log('[timer] clearPendingClaimTronReward transaction:', { t: t2 - t1 });
64-
t1 = process.uptime() * 1000;
65-
yield updateRecordCache2(
66-
models.TronUser,
67-
models.escAttrs({ username })
68-
);
69-
log('[timer] clearPendingClaimTronReward updateRecordCache2:', {
70-
t: process.uptime() * 1000 - t1,
71-
});
7237
}
7338
};
7439

0 commit comments

Comments
 (0)