pipobscure / NodeJS-AsteriskManager

NodeJS Asterisk Manager API
Other
248 stars 103 forks source link

Start script when asterisk is unavailable #26

Closed ivahaev closed 10 years ago

ivahaev commented 10 years ago

Hi, I have an issue. Everything work good, but one thing I don't understand. How can I start app when asterisk is not available? When it happens, application finished with error:

Error: connect ECONNREFUSED at errnoException (net.js:904:11) at Object.afterConnect as oncomplete

I have tried to wrap initialization into try-catch, but it didn't help.

igorescobar commented 10 years ago

Share your code with us my friend.

Sent from my iPhone

On 25/09/2014, at 18:03, ivahaev notifications@github.com wrote:

Hi, I have an issue. Everything work good, but one thing I don't understand. How can I start app when asterisk is not available? When it happens, application finished with error:

Error: connect ECONNREFUSED at errnoException (net.js:904:11) at Object.afterConnect as oncomplete

I have tried to wrap initialization into try-catch, but it didn't help.

— Reply to this email directly or view it on GitHub.

ivahaev commented 10 years ago

Very simple code:

var config = require('../config'),
    AsteriskManager = require("asterisk-manager"),
    ami;

asteriskReconnect();

console.log("end file");

function asteriskConnect() {
    ami = new AsteriskManager(
        config.get('asterisk:port') || 5038,
        config.get('asterisk:host'),
        config.get('asterisk:username'),
        config.get('asterisk:secret'),
        true
    );

    ami.on('managerevent', allEventsListener);

    ami.on('close', function () {
        console.log('Connection with asterisk closed!');
        asteriskReconnect();
    });
}

function asteriskReconnect() {
    setTimeout(asteriskConnect(), 500);
}

function allEventsListener(evt) {
    console.log(evt);
}

And I try to change asteriskConnect function:

try {
    ami = new AsteriskManager(
        config.get('asterisk:port') || 5038,
        config.get('asterisk:host'),
        config.get('asterisk:username'),
        config.get('asterisk:secret'),
        true
    );
} catch (e) {
        console.log(e);
}       
igorescobar commented 10 years ago

It usually means that the port you're trying to use its already taken. Try to use a different port... or... Try to connect via telnet in yout config.get('asterisk:host') 5038 and see what happens.

ivahaev commented 10 years ago

AMI works with no problem. When asterisk is running, application connects well, but when I manually shutdown the asterisk, application falls with error. That is the problem. Application can't start when asterisk is not running. I think, it needs an extra argument when creating instance of asterisk-manager - callback function with possible connection error.

igorescobar commented 10 years ago

It is because you are not handling the exception. Every node app crashes when a no handled exception occurs.

[]'s Igor

On 28/09/2014, at 04:06, ivahaev notifications@github.com wrote:

AMI works with no problem. When asterisk is running, application connects well, but when I manually shutdown the asterisk, application falls with error. That is the problem. Application can't start when asterisk is not running;

— Reply to this email directly or view it on GitHub.

ivahaev commented 10 years ago

And I try to change asteriskConnect function:

try {
    ami = new AsteriskManager(
        config.get('asterisk:port') || 5038,
        config.get('asterisk:host'),
        config.get('asterisk:username'),
        config.get('asterisk:secret'),
        true
    );
} catch (e) {
        console.log(e);
}       

But app still crashes with error.

igorescobar commented 10 years ago

Yeah but I don't think that is going to work. Google a little bit more about more about the subject.

[]'s Igor

On 28/09/2014, at 11:05, ivahaev notifications@github.com wrote:

And I try to change asteriskConnect function:

try { ami = new AsteriskManager( config.get('asterisk:port') || 5038, config.get('asterisk:host'), config.get('asterisk:username'), config.get('asterisk:secret'), true ); } catch (e) { console.log(e); }
But app still crashes with error.

— Reply to this email directly or view it on GitHub.

wenhuancui commented 10 years ago

@ivahaev It's because try-catch cannot catch exceptions from asynchronous operations, which new Asteriskmanager() is performing. We have to use: process.on('uncaughtException', function (err) { console.error(err); });