I have the following example code (on a node.js server) that should insert data into an sqlite table and then run a child process which copies the sqlite database file to another directory. The problem is that the copied version does not contain the newly inserted data. When I set a timeout before executing the command everything works but I would prefer to use a callback or event.
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('test.db');
const exec = require('child_process').exec;
db.serialize(function() {
var val = Date.now()/1000;
db.run("INSERT INTO test (val) VALUES (?);", [val]);
db.close();
exec('/bin/cp -rf /path0/test.db /path1/');
});
as the documentations says about close function :
Database#close([callback])
Closes the database.
callback (optional): If provided, this function will be called when the database was closed successfully or when an error occurred. The first > argument is an error object. When it is null, closing succeeded. If no callback is provided and an error occurred, an error event with the error object as the only parameter will be emitted on the database object. If closing succeeded, a close event with no parameters is emitted, regardless of whether a callback was provided or not.
you should be able to provide a callback to the close function to be run after the db is closed, if i understand your code well it should be something like this :
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('test.db');
const exec = require('child_process').exec;
db.serialize(function() {
var val = Date.now()/1000;
db.run("INSERT INTO test (val) VALUES (?);", [val]);
db.close(() => { exec('/bin/cp -rf /path0/test.db /path1/') });
});
reference is here!
Related
I have two Java scripts and i would like to merge them to get a output that can be treated as successful or failure. I'm not sure if this is the right direction or if i have got this completely wrong.
Initial script is to git clone the repos
const shell = require('shelljs')
const path = '/home/test/'
shell.cd(path)
shell.exec('git clone https://github.com/test/mic.git')
This is a java script and it does clone the repo.
node git.js and it simply clones the repos
Now I have another script which needs to get the result of the above script and pass it to a variable which then says if it is success of failure.
var body = response.getResponseBody();
var res = JSON.parse(body);
if (res.flag < 1){
api.setValue("flag", "failed");
}
Is there a way to integrate these scripts to get the right results.
All i want is if the first script will success/fail and get the status as a result which can be passed as a flag to another variable.
Any directions is really helpful
Shell.exec takes a callback.
If you have an error the code parameter in the callback should be a non-zero
shell.exec('git clone https://github.com/test/mic.git', (code, stdout, stderr) => {
if(code === 0) {
// No Error
} else {
// Had an error
}
})
In my JavaScript app I have a code like this that reads some data from a websocket and inserts it into MySQL databae using mysql2 module:
const mysql = require('mysql2/promise');
const db = await mysql.createConnection(params);
const ws = new WebSocket(someUrl);
ws.on('message', (data) =>
{
const t = JSON.parse(data);
//insert some row to MySQL table
db.execute("INSERT INTO table1 …);
});
periodically the following MySQL error occurs in this code:
Can't create more than max_prepared_stmt_count statements (current value: 16382)
My first understanding was that only one statement is executed at a time because there is only one thread in JavaScript and I use only one database connection, but looks like there is a little more complicated mechanism.
What does actually happen? Where multiple statements come from?
EDIT1:
Probably mysql2 caches the statements.
When I try to call Dexie on a database on which another call has been done by IndexedDB, there rises an error that the connection is already made to the database.
Can we pass an existing connection to Indexedb to Dexie?
This can be helpful when we want to use same connection in a Dexie object and another object and this happens to me when I try to add Dexie to my project. I don't want to rewrite the existing function.
Example:
function initDataBase(callback){
if(window.indexedDB){
var requeteBDD = window.indexedDB.open("databasename",1);
requeteBDD.onsuccess = function(){
if(typeof callback == "function")
callback(requeteBDD.result);
};
}
}
So can we do,for instance
initDataBase(function(db){
var dex = new Dexie(db);
});
I would like to use same connection as the first one.
Is it possible?
It's not possible as of current version to pass an instance of IDBDatabase into Dexie constructor. However, this would definitely be an easy pull request to do in the source, as Dexie already has the ability to open existing database by name and adapt to it's existing schema.
However, you should not get an error if you instanciate multiple IDBDatabases to same database name unless one of them tries to upgrade it using another version.
Dexie can open an existing database without creating the schema (even though you can only pass a name and not the db instance), as shown in the following fiddle: https://jsfiddle.net/dfahlander/b8Levamm/
new Dexie('MyDatabase').open().then(function (db) {
log ("Found database: " + db.name);
log ("Database version: " + db.verno);
db.tables.forEach(function (table) {
log ("Found table: " + table.name);
log ("Table Schema: " +
JSON.stringify(table.schema, null, 4));
});
}).catch('NoSuchDatabaseError', function(e) {
// Database with that name did not exist
log ("Database not found");
}).catch(function (e) {
log ("Oh uh: " + e);
});
(which fails because the given DB is not there. But if you create it on jsfiddle and run it again, you'll see it open).
I have created a Redis cluster with 30 instances (15 masters/ 15 nodes). With python code i connected to these instances, i found the masters and then i wanted to add some keys to them.
def settomasters(port, host):
r = redis.Redis( host=host, port=port )
r.set("key"+port,"value")
Error:
redis.exceptions.ResponseError: MOVED 12539 127.0.0.1:30012
If i try to set key from redis-cli -c -p portofmyinstance sometimes i get a redirection message that tells where the keys stored.
I know that in case of get requests for example, a smart client is needed in order to redirect the requests to the correct node (the node that holds the key) otherwise a moved error occurs. Is it the same situation? I need to catch the redis.exceptions.ResponseError and try to set again?
while True:
try:
r.set("key","value")
break
except:
print "error"
pass
My first try was above code but without solution. The set operation never succeeds.
On the other hand below code in javascript does not throw an error and i cannot figure the reason:
var redis = require('redis-stream'),
client = new redis(30001, '127.0.0.1');
// Open stream
var stream = client.stream();
// Example of setting 200 records
for(var record = 0; record <200; record++) {
var command = ['set', 'qwerty' + record, 'QWERTYUIOP'];
stream.redis.write( redis.parse(command) );
}
stream.on('close', function () {
console.log('Completed!');
});
// Close the stream after batch insert
stream.end();
Any help will be appreciated, thanks.
with a redis cluster you can use the normal redis client only if you "find for the certain key the slot that belongs and then the slots that each master serves. With this information i can set keys to the correct node without moved redirection errors." as #Antonis said. Otherwise you need http://redis-py-cluster.readthedocs.io/en/master/
I wrote a database population script (in JavaScript) so that I could easily run it on my local and on the server. I want to run it from the command line so that all I have to do is push it to the server/gear and execute it. The script runs fine on my local, but when I try to run it on the OpenShift gear it gives the following error:
MongoDB shell version: 2.4.9
connecting to: 127.XX.XXX.X:27017/admin
Sat Sep 12 12:20:25.979 count failed: { "ok" : 0, "errmsg" : "unauthorized" }
at src/mongo/shell/query.js:180
failed to load: ./Create.js
I'm trying to execute the command:
[xxxxxx-xxxxxx.rhcloud.com scripts]\> mongo ./Create.js
I have included the innards of my file. I left out all the document creation stuff. Just know I added document to an array.
// Switch to the DB we want
var connection;
try {
// Try to connect to local first
connection = new Mongo();
}
catch(err) {
// If error, connect to OpenShift server
connection = new Mongo('127.XX.XXX.X:27017');
}
db = connection.getDB('xxxxxxx');
// Create array to store documents
var newDocs = [];
...
// Documents are added to newDocs here
...
// If any new documents exist, insert them
if(newDocs.length) {
var bulk = db.xxxxxxx.initializeUnorderedBulkOp();
newDocs.forEach(function (doc) {
bulk.insert(doc);
});
bulk.execute();
print('NEW DOCS INSERTED');
}
else {
print('NO NEW DOCS');
}
Looks like you are missing your username/password to connect to your mongodb database on openshift? look at the error you got "unauthorized..."
I used db.auth('username', 'password'); to authenticate. Ultimately, I added a variable to the script to track if I was running the script locally or on the server. Then I used a variable for the password so that I wouldn't have the DB password just hanging out in the script. I pass the password in from the command line with the --eval option. Here is the altered head of the script:
// Switch to the DB we want
var connection;
var isLocal = true;
try {
// Try to connect to local first
connection = new Mongo();
}
catch(err) {
// If error, connect to OpenShift server
connection = new Mongo('127.XX.XXX.X:27017');
isLocal = false;
}
db = connection.getDB('xxxxxxx');
if(!isLocal) {
// If on server, authenticate
db.auth('username', password);
}
// Create array to store documents
var newDocs = [];
Then, to run the script from the command line on the server, I use the following:
mongo --eval "var password = 'xxxxxxx';" Create.js