-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.js
141 lines (112 loc) · 3.39 KB
/
scheduler.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require('dotenv').config()
const moment = require('moment-timezone')
const requestPromise = require('request-promise')
var timeouts = []
var intervals = []
const millisInHour = 1000 * 60 * 60
const millisInDay = millisInHour * 24
const getMillisUntilTime = (hour, minute) => {
if (minute === undefined) minute = 0
var now = moment()
var nowInNewYork = now.clone().tz('America/New_York')
var timeInNewYork = now.clone().tz('America/New_York').hour(hour).minute(minute).seconds(0).milliseconds(0)
var diff = timeInNewYork.diff(nowInNewYork)
if (diff < 0) diff += millisInDay // it's after the time, try again tomorrow
return diff
}
const getMillisNearest = (minute) => {
if (minute < 0 || minute > 59) throw Error(`Invalid 'minute' argument: ${minute}`)
var now = moment()
var nowInNewYork = now.clone().tz('America/New_York')
var nextMinute = minute === 0 ? 0 : Math.ceil((nowInNewYork.minute() + 1) / minute) * minute
var hoursToAdd = 0
if (nextMinute >= 60 || nextMinute === 0) {
hoursToAdd = 1
nextMinute = minute
}
var timeInNewYork =
now.clone().tz('America/New_York').add(hoursToAdd, 'hour').minute(nextMinute).seconds(0).milliseconds(0)
return timeInNewYork.diff(nowInNewYork)
}
const hydroBeenWarned = {
gas: false,
balance: true
}
// GET /
const callIndex = () => {
let options = {
method: 'GET',
timeout: 1000 * 10,
url: process.env.API_URL,
auth: {
'bearer': process.env.ACCESS_TOKEN
}
}
requestPromise(options)
}
// POST /gas
const callGas = (notify) => {
if (notify === undefined) throw Error('Must notify at least one chat.')
let options = {
method: 'POST',
timeout: 1000 * 10,
url: `${process.env.API_URL}gas`,
auth: {
'bearer': process.env.ACCESS_TOKEN
},
body: {
notify: notify
},
json: true
}
return requestPromise(options)
}
// POST /balance
const callBalance = (notify) => {
if (notify === undefined) throw Error('Must notify at least one chat.')
let options = {
method: 'POST',
timeout: 1000 * 10,
url: `${process.env.API_URL}balance`,
auth: {
'bearer': process.env.ACCESS_TOKEN
},
body: {
address: '0x0fccb4868b7f13ede288aff9298fce67541e3d38',
thresholdBalance: 1,
notify: notify
},
json: true
}
return requestPromise(options)
}
const scheduleCall = (call, waitTime, intervalTime, callImmediately) => {
if (callImmediately === true) call()
timeouts.push(setTimeout(() => {
call()
intervals.push(setInterval(() => {
call()
}, intervalTime))
}, waitTime))
}
const onceEvery = (call, minutes, callImmediately) => {
let waitTime = getMillisNearest(0)
let intervalTime = 1000 * 60 * minutes
scheduleCall(call, waitTime, intervalTime, callImmediately)
}
const oncePerDay = (call, hour, callImmediately) => {
let waitTime = getMillisUntilTime(hour)
let intervalTime = millisInDay
scheduleCall(call, waitTime, intervalTime, callImmediately)
}
// wait 2 seconds before beginning
setTimeout(() => {
// make sure the app stays awake by calling it every 20 minutes
scheduleCall(callIndex, getMillisNearest(20), 1000 * 60 * 20, true)
// logs every interval
onceEvery(() => { callGas(['logs']) }, 60)
onceEvery(() => { callBalance(['logs']) }, 60)
// notifications to hydro once per day
oncePerDay(() => { callGas(['hydro']) }, 9)
oncePerDay(() => { callBalance(['hydro']) }, 9)
}, 1000 * 2)