Saturday 21 September 2019

Can any one help me with this Redis script?

I have a redis script that, in production, is run as a cron job. I did some edits, and am trying to test it before I send it back to my boss. I'm pretty sure the code is all correct, but the script doesn't seem to be working.Essentially, I am using redis to track server calls, and every so often these redis logs get saved to our Mongo database. When they are saved to Mongo, two things happen. 1) I loop through all redis keys and save them to Mongo. then I add a field to the redis file with a key of 'backUp' and a value of 'true' 2) I again loop through all the redis keys, and if there is a key of 'backUp' with a value of 'true' I delete that key.Because I am just trying to test this script without the whole app running, I've added a single redis document with 'client.hmset'. I've also added some log statements, and at the bottom I added some code that lets me run the file as a script.Here is the code:const db = require('../mongodb') const analyticsRepo = require('../app/repos/analytics') const ObjectID = require('mongodb').ObjectID let Binary = require('mongodb').Binary const redis = require('redis') const client = redis.createClient({ port: 6379 }) let start exports.start = start = function (cb) { console.log('start ()') client.hmset(['newHASH', 'route', 'routeValue', 'ipAddress', 'ipAddressValue'], function (err, res) { if (err) { console.log(err) } console.log(res) client.keys('*', function (err, keys) { // gets all hashes in redis db, returns them as 'keys' if (err) { console.log(err) } console.log('keys: ' + keys) for (let i = 0, len = keys.length; i < len; i++) { // loop through all keys client.hgetall(keys[i], function (err, results) { // gets all key/value pairs from keys[i], returns them as 'results' if (err) { console.log(err) } /* --inserts the object to be stored in Mongo db using the key/value pairs from 'results'-- */ const newLog = { route: results.route, ipAddress: Binary(results.ipAddress), // converts string to binary userId: new ObjectID(results.userId), // converts string to Mongo ObjectID timestamp: Date.parse(results.timestamp) // converts string to unix timestamp } analyticsRepo.create(newLog, function (err, insert) { if (err) { console.log(err) } }) // add backup field to keys[i] after saved to Mongo client.hset([keys[i], 'backup', 'true'], (err, backup) => { if (err) { console.log(err) } }) }) } }) // 2nd iteration of all hashes in redis client.keys('*', function (err, keys) { // gets all hashes in redis db, returns them as 'keys' if (err) { console.log(err) } for (let i = 0, len = keys.length; i < len; i++) { // delete the hash from redis client.hget([keys[i], 'backup'], (err, backupField) => { if (err) { console.log(err) } if (backupField === 'true') { client.del(keys[i], (err, success) => { if (err) { console.log(err) } console.log('record deleted') }) } }) } }) }) cb() } const redisToMongo = async function () { await db.init(function (err, results) { if (err) { console.error(err) return process.exit(1) } else { console.log('program begin') start(() => { process.exit() }) } }) } let args = process.argv[1] if (args === 'LOCAL PATH') { redisToMongo().then(() => { console.log('program run') }) } When I call the `redisToMongo` function at the bottom, I have a log statement that logs 'program run'. This log works correctly. Similarly, within the `redisToMongo` function I have a log statement that logs 'program begin'. This log works correctly. Similarly the first line of the `start` function, which has all the "real" code. has a log statement that logs 'star t()'. This log works, but nothing else seems to work. My client.hmset call should either log an error or success statement but neither on happens.Presumable if this were working, the redis document would be created, and then saved to my MongoDB, but this doesn't happen. And just to be sure, I tried calling a 'bad' redis command like 'hset123()'. This threw an error just like it should.How is it possible that I'm getting no errors, no log statements, and no successful additions to my database? Can someone please help me out here? Like I said, I'm pretty confident that the actual redis code is correct; I think I just don't know how to get this script to run or something...

Submitted September 21, 2019 at 08:02PM by Briyo2289

No comments:

Post a Comment