mirror of
https://github.com/SunRed/haste-server.git
synced 2025-09-05 21:10:16 +02:00
Replaced all callbacks with promises in document stores
This commit is contained in:
parent
c165781b18
commit
7de68695fe
4 changed files with 130 additions and 146 deletions
|
@ -10,7 +10,7 @@ class FileDocumentStore {
|
|||
}
|
||||
|
||||
//save data in a file, key as md5 - since we don't know what we can passed here
|
||||
async set(key, data, callback, skipExpire){
|
||||
async set(key, data, skipExpire){
|
||||
const _this = this;
|
||||
const filePath = this.getPath(key);
|
||||
|
||||
|
@ -23,35 +23,35 @@ class FileDocumentStore {
|
|||
}
|
||||
|
||||
winston.silly('set key', { type: 'file', filename: filePath });
|
||||
await fs.promises.writeFile(filePath, data, {mode: '600'})
|
||||
return await fs.promises.writeFile(filePath, data, {mode: '600'})
|
||||
.then(() => {
|
||||
callback(true);
|
||||
if (_this.expire && !skipExpire){
|
||||
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
||||
winston.warn('file store doesn\'t support expiration', { file: filePath });
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('error while writing document to file', { file: filePath, error: err });
|
||||
callback(false);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
//get data from a file
|
||||
async get(key, callback, skipExpire){
|
||||
async get(key, skipExpire){
|
||||
const _this = this;
|
||||
const filePath = this.getPath(key);
|
||||
|
||||
winston.silly('get key', { type: 'file', filename: filePath });
|
||||
await fs.promises.readFile(filePath, {encoding: 'utf8'})
|
||||
return await fs.promises.readFile(filePath, {encoding: 'utf8'})
|
||||
.then(data => {
|
||||
callback(data);
|
||||
if (_this.expire && !skipExpire){
|
||||
winston.warn('file store cannot set expirations on keys', { file: filePath });
|
||||
}
|
||||
return data;
|
||||
})
|
||||
.catch(err => {
|
||||
winston.debug('error while reading document', { file: filePath, error: err });
|
||||
return callback(false);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
const winston = require('winston');
|
||||
const mongodb = require('mongodb');
|
||||
|
||||
const MongoDocumentStore = function (config){
|
||||
this.expire = config.expire;
|
||||
this.MongoClient = new mongodb.MongoClient(config.connectionUri, config.clientOptions);
|
||||
};
|
||||
class MongoDocumentStore {
|
||||
constructor(config){
|
||||
this.expire = config.expire;
|
||||
this.MongoClient = new mongodb.MongoClient(config.connectionUri, config.clientOptions);
|
||||
}
|
||||
|
||||
MongoDocumentStore.prototype.set = async function (key, data, callback, skipExpire){
|
||||
winston.silly(`mongo set ${key}`);
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const that = this;
|
||||
async set(key, data, callback, skipExpire){
|
||||
winston.silly(`mongo set ${key}`);
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const that = this;
|
||||
|
||||
await this.safeConnect(async ( {error} = {} ) => {
|
||||
if (error) return callback(false);
|
||||
if ((await this.safeConnect()).error) return null;
|
||||
|
||||
await this.MongoClient.db().collection('entries').updateOne(
|
||||
return await this.MongoClient.db().collection('entries').updateOne(
|
||||
{
|
||||
'entry_id': key,
|
||||
$or: [
|
||||
|
@ -34,22 +34,19 @@ MongoDocumentStore.prototype.set = async function (key, data, callback, skipExpi
|
|||
}
|
||||
)
|
||||
.then((err, result) => {
|
||||
return callback(true);
|
||||
return true;
|
||||
})
|
||||
.catch((err, result) => {
|
||||
winston.error('error updating mongodb document', { error: err });
|
||||
return callback(false);
|
||||
return false;
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
async get(key, skipExpire){
|
||||
winston.silly(`mongo get ${key}`);
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const that = this;
|
||||
|
||||
MongoDocumentStore.prototype.get = async function (key, callback, skipExpire){
|
||||
winston.silly(`mongo get ${key}`);
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const that = this;
|
||||
|
||||
await this.safeConnect(async ( {error} = {} ) => {
|
||||
if (error) return callback(false);
|
||||
if ((await this.safeConnect()).error) return null;
|
||||
|
||||
let document = await this.MongoClient.db().collection('entries').findOne({
|
||||
'entry_id': key,
|
||||
|
@ -57,15 +54,12 @@ MongoDocumentStore.prototype.get = async function (key, callback, skipExpire){
|
|||
{ expiration: -1 },
|
||||
{ expiration: { $gt: now } }
|
||||
]
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('error finding mongodb document', { error: err });
|
||||
return callback(false);
|
||||
});
|
||||
}).catch(err => {
|
||||
winston.error('error finding mongodb document', { error: err });
|
||||
return null;
|
||||
});
|
||||
|
||||
callback(document ? document.value : false);
|
||||
|
||||
if (document && document.expiration != -1 && that.expire && !skipExpire){
|
||||
if (document && document.expiration != -1 && that.expire && !skipExpire) {
|
||||
await this.MongoClient.db().collection('entries').updateOne(
|
||||
{ 'entry_id': key },
|
||||
{ $set: { expiration: that.expire + now } }
|
||||
|
@ -74,22 +68,25 @@ MongoDocumentStore.prototype.get = async function (key, callback, skipExpire){
|
|||
});
|
||||
winston.silly('extended expiry of mongodb document', { key: key, timestamp: that.expire + now });
|
||||
}
|
||||
});
|
||||
};
|
||||
return document ? document.value : null;
|
||||
}
|
||||
safeConnect(){
|
||||
//don't try connecting again if already connected
|
||||
//https://jira.mongodb.org/browse/NODE-1868
|
||||
if (this.MongoClient.isConnected()) return { error: null };
|
||||
return this.MongoClient.connect()
|
||||
.then(client => {
|
||||
winston.debug('connected to mongodb', { success: true });
|
||||
return { error: null };
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('error connecting to mongodb', { error: err });
|
||||
return { error: err };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
MongoDocumentStore.prototype.safeConnect = function(cb){
|
||||
//don't try connecting again if already connected
|
||||
//https://jira.mongodb.org/browse/NODE-1868
|
||||
if (this.MongoClient.isConnected()) return cb({error: null});
|
||||
this.MongoClient.connect()
|
||||
.then(client => {
|
||||
winston.debug('connected to mongodb', { success: true });
|
||||
cb({error: null});
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('error connecting to mongodb', { error: err });
|
||||
cb({error: err});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = MongoDocumentStore;
|
|
@ -18,30 +18,34 @@ class RedisDocumentStore {
|
|||
winston.info('initialized redis client');
|
||||
}
|
||||
|
||||
async set(key, data, callback, skipExpire){
|
||||
await this.client.set(key, data).catch(err => {
|
||||
winston.error('failed to call redisClient.set', {error: err});
|
||||
callback(false);
|
||||
return;
|
||||
});
|
||||
if (!skipExpire) this.setExpiration(key);
|
||||
callback(true);
|
||||
async set(key, data, skipExpire){
|
||||
return await this.client.set(key, data)
|
||||
.then(() => {
|
||||
if (!skipExpire) this.setExpiration(key);
|
||||
return true;
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('failed to set redis document', {error: err});
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
async get(key, callback, skipExpire){
|
||||
let data = await this.client.get(key).catch(err => {
|
||||
winston.error('failed to get document from redis', {key: key, error: err});
|
||||
callback(false);
|
||||
return;
|
||||
});
|
||||
if (!skipExpire) this.setExpiration(key);
|
||||
callback(data);
|
||||
async get(key, skipExpire){
|
||||
return await this.client.get(key)
|
||||
.then(data => {
|
||||
if (!skipExpire) this.setExpiration(key);
|
||||
return data;
|
||||
})
|
||||
.catch(err => {
|
||||
winston.error('failed to get redis document', {key: key, error: err});
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
async setExpiration(key){
|
||||
if (!this.expire) return;
|
||||
await this.client.expire(key, this.expire).catch(err => {
|
||||
winston.warn('failed to set expiry on key', {key: key, error: err});
|
||||
winston.warn('failed to set redis key expiry', {key: key, error: err});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue