Skip to content

Instantly share code, notes, and snippets.

@freeart
Last active August 22, 2017 15:13
Show Gist options
  • Save freeart/c4af876110789cd9b08c52ca30281a14 to your computer and use it in GitHub Desktop.
Save freeart/c4af876110789cd9b08c52ca30281a14 to your computer and use it in GitHub Desktop.
const
async = require('async'),
SocialNetworkService = require('SocialNetworkService'),
Rabbitmq = require('rabbitmq-boilerplate'),
configStorage = require('needToMake');
class Template {
constructor() {
//define variables
this.config = {
accountQueue: "template_task-account",
postQueue: "template_task-post",
storePostQueue: "template_store-post",
storeCommentQueue: "template_store-comment"
}
this.snsConnect = null;
this.rabbitConnect = null;
//prepair and start
this.prepair()
.then(() => this.start())
.catch((err) => console.error(err))
}
prepair() {
const snsPromise = new Promise((resolve, reject) => {
configStorage.watch("template/token", (token) => {
if (this.snsConnect) {
this.snsConnect.disconnect();
}
const sns = new SocialNetworkService(token);
sns.connect((err, conn) => {
if (err) {
return reject(err)
}
this.snsConnect = conn;
resolve();
});
});
});
const rabbitmqPromise = new Promise((resolve) => {
configStorage.watch("global/rabbitmq", (connString) => {
if (!this.rabbitConnect) {
this.rabbitConnect = new Rabbitmq();
}
this.rabbitConnect.config(connString);
resolve();
});
});
return Promise.all([snsPromise, rabbitmqPromise]);
}
start() { //class entry point
this.listenAccount();
this.listenPost();
}
listenAccount() {
this.rabbitConnect.listen(this.config.accountQueue, (err, msg) => {
if (err) {
console.error(err);
return process.nextTick(() => this.listenAccount());
}
const task = JSON.parse(msg.payload);
async.autoInject({
account: (cb) => {
this.snsConnect.findAccount(task.userId, cb);
},
posts: (account, cb) => {
this.snsConnect.getPosts(account, cb);
}
}, (err, scope) => {
if (err) {
msg.resolve();
return console.error(err);
}
async.eachSeries(scope.posts, (post, cb) => {
async.parallel([
(cb) => {
this.rabbitConnect.send(post, this.config.storePostQueue, cb);
},
(cb) => {
this.rabbitConnect.send(post, this.config.storeCommentQueue, cb);
}
], cb)
}, (err) => {
err && console.error(err);
msg.resolve();
})
});
})
}
listenPost() {
this.rabbitConnect.listen(this.config.postQueue, (err, msg) => {
if (err) {
console.error(err);
return process.nextTick(() => this.listenPost());
}
const task = JSON.parse(msg.payload);
async.autoInject({
account: (cb) => {
this.snsConnect.findAccount(task.userId, cb);
},
post: (account, cb) => {
this.snsConnect.openPost(account, task.postId, cb);
},
comments: (post, cb) => {
this.snsConnect.getComments(post, cb);
}
}, (err, scope) => {
if (err) {
msg.resolve();
return console.error(err);
}
async.until(() => !scope.comments.length, (cb) => {
const batch = scope.comments.splice(0, 20);
this.rabbitConnect.send(batch, this.config.storeCommentQueue, cb);
}, (err) => {
err && console.error(err);
msg.resolve();
})
});
})
}
}
module.exports = new Template();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment