0

I have a function:

function update_log_file(){
    return new Promise(function(resolve, reject){

        setInterval(function(){
            let logFile = '\\\\network\\sharedfolder\\GG\\' + moment(Date.now()).format('YYMMDD') + 'data_log.txt';

                let log_with_batch = {
                    log: logFile,
                    batch: 'new'
                }

                resolve(log_with_batch);

        }, 2000);


    });
}

and the invoker:

    update_log_file().then(function(log_with_batch){
       console.log(log_with_batch);
       tail = new Tail(log_with_batch.log);
       tail.on('line', function(data){
        console.log(data)
       });

       tail.error('error', function(error){
        console.log(error)
       });
    });

How am I able to see the object "log_with_batch" every 2 secs using promises and setInterval?

Please feel free to correct my functions. Thank you!

xtranghero
  • 89
  • 1
  • 12
  • 2
    Why you want to use a promise for this? whith setInterval it will work. Promises resolves just one time. – jonhid Mar 14 '18 at 06:15
  • @jonhid: Because I have a tail [node-tail](https://github.com/lucagrulla/node-tail) (Apology for not including in the code) If I use node-tail inside the setInterval the listener will multiply over time. – xtranghero Mar 14 '18 at 06:19
  • 2
    "*How to work with promises and `setInterval`*" - you don't. Promises resolve only once, an interval fires multiple times. And yes, your `node-tail` lib already fires multiple `line` events, there is absolutely no need to use `setInterval` here? Don't use it, don't use promise, just call `new Tail` and install your event listeners. – Bergi Mar 14 '18 at 06:32
  • @Bergi: Thanks for clarification. I appreciated it. I'm thinking if I just call `new Tail` I may not be able to change the filename (pathTofilename) according to the system date. – xtranghero Mar 14 '18 at 06:38
  • 2
    Oh that's what you're trying to do! I think for that you'll want to [watch the directory](https://stackoverflow.com/q/13695046/1048572) for new files appearing, or just check the date on every `line` event, and then open another `new Tail` on the new file. – Bergi Mar 14 '18 at 07:54
  • @Bergi: Thanks! I'll definitely try that. I'll get back to you once it's done – xtranghero Mar 15 '18 at 04:55
  • @Bergi: It works now! :D – xtranghero Mar 19 '18 at 08:09
  • @xtranghero You might want to [post your solution as an answer](https://stackoverflow.com/help/self-answer) – Bergi Mar 19 '18 at 10:25
  • @Bergi: Did an overnight testing, but the tailing stopped. I have to change the code first. Here's the code: `let pathToLog = '\\\\network\\shared\\p\\a\\t\\h\\moment(Date.now()).format('YYMMDD') + '_data_log.txt'; if(pathToLog){ tail = new Tail(pathToLog); } fs.watch(pathToLog, (eventType, filename) => { if('${eventType}' == 'rename'){ tail = new Tail(pathToLog); console.log(tail); } }); tail.on('line', function(data){ console.log(data); }); tail.on('error', function(error){ console.log(error); }); ` – xtranghero Mar 19 '18 at 23:06

1 Answers1

0

Check this, In update_log_file you don't need the promise. Promise resolves only once.

function tailExec() {
    let logFile = '\\\\network\\sharedfolder\\GG\\' + moment(Date.now()).format('YYMMDD') + 'data_log.txt';
    tail = new Tail(logFile );
    tail.on('line', function (data) {
        console.log(data)
    });
    tail.error('error', function (error) {
        console.log(error)
    });
}

setInterval(tailExec, 2000);
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37