-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (30 loc) · 1.08 KB
/
index.js
File metadata and controls
32 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const { Worker } = require('node:worker_threads');
const fs = require('node:fs');
const { Cron } = require('croner');
const config = require('./config.json');
const { instance } = require('./instance');
(async () => {
for (const file of fs.readdirSync('./workers')) {
if (!file.endsWith('.js')) continue;
// Create worker from the file
const worker = new Worker(`./workers/${file}`);
worker.on('message', async (message) => {
if (!message.isNew) return false;
console.log(new Date(), `Got ${message.newArticles.length} new events from ${message.workerName}`);
await instance.post(config.webhookUrl, {
content: config.content,
embeds: message.newArticles.map((article) => ({
title: article.title,
url: article.url,
description: article.description,
image: { url: article.image },
timestamp: article.createdAt,
color: article.color,
})),
});
});
(new Cron(config.cronExpression || '*/10 * * * * *', () => {
worker.postMessage('RUN');
})).trigger();
}
})();