generated from hatchways/express-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into reorder-switch
- Loading branch information
Showing
14 changed files
with
146 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
Binary file not shown.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const redis = require("redis"); | ||
const Queue = require("bull"); | ||
const User = require("../models/userModel"); | ||
const Mention = require("../models/mentionModel"); | ||
const { addMentionsToDB } = require("./scraper"); | ||
|
||
const connectRedis = () => { | ||
// to start redis server in background | ||
// run "redis-server --daemonize yes" | ||
// to stop the server running "redis-cli shutdown" | ||
const client = redis.createClient({ | ||
host: process.env.REDIS_HOST, | ||
port: process.env.REDIS_PORT, | ||
}); | ||
client.on("error", (err) => { | ||
console.log("Error " + err); | ||
}); | ||
|
||
client.on("ready", function () { | ||
console.log("redis is running"); | ||
}); | ||
}; | ||
|
||
const handleTaskQueues = () => { | ||
const companiesQueue = new Queue("queue-for-getting-componies", { | ||
redis: { port: process.env.REDIS_PORT, host: process.env.REDIS_HOST }, | ||
}); | ||
|
||
const scrapingQueue = new Queue("queue-for-scraping", { | ||
redis: { port: process.env.REDIS_PORT, host: process.env.REDIS_HOST }, | ||
}); | ||
// job producer, repeated every 15 mins | ||
companiesQueue.add( | ||
{}, | ||
{ | ||
repeat: { cron: "*/15 * * * *" }, | ||
delay: 2000, | ||
jobId: "repeatCompaniesUpdate", | ||
} | ||
); | ||
|
||
// to remove existedQueue use | ||
// companiesQueue.empty(); | ||
|
||
// job consumer | ||
companiesQueue.process(async function (job, done) { | ||
try { | ||
User.getAllCompanies().then((companies) => | ||
companies.forEach((company) => { | ||
for (var platform in User.schema.obj.platforms) { | ||
scrapingQueue.add({ company: company, platform: platform }); | ||
} | ||
}) | ||
); | ||
done(new Error("error adding companies to queue")); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}); | ||
|
||
scrapingQueue.process(async function (job, done) { | ||
try { | ||
await addMentionsToDB(job.data.company, job.data.platform); | ||
done(new Error(`${job.data.company} and ${job.data.platform} error`)); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { connectRedis, handleTaskQueues }; |
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
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