# External script to query MariaDB sometime return an error

**URL:** <https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441>\
**Category:** Uncategorized\
**Created:** [January 18, 2023, 7:15pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441 "2023-01-18T19:15:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike99](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mike99](https://forum.genieacs.com/u/mike99)\
**Post date:** [January 18, 2023, 7:15pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441/1 "2023-01-18T19:15:20Z")

</div>

HI all,

I’m using 1.2.8 and I have an external script that query MySQL to return specific information about wifi (SSID, password) or voip (sip user, sip password).

The problem, is that it work maybe 1/3 of the times, jugging from trying to summon back the device after every error return, and the 2/3 of the times, it return the following error.

```auto
name: Error
message: |-
  Cannot execute new commands: connection closed
  sql: select * from genieacs.voip where serial = ? - parameters:['xxxx12345678']
stack: |-
  Error: Cannot execute new commands: connection closed
  sql: select * from genieacs.voip where serial = ? - parameters:['xxxx12345678']
      at Object.module.exports.createError (/opt/genieacs/ext/node_modules/mariadb/lib/misc/errors.js:61:10)
      at Query.throwNewError (/opt/genieacs/ext/node_modules/mariadb/lib/cmd/command.js:63:16)
      at _addCommandDisabled (/opt/genieacs/ext/node_modules/mariadb/lib/connection.js:1265:9)
      at ConnectionCallback.Connection._queryCallback (/opt/genieacs/ext/node_modules/mariadb/lib/connection.js:579:5)
      at Object.getVoip (/opt/genieacs/ext/voip.js:11:8)

```

It don’t seems to be a problem related to the DB since, I’ve done a second node script for debug purpose that call the same “external” node script than GACS (voip.js or wifi.js) to pass the search parameter and print the output from the db in objects / values path format. By exemple:

```auto
$node test.js xxxx12345678 getVoip
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.Enable = Enabled
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthUserName = sipuser1
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthPassword = passw1
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.2.Enable = Enabled
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.2.SIP.AuthUserName = sipuser2
InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.2.SIP.AuthPassword = passw2

```

And, if I use this script back to back several times in a row, no matter how many time, it seems to always work without any issue. I read somewhere that if a connection is already open, I have to wait for it to close but I’m not sure of how to do it in a GACS provisioning script.

Can anybody help ?

Thanks

---

<div class="post-metadata">

**Author:** ![rudymartin](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rudymartin](https://forum.genieacs.com/u/rudymartin)\
**Post date:** [January 18, 2023, 7:22pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441/2 "2023-01-18T19:22:01Z")

</div>

> [@Extension timeout even with callback](https://forum.genieacs.com/t/extension-timeout-even-with-callback/2787/2):
>
> I know that I am not answering your exact question, might be unrelated to your problem, and others might disagree with me on this, but I had the same problem about extensions time-out, and ended up using a reverse approach. Instead of going out to the network to ask for information, it is our restapi server which goes to the ACS, using a web page as middle man, which in turn creates a JSON text file on filesystem using the serial number as filename and the content passed by the post as is. Eac…

---

<div class="post-metadata">

**Author:** ![mike99](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mike99](https://forum.genieacs.com/u/mike99)\
**Post date:** [January 18, 2023, 8:24pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441/3 "2023-01-18T20:24:22Z")

</div>

So, if I understand well, instead of doing a query to the DB, you write a JSON text file directly on the GACS server and query this text file instead. Right ?

---

<div class="post-metadata">

**Author:** ![rudymartin](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rudymartin](https://forum.genieacs.com/u/rudymartin)\
**Post date:** [January 19, 2023, 12:47pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441/4 "2023-01-19T12:47:55Z")

</div>

yeah. But that’s my way of doing things. You simply read the file from the extension and pass the content to the provision. Something like this:

```auto
const fs = require( 'fs' );

function extension( myargs, callback ){
    var json = JSON.parse( myargs[0] );
    var file = "/data/acs/json/" + json["serialnum"] + ".json";
    fs.readFile( file, 'utf8', function( err, data ){
        if( err !== null ){
            console.log( "read file !" + err );
            return callback( null, err );
        }
        return callback( null, data );
    } );
}

```

---

<div class="post-metadata">

**Author:** ![rudymartin](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rudymartin](https://forum.genieacs.com/u/rudymartin)\
**Post date:** [January 19, 2023, 7:10pm UTC](https://forum.genieacs.com/t/external-script-to-query-mariadb-sometime-return-an-error/3441/5 "2023-01-19T19:10:20Z")

</div>

free sample:

```auto
root@acs3:/data/acs/json# cat 4857544388C33DA6.json
{
    "InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID": "37Sur.com-sample",
    "InternetGatewayDevice.LANDevice.1.WLANConfiguration.5.SSID": "37Sur.com-sample-5GHz",
    "InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.KeyPassphrase": "samplePWD",
    "InternetGatewayDevice.LANDevice.1.WLANConfiguration.5.PreSharedKey.1.KeyPassphrase": "samplePWD"
}

```

this way the only thing you need to do is to `JSON.parse()` the content into a javascript object, then `Object.keys()` to get the key for each pair of the object.
