Skip to content

Commit 897a0a8

Browse files
mirgeePatrik-Stas
andauthored
Periodically prune reviewed and rejected messages (#141)
* Prune processed msgs Signed-off-by: Miroslav Kovar <[email protected]> Co-authored-by: Patrik Stas <[email protected]>
1 parent b29935f commit 897a0a8

File tree

6 files changed

+104
-16
lines changed

6 files changed

+104
-16
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var dbm;
4+
var type;
5+
var seed;
6+
var fs = require('fs');
7+
var path = require('path');
8+
var Promise;
9+
10+
/**
11+
* We receive the dbmigrate dependency from dbmigrate initially.
12+
* This enables us to not have to rely on NODE_PATH.
13+
*/
14+
exports.setup = function(options, seedLink) {
15+
dbm = options.dbmigrate;
16+
type = dbm.dataType;
17+
seed = seedLink;
18+
Promise = options.Promise;
19+
};
20+
21+
exports.up = function(db) {
22+
var filePath = path.join(__dirname, 'sqls', '20220210204408-create-events-up.sql');
23+
return new Promise( function( resolve, reject ) {
24+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
25+
if (err) return reject(err);
26+
console.log('received data: ' + data);
27+
28+
resolve(data);
29+
});
30+
})
31+
.then(function(data) {
32+
return db.runSql(data);
33+
});
34+
};
35+
36+
exports.down = function(db) {
37+
var filePath = path.join(__dirname, 'sqls', '20220210204408-create-events-down.sql');
38+
return new Promise( function( resolve, reject ) {
39+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
40+
if (err) return reject(err);
41+
console.log('received data: ' + data);
42+
43+
resolve(data);
44+
});
45+
})
46+
.then(function(data) {
47+
return db.runSql(data);
48+
});
49+
};
50+
51+
exports._meta = {
52+
"version": 1
53+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Replace with your SQL commands */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE PROCEDURE prune_msgs()
2+
BEGIN
3+
DELETE LOW_PRIORITY FROM messages WHERE status_code = 'MS-105' OR status_code = 'MS-106';
4+
END;
5+
6+
7+
CREATE EVENT msgs_cleanup
8+
ON SCHEDULE EVERY 10 MINUTE
9+
ON COMPLETION PRESERVE
10+
COMMENT 'Cleanup processed messages (in states MS-105 or MS-106)'
11+
DO
12+
CALL prune_msgs();

dbutils/src/db-schemas.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ async function _runSingleCmd(dbConfig, cmd) {
2020
}
2121
}
2222

23-
async function runSingleCmd(user, password, host, port, cmd) {
23+
async function runSingleCmd(user, password, host, port, cmd, database) {
2424
const dbConfig = {
2525
host,
2626
port,
2727
user,
2828
password,
29+
database,
2930
multipleStatements: false
3031
}
3132
await _runSingleCmd(dbConfig, cmd)
@@ -110,4 +111,5 @@ module.exports = {
110111
createMysqlDatabase,
111112
assureMysqlDatabase,
112113
enableConsoleLogging,
114+
runSingleCmd
113115
}

dbutils/src/db-testutils.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818

1919
const uuid = require('uuid')
2020
const { migrateSchemaData, migrateSchemaWallet } = require('./migration')
21-
const { dropMysqlDatabase, createMysqlDatabase } = require('./db-schemas')
21+
const { dropMysqlDatabase, createMysqlDatabase, runSingleCmd } = require('./db-schemas')
22+
23+
const user = 'root'
24+
const password = 'mysecretpassword'
25+
const host = 'localhost'
26+
const port = 3306
2227

2328
async function createDbSchemaApplication (dbNameId) {
2429
const dbId = dbNameId || uuid.v4().split('-').join("")
2530
const database = `agencytest_data_${dbId}`
26-
const user = 'root'
27-
const password = 'mysecretpassword'
28-
const host = 'localhost'
29-
const port = 3306
3031

3132
await createMysqlDatabase(user, password, host, port, database)
3233
await migrateSchemaData(user, password, host, port, database)
@@ -45,10 +46,6 @@ async function createDbSchemaApplication (dbNameId) {
4546
async function createDbSchemaWallets (dbNameId) {
4647
const dbId = dbNameId || uuid.v4().split('-').join("")
4748
const database = `agencytest_wallet_${dbId}`
48-
const user = 'root'
49-
const password = 'mysecretpassword'
50-
const host = 'localhost'
51-
const port = 3306
5249

5350
await createMysqlDatabase(user, password, host, port, database)
5451
await migrateSchemaWallet(user, password, host, port, database)
@@ -64,7 +61,13 @@ async function createDbSchemaWallets (dbNameId) {
6461
}
6562
}
6663

64+
async function pruneMsgs (dbName) {
65+
const cmd = 'CALL prune_msgs()'
66+
await runSingleCmd(user, password, host, port, cmd, dbName)
67+
}
68+
6769
module.exports = {
6870
createDbSchemaApplication,
69-
createDbSchemaWallets
71+
createDbSchemaWallets,
72+
pruneMsgs
7073
}

vcxagency-node/test/unit/messaging/agent-msgs.spec.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const uuid = require('uuid')
3434
const rimraf = require('rimraf')
3535
const os = require('os')
3636
const { getBaseAppConfig } = require('./common')
37-
const { createDbSchemaApplication, createDbSchemaWallets } = require('dbutils')
37+
const { createDbSchemaApplication, createDbSchemaWallets, pruneMsgs } = require('dbutils')
3838
const { setupVcxLogging } = require('../../utils')
3939
const { buildApplication, cleanUpApplication } = require('../../../src/setup/app')
4040
const { buildAgencyClientVirtual } = require('./common')
@@ -369,24 +369,41 @@ describe('onboarding', () => {
369369
expect(updated2.uids.length).toBe(1)
370370
expect(updated2.uids.includes(msg6Id)).toBeTruthy()
371371

372-
const msgReply = await vcxFlowGetMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, [aconn1UserPwDid, aconn2UserPwDid], [], [])
373-
const { msgsByConns } = msgReply
372+
let msgReply = await vcxFlowGetMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, [aconn1UserPwDid, aconn2UserPwDid], [], [])
373+
let { msgsByConns } = msgReply
374374
expect(Array.isArray(msgsByConns)).toBeTruthy()
375375
expect(msgsByConns.length).toBe(2)
376376

377-
const msgsByConn1 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn1UserPwDid)
377+
let msgsByConn1 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn1UserPwDid)
378378
expect(msgsByConn1).toBeDefined()
379379
expect(msgsByConn1.msgs.length).toBe(3)
380380
assertMsgsByConHasMessageWithStatus(msgsByConn1, msg1Id, 'MS-106')
381381
assertMsgsByConHasMessageWithStatus(msgsByConn1, msg2Id, 'MS-106')
382382
assertMsgsByConHasMessageWithStatus(msgsByConn1, msg3Id, 'MS-104')
383383

384-
const msgsByConn2 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn2UserPwDid)
384+
let msgsByConn2 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn2UserPwDid)
385385
expect(msgsByConn2).toBeDefined()
386386
expect(msgsByConn2.msgs.length).toBe(3)
387387
assertMsgsByConHasMessageWithStatus(msgsByConn2, msg4Id, 'MS-103')
388388
assertMsgsByConHasMessageWithStatus(msgsByConn2, msg5Id, 'MS-103')
389389
assertMsgsByConHasMessageWithStatus(msgsByConn2, msg6Id, 'MS-106')
390+
391+
await pruneMsgs(tmpDbData.info.database)
392+
msgReply = await vcxFlowGetMsgsFromAgent(agencyUserWh, sendToAgency, agent1Did, agent1Verkey, agencyUserVerkey, [aconn1UserPwDid, aconn2UserPwDid], [], []);
393+
({ msgsByConns } = msgReply)
394+
expect(Array.isArray(msgsByConns)).toBeTruthy()
395+
expect(msgsByConns.length).toBe(2)
396+
397+
msgsByConn1 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn1UserPwDid)
398+
expect(msgsByConn1).toBeDefined()
399+
expect(msgsByConn1.msgs.length).toBe(1)
400+
assertMsgsByConHasMessageWithStatus(msgsByConn1, msg3Id, 'MS-104')
401+
402+
msgsByConn2 = msgsByConns.find(msgsByConn => msgsByConn.pairwiseDID === aconn2UserPwDid)
403+
expect(msgsByConn2).toBeDefined()
404+
expect(msgsByConn2.msgs.length).toBe(2)
405+
assertMsgsByConHasMessageWithStatus(msgsByConn2, msg4Id, 'MS-103')
406+
assertMsgsByConHasMessageWithStatus(msgsByConn2, msg5Id, 'MS-103')
390407
})
391408

392409
it('should not update any message statusCode of no uids are specified in update request', async () => {

0 commit comments

Comments
 (0)