again improve debug output
This commit is contained in:
22
dist/mongosave.js
vendored
22
dist/mongosave.js
vendored
@ -12,42 +12,42 @@ class MongoSave extends CallChain.AAsyncBaseChainItem {
|
|||||||
}
|
}
|
||||||
func(message, finished) {
|
func(message, finished) {
|
||||||
if (!this.dbh) {
|
if (!this.dbh) {
|
||||||
log.info("Not database connection yet");
|
log.info("MongoSave: Not database connection yet");
|
||||||
if (!this.connectPending) {
|
if (!this.connectPending) {
|
||||||
this.connectPending = true;
|
this.connectPending = true;
|
||||||
this.mongoClient.connect(this.url)
|
this.mongoClient.connect(this.url)
|
||||||
.then((db) => {
|
.then((db) => {
|
||||||
log.info("Successfully opened MongoDB connect");
|
log.info("MongoSave: Successfully opened MongoDB connect");
|
||||||
this.dbh = db;
|
this.dbh = db;
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.error(`Failure when opening MongoDB connect: ${err}`);
|
log.error(`MongoSave: Failure when opening MongoDB connect: ${err}`);
|
||||||
this.dbh = undefined;
|
this.dbh = undefined;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log.info("Connecting to database is pending");
|
log.info("MongoSave: Connecting to database is pending");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.dbh) {
|
if (this.dbh) {
|
||||||
log.info("Database handle is available");
|
log.info("MongoSave: Database handle is available");
|
||||||
let coll = this.dbh.collection("iot");
|
let coll = this.dbh.collection("iot");
|
||||||
coll.insertOne(message)
|
coll.insertOne(message)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
log.info(`Successfully wrote one item in database: ${res.insertedId}`);
|
log.info(`MongoSave: Successfully wrote one item in database: ${res.insertedId}`);
|
||||||
let nextValue = { id: res.insertedId, payload: message };
|
let nextValue = { id: res.insertedId, payload: message };
|
||||||
finished(nextValue);
|
finished(nextValue);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.error(`Failure when trying to write one item in database: ${err}`);
|
log.error(`MongoSave: Failure when trying to write one item in database: ${err}`);
|
||||||
log.error("Chain interrupted");
|
log.error("MongoSave: Chain interrupted");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
log.error(`No database connection yet, drop message ${message}`);
|
log.error(`MongoSave: No database connection yet, drop message ${message}`);
|
||||||
log.error("Chain interrupted");
|
log.error("MongoSave: Chain interrupted");
|
||||||
}
|
}
|
||||||
log.info(`Returning from ${this.label}`);
|
log.info(`MongoSave: Returning from ${this.label}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.MongoSave = MongoSave;
|
exports.MongoSave = MongoSave;
|
||||||
|
@ -23,57 +23,39 @@ export class MongoSave extends CallChain.AAsyncBaseChainItem {
|
|||||||
|
|
||||||
protected func(message : any, finished : (message : any) => void) : void {
|
protected func(message : any, finished : (message : any) => void) : void {
|
||||||
if (! this.dbh) {
|
if (! this.dbh) {
|
||||||
log.info("Not database connection yet")
|
log.info("MongoSave: Not database connection yet")
|
||||||
if (! this.connectPending) {
|
if (! this.connectPending) {
|
||||||
this.connectPending = true
|
this.connectPending = true
|
||||||
this.mongoClient.connect(this.url)
|
this.mongoClient.connect(this.url)
|
||||||
.then((db:MongoDB.Db) => {
|
.then((db:MongoDB.Db) => {
|
||||||
log.info("Successfully opened MongoDB connect")
|
log.info("MongoSave: Successfully opened MongoDB connect")
|
||||||
this.dbh = db
|
this.dbh = db
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
log.error(`Failure when opening MongoDB connect: ${err}`)
|
log.error(`MongoSave: Failure when opening MongoDB connect: ${err}`)
|
||||||
this.dbh = undefined
|
this.dbh = undefined
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
log.info("Connecting to database is pending")
|
log.info("MongoSave: Connecting to database is pending")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.dbh) {
|
if (this.dbh) {
|
||||||
log.info("Database handle is available")
|
log.info("MongoSave: Database handle is available")
|
||||||
let coll : MongoDB.Collection = this.dbh.collection("iot")
|
let coll : MongoDB.Collection = this.dbh.collection("iot")
|
||||||
coll.insertOne(message)
|
coll.insertOne(message)
|
||||||
.then((res : MongoDB.InsertOneWriteOpResult) => {
|
.then((res : MongoDB.InsertOneWriteOpResult) => {
|
||||||
log.info(`Successfully wrote one item in database: ${res.insertedId}`)
|
log.info(`MongoSave: Successfully wrote one item in database: ${res.insertedId}`)
|
||||||
let nextValue = <MongoItem>{id: res.insertedId, payload: message}
|
let nextValue = <MongoItem>{id: res.insertedId, payload: message}
|
||||||
finished(nextValue)
|
finished(nextValue)
|
||||||
})
|
})
|
||||||
.catch((err : any) => {
|
.catch((err : any) => {
|
||||||
log.error(`Failure when trying to write one item in database: ${err}`)
|
log.error(`MongoSave: Failure when trying to write one item in database: ${err}`)
|
||||||
log.error("Chain interrupted")
|
log.error("MongoSave: Chain interrupted")
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
log.error(`No database connection yet, drop message ${message}`)
|
log.error(`MongoSave: No database connection yet, drop message ${message}`)
|
||||||
log.error("Chain interrupted")
|
log.error("MongoSave: Chain interrupted")
|
||||||
}
|
}
|
||||||
log.info(`Returning from ${this.label}`)
|
log.info(`MongoSave: Returning from ${this.label}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
public begin() :void {
|
|
||||||
if (this._next != null) {
|
|
||||||
this._next.begin()
|
|
||||||
}
|
|
||||||
this.addListener('yourturn', (message : any) : void => {
|
|
||||||
log.info(`Calling ${this.toString()}`)
|
|
||||||
let result : any = this.func(message)
|
|
||||||
if (this._next == null) {
|
|
||||||
log.info(`Last chain item, final result ${result}`)
|
|
||||||
} else {
|
|
||||||
this._next.send(result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user