Commit 173aca4a36993b7e6279f9c18d2ccdd6a313d8fb

Authored by Adhidarma Hadiwinoto
1 parent d600e067e9
Exists in master

Log redis history fetched

Showing 1 changed file with 1 additions and 0 deletions Inline Diff

1 'use strict'; 1 'use strict';
2 2
3 const redis = require('redis'); 3 const redis = require('redis');
4 const CircularBuffer = require("circular-buffer"); 4 const CircularBuffer = require("circular-buffer");
5 5
6 const config = require('komodo-sdk/config'); 6 const config = require('komodo-sdk/config');
7 const logger = require('komodo-sdk/logger'); 7 const logger = require('komodo-sdk/logger');
8 8
9 const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' }); 9 const redisClient = redis.createClient(config.redis || { host: '127.0.0.1' });
10 const history = new CircularBuffer(1000); 10 const history = new CircularBuffer(1000);
11 11
12 const REDIS_KEYWORD = `SHAKIR_SMS_HISTORY_${config.name||'SMS'}`; 12 const REDIS_KEYWORD = `SHAKIR_SMS_HISTORY_${config.name||'SMS'}`;
13 13
14 function fetchFromRedis() { 14 function fetchFromRedis() {
15 redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => { 15 redisClient.LRANGE(REDIS_KEYWORD, 0, -1, (err, reply) => {
16 reply.forEach((el) => { 16 reply.forEach((el) => {
17 try { 17 try {
18 history.push(JSON.parse(el)); 18 history.push(JSON.parse(el));
19 } catch (e) { 19 } catch (e) {
20 logger.warn(`Error parsing JSON on redis history. ${e.toString()}`); 20 logger.warn(`Error parsing JSON on redis history. ${e.toString()}`);
21 } 21 }
22 }); 22 });
23 logger.info(`History fetched from redis with ${reply.length} items.`);
23 }) 24 })
24 } 25 }
25 fetchFromRedis(); 26 fetchFromRedis();
26 27
27 function push(item) { 28 function push(item) {
28 redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item)); 29 redisClient.LPUSH(REDIS_KEYWORD, JSON.stringify(item));
29 history.push(item); 30 history.push(item);
30 } 31 }
31 32
32 function dump() { 33 function dump() {
33 const result = history.toarray(); 34 const result = history.toarray();
34 // console.log(JSON.stringify(result, null, 2)); 35 // console.log(JSON.stringify(result, null, 2));
35 return result; 36 return result;
36 } 37 }
37 38
38 exports.push = push; 39 exports.push = push;
39 exports.dump = dump; 40 exports.dump = dump;