mail-listener icon indicating copy to clipboard operation
mail-listener copied to clipboard

Add IMAP listeners on "close" and "error"

Open anotheri opened this issue 11 years ago • 10 comments

Add please two imap listeners and emits. Something like this:

After @emit "server:connected":

@imap.on "close", (err) =>
    # console.log "IMAP close"
    @emit "imap:close", err

@imap.on "error", (err) =>
    # console.log "IMAP error", err
    @emit "imap:err", err;

I think it may have another predefine handlers by default (stop mailListener, for example, or restart it, or something other). But i need opportunity to set handlers on this events from listeners for mail-listener class like this:

mailListener.on("imap:err", function(){
    console.log("imapError");
});

mailListener.on("imap:close", function(){
    mailListener.restart();
        // or something other code
});

anotheri avatar Jun 13 '13 10:06 anotheri

Mail listener already emit these events. See if below example makes sense to you.

mailListener.on("server:error", function(error){
                console.log('imap error')
});
mailListener.on("server:disconnected", function(){
      //  restart here
});

chirag04 avatar Jun 13 '13 10:06 chirag04

@chirag04 server:error and server:disconnected are not imap events, check the code. you can try to add this events and check it. imap is down after 1 hour of inactivity and only @imap.on "close", (err) => can catch it.

anotheri avatar Jun 13 '13 11:06 anotheri

@chirag04 this is actual for imap v. 0.7.8. I didn't test this with other versions of imap. but I'm testing now imap v.0.7.12.

anotheri avatar Jun 13 '13 11:06 anotheri

Ok. got it. I will leave this to @podviaznikov

chirag04 avatar Jun 13 '13 12:06 chirag04

@chirag04 It seems that fallings down after 1 hour of inactivity were fixed in imap. And that's great! It works already 1.5 hour. ) I'll leave my listener in active status for 24 hours and will describe how it works tomorrow.

@podviaznikov Could you update mail-listener in the npm please? Version based on imap 0.7.12 seems more stable.

anotheri avatar Jun 13 '13 13:06 anotheri

But... After ~2 hour I send a second mail, it was parsed correctly and listener working else few minute and than shutted down with unhandled error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:883:11)
    at TCP.onread (net.js:539:19)

And I think this error should be handled next way:

@imap.on "error", (err) =>
    # console.log "IMAP error", err
    @emit "imap:err", err;

And I started listener again...

anotheri avatar Jun 13 '13 13:06 anotheri

@anotheri till then you might want to start your script with forever. Shall give you some good sleep. Haha :)

chirag04 avatar Jun 13 '13 13:06 chirag04

@chirag04 thanks for realy good advice. ) but now i want test it without forever. this (forever) is my ace in the hole. =))

anotheri avatar Jun 13 '13 13:06 anotheri

@chirag04 And, as i sad yesterday, listener was shutted down with only output:

 --- IMAP CLOSE: false

that I define into this block:

@imap.on "close", (err) =>
    console.log " --- IMAP CLOSE:", err
    @emit "imap:close", err

In this case imap v. 0.7.12 works like previous. So add emit functions on imap close and error events please. And I'll use my ace — forever. =)

anotheri avatar Jun 14 '13 05:06 anotheri

Not sure if this is relevant but i tried to start mail-listener and kept it on server. Its still connected and running from last 5hours.

chirag04 avatar Jun 17 '13 13:06 chirag04