Skip to content

Instantly share code, notes, and snippets.

@endor
Created November 14, 2018 11:35
Show Gist options
  • Save endor/f797c0892a682c90159d29314247423f to your computer and use it in GitHub Desktop.
Save endor/f797c0892a682c90159d29314247423f to your computer and use it in GitHub Desktop.
Bottleneck
import Bottleneck from "bottleneck";
const redisUrl = "redis://127.0.0.1:6379";
const integrations = [
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
];
const globalInstance1 = new Bottleneck({
clearDatastore: true,
clientOptions: {
url: redisUrl,
},
datastore: "redis",
id: "global",
maxConcurrent: 3,
});
const globalInstance2 = new Bottleneck({
clearDatastore: true,
clientOptions: {
url: redisUrl,
},
datastore: "redis",
id: "global",
maxConcurrent: 3,
});
const globalInstance3 = new Bottleneck({
clearDatastore: true,
clientOptions: {
url: redisUrl,
},
datastore: "redis",
id: "global",
maxConcurrent: 3,
});
function scheduleJob(id: string, instance: string, scheduler: Bottleneck) {
scheduler.schedule({id}, () => {
console.log(`Instance ${instance} running ${id} -- ${new Date()}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 10000);
});
}).then(() => {
scheduleJob(id, instance, scheduler);
});
}
integrations.forEach((name) => {
// run on instance1
const b1 = new Bottleneck({
id: name,
maxConcurrent: 1,
minTime: 60000,
});
b1.chain(globalInstance1);
scheduleJob(name, "1", b1);
// run on instance2
const b2 = new Bottleneck({
id: name,
maxConcurrent: 1,
minTime: 60000,
});
b2.chain(globalInstance2);
scheduleJob(name, "2", b2);
// run on instance3
const b3 = new Bottleneck({
id: name,
maxConcurrent: 1,
minTime: 60000,
});
b3.chain(globalInstance3);
scheduleJob(name, "3", b3);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment