-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollAPI.js
43 lines (36 loc) · 1.07 KB
/
pollAPI.js
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
33
34
35
36
37
38
39
40
41
42
43
// const { Worker, SHARE_ENV } = require("worker_threads");
const { Worker } = require("worker_threads");
let isPolling = false;
let frequencyInMinutes = 1;
setInterval(function () {
if (!isPolling) return;
// check for date and time
const now = new Date();
const day = now.getDay();
// if it's either Sunday or Saturday, return
if (day === 0 || day === 6) return;
// if it's not in between 6:30am to 1:00pm PDT, return
const hours = now.getUTCHours();
const minutes = now.getUTCMinutes();
if (hours < 13 || hours > 20) return;
if (hours === 13 && minutes < 30) return;
console.log("Starting to poll API at", new Date());
const worker = new Worker("./worker.js");
worker.on("message", function (message) {
console.log(message);
});
worker.on("online", function () {
console.log("Worker thread has started");
});
}, frequencyInMinutes * 60 * 1000);
module.exports = {
start: function () {
isPolling = true;
},
stop: function () {
isPolling = false;
},
setFrequencyMinutes: function (mins) {
frequencyInMinutes = mins;
},
};