Skip to content

Commit

Permalink
Cleanup logging within oada libs
Browse files Browse the repository at this point in the history
  • Loading branch information
awlayton committed Mar 8, 2021
1 parent 8ca9ab9 commit 754e8d0
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 47 deletions.
4 changes: 2 additions & 2 deletions oada/libs/oada-lib-arangodb/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var Bluebird = require('bluebird');
const Bluebird = require('bluebird');
const { Database } = require('arangojs');

const config = require('./config');
Expand All @@ -21,7 +21,7 @@ const DeadlockError = {
name: 'ArangoError',
errorNum: 29,
};
let query = db.query;
const query = db.query;
db.query = function (...args) {
let tries = 0;
function tryquery() {
Expand Down
13 changes: 8 additions & 5 deletions oada/libs/oada-lib-arangodb/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

'use strict';

const config = require('./config');
const Bluebird = require('bluebird');
const debug = require('debug');
const equal = require('deep-equal');

const config = require('./config');
const users = require('./libs/users.js');

const trace = debug('arango:init:trace');
const error = debug('arango:init:error');
const info = debug('arango:init:info');
const equal = require('deep-equal');
const users = require('./libs/users.js');
const Bluebird = require('bluebird');

// Can't use db.js's db because we're creating the actual database
const db = require('arangojs')({
Expand Down Expand Up @@ -197,7 +199,8 @@ module.exports = {
' exists on collection ' +
colname +
', and ensureDefaults is falsy, ' +
'so we are DELETING THIS DOCUMENT FROM THE DATABASE! Before deleting, its value in the database was: ',
'so we are DELETING THIS DOCUMENT FROM THE DATABASE! ' +
'Before deleting, its value in the database was: ',
JSON.stringify(dbdoc, false, ' ')
);
return db
Expand Down
2 changes: 1 addition & 1 deletion oada/libs/oada-lib-arangodb/libs/authorizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function save(token) {
t._key = t._id.replace(/^authorizations\//, '');
delete t._id;
}
trace('save: Replacing/Inserting token ', t);
trace('save: Replacing/Inserting token %s', t);
// overwrite will replace the given token if it already exists
return authorizations
.save(t, { overwrite: true })
Expand Down
13 changes: 8 additions & 5 deletions oada/libs/oada-lib-arangodb/libs/changes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

const db = require('../db');
const debug = require('debug');
const trace = debug('arangodb#resources:trace');
const { aql } = require('arangojs');
const pointer = require('json-pointer');
const debug = require('debug');

const db = require('../db');
const config = require('../config');

const trace = debug('arangodb#resources:trace');

const changes = db.collection(config.get('arangodb:collections:changes:name'));
const changeEdges = db.collection(
config.get('arangodb:collections:changeEdges:name')
Expand Down Expand Up @@ -148,7 +151,7 @@ function toChangeObj(arangoPathObj) {
let body = arangoPathObj.vertices[nVertices - 1].body;
let resource_id = arangoPathObj.vertices[nVertices - 1].resource_id;
// return change object
trace('toChangeObj: returning change object with body ', body);
trace('toChangeObj: returning change object with body %O', body);
return {
resource_id,
path,
Expand Down Expand Up @@ -191,7 +194,7 @@ function putChange({
throw new Error('children must be an array.');
}
let number = parseInt(rev, 10);
trace('putChange: inserting change with body ', change);
trace('putChange: inserting change with body %O', change);
return db
.query(
aql`
Expand Down
3 changes: 2 additions & 1 deletion oada/libs/oada-lib-arangodb/libs/codes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';

const { aql } = require('arangojs');

const config = require('../config');
const db = require('../db');
const { aql } = require('arangojs');
const util = require('../util');

const users = require('./users.js');
Expand Down
12 changes: 7 additions & 5 deletions oada/libs/oada-lib-arangodb/libs/remoteResources.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

const db = require('../db');
const debug = require('debug');
const trace = debug('arangodb#remoteResources:trace');
const { aql } = require('arangojs');
const debug = require('debug');

const db = require('../db');
const config = require('../config');

const trace = debug('arangodb#remoteResources:trace');

const remoteResources = db.collection(
config.get('arangodb:collections:remoteResources:name')
);
Expand All @@ -31,13 +33,13 @@ function getRemoteId(id, domain) {
`
)
.call('all')
.tap((rids) => trace('Found:', rids));
.tap((rids) => trace('Found: %O', rids));
}

function addRemoteId(rid, domain) {
let rids = Array.isArray(rid) ? rid : [rid];

trace('Adding remote IDs:', rids);
trace('Adding remote IDs: %O', rids);
return db.query(aql`
FOR rid IN ${rids}
INSERT {
Expand Down
17 changes: 10 additions & 7 deletions oada/libs/oada-lib-arangodb/libs/resources.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
'use strict';

const db = require('../db');
const debug = require('debug');
const info = debug('arangodb#resources:info');
const trace = debug('arangodb#resources:trace');
const cloneDeep = require('clone-deep');
const Bluebird = require('bluebird');
const { aql } = require('arangojs');
const debug = require('debug');
const cloneDeep = require('clone-deep');
const pointer = require('json-pointer');

const db = require('../db');
const config = require('../config');
const util = require('../util');
const users = require('./users');

const info = debug('arangodb#resources:info');
const trace = debug('arangodb#resources:trace');

const resources = db.collection(
config.get('arangodb:collections:resources:name')
);
Expand Down Expand Up @@ -250,7 +253,7 @@ function getResourceOwnerIdRev(id) {
)
.then((result) => result.next())
.then((obj) => {
trace('getResourceOwnerIdRev(' + id + '): result = ', obj);
trace('getResourceOwnerIdRev(%s): result = %O', id, obj);
return obj;
})
.catch(
Expand Down Expand Up @@ -357,7 +360,7 @@ function putResource(id, obj, checkLinks = true) {
return (checkLinks ? addLinks(obj) : Promise.resolve([])).then(
function docUpsert(links) {
info(`Upserting resource ${obj._key}`);
trace(`Upserting links: ${JSON.stringify(links, null, 2)}`);
trace('Upserting links: %O', links);

// TODO: Should it check that graphNodes exist but are wrong?
var q;
Expand Down
17 changes: 10 additions & 7 deletions oada/libs/oada-lib-arangodb/libs/users.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';

const debug = require('debug');
const info = debug('arangodb#resources:info');
const config = require('../config');
const db = require('../db.js');
const { aql } = require('arangojs');
const bcrypt = require('bcryptjs');
const Bluebird = require('bluebird');
const debug = require('debug');
const bcrypt = require('bcryptjs');
const flatten = require('flat');

const db = require('../db.js');
const config = require('../config');
const util = require('../util');

const info = debug('arangodb#resources:info');

const users = db.collection(config.get('arangodb:collections:users:name'));
const flatten = require('flat');

/*
user {
Expand Down Expand Up @@ -112,7 +115,7 @@ function findByUsernamePassword(username, password) {

function create(u) {
return Bluebird.try(() => {
info('create user was called with data', u);
info('create user was called with data %O', u);
if (u.password) u.password = hashPw(u.password);
// Throws if username already exists
return users.save(u, { returnNew: true }).then((r) => r.new || r);
Expand Down
3 changes: 2 additions & 1 deletion oada/libs/oada-lib-kafka/Requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

'use strict';

const EventEmitter = require('events');
const util = require('util');
const EventEmitter = require('events');

const ksuid = require('ksuid');
const Bluebird = require('bluebird');

Expand Down
1 change: 1 addition & 0 deletions oada/libs/oada-lib-kafka/Responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'use strict';

const util = require('util');

const ksuid = require('ksuid');
const Bluebird = require('bluebird');

Expand Down
28 changes: 15 additions & 13 deletions oada/libs/oada-lib-kafka/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
'use strict';

const process = require('process');

const EventEmitter = require('events');
var Promise = require('bluebird');

const Bluebird = require('bluebird');
const ksuid = require('ksuid');
const kf = require('node-rdkafka');

const config = require('./config');
const info = require('debug')('@oada/lib-kafka:info');
const error = require('debug')('@oada/lib-kafka:error');
Expand All @@ -34,6 +35,7 @@ const rdkafkaOpts = Object.assign(config.get('kafka:librdkafka'), {

const CONNECT = Symbol('kafka-lib-connect');
const DATA = Symbol('kafa-lib-data');

const pollInterval = 500;
const healthInterval = 5 * 60 * 1000;

Expand Down Expand Up @@ -83,7 +85,7 @@ class Base extends EventEmitter {
this.consumer.on('error', (...args) => super.emit('error', ...args));
this.producer.on('error', (...args) => super.emit('error', ...args));
this.producer.on('delivery-report', function (err, _report) {
if (err) error('!!!!!!!!!!!!!!!!!!!!!!!', err);
if (err) error('!!!!!!!!!!!!!!!!!!!!!!! %O', err);
});

this.consumer.on('event.error', (...args) =>
Expand All @@ -93,13 +95,13 @@ class Base extends EventEmitter {
super.emit('error', ...args)
);

let consumerReady = Promise.fromCallback((done) => {
const consumerReady = Bluebird.fromCallback((done) => {
this.consumer.on('ready', () => {
info(`${this.group}'s consumer ready`);
done();
});
});
let producerReady = Promise.fromCallback((done) => {
const producerReady = Bluebird.fromCallback((done) => {
this.producer.on('ready', () => {
this.producer.setPollInterval(
config.get('kafka:producer:pollInterval') || pollInterval
Expand All @@ -119,7 +121,7 @@ class Base extends EventEmitter {
done();
});
});
this.ready = Promise.join(consumerReady, producerReady);
this.ready = Bluebird.join(consumerReady, producerReady);

super.on('error', die);
}
Expand All @@ -135,15 +137,15 @@ class Base extends EventEmitter {
async [CONNECT]() {
// Assume all messages are JSON
this.consumer.on('data', ({ value, ...data }) => {
let resp = JSON.parse(value);
const resp = JSON.parse(value);
super.emit(DATA, resp, data);
});

this.consumer.connect();
this.producer.connect();
await this.ready;

let topics = Array.isArray(this.consumeTopic)
const topics = Array.isArray(this.consumeTopic)
? this.consumeTopic
: [this.consumeTopic];
this.consumer.subscribe(topics);
Expand All @@ -154,27 +156,27 @@ class Base extends EventEmitter {

async produce({ mesg, topic, part = null }) {
// Assume all messages are JSON
let payload = JSON.stringify({
const payload = JSON.stringify({
time: Date.now(),
group: this.group,
...mesg,
});
let value = Buffer.from(payload);
const value = Buffer.from(payload);

await this.ready;

return this.producer.produce(topic || this.produceTopic, part, value);
}

disconnect() {
let dcons = Promise.fromCallback((done) => {
const dcons = Bluebird.fromCallback((done) => {
this.consumer.disconnect(() => done());
});
let dprod = Promise.fromCallback((done) => {
const dprod = Bluebird.fromCallback((done) => {
this.producer.disconnect(() => done());
});

return Promise.join(dcons, dprod);
return Bluebird.join(dcons, dprod);
}
}

Expand Down

0 comments on commit 754e8d0

Please sign in to comment.