Last active
May 7, 2018 08:34
-
-
Save rifatx/6f84b79cfe4a0e951b0be3f15a435e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async buildConsumers() { | |
const path = Path.resolve(__dirname + '/../../../Consumers'); | |
if (FS.existsSync(path)) { | |
const consumers = Object.values(RequireAll({ | |
dirname: path, | |
recursive: false | |
})).map(consumer => consumer.default); | |
return await new Promise(resolve => { | |
let counter = 0; | |
consumers.forEach(async consumer => { | |
const instance = new consumer(); | |
const googleConsumer = await this.createGoogleConsumer(instance.model.topic, instance['shared']); | |
instance.setConsumer(googleConsumer); | |
counter++; | |
if (consumers.length === counter) { | |
console.log(Chalk.green("PubSub Consumers Ready")); | |
resolve(); | |
} | |
}); | |
}); | |
} | |
} | |
async createGoogleConsumer(topicName, shared = false) { | |
const topic = this.client.topic(topicName); | |
await topic.get({ autoCreate: true }); | |
const name = shared ? Config.target.name+'_'+topicName+'_'+OS.hostname()+'_'+Config.values.env : Config.target.name+'_'+topicName+'_'+Config.values.env; | |
return await this.client.topic(topicName).createSubscription(name, { ackDeadline: 60000*5, ackDeadlineSeconds: 60*5 }).then(consumer => consumer[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment