forked from zozs/a-wild-button-appears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler-standalone.js
30 lines (25 loc) · 1.02 KB
/
handler-standalone.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
const schedule = require('node-schedule')
const { hourlyCheck } = require('./announces')
const { asyncEventRouter } = require('./async-routes')
const wildbuttonApp = require('./wildbutton')
// Only used if running standalone, if serverless, a scheduled
// event to a specific endpoint is used instead.
function initSchedule () {
const wrapHourlyCheck = async () => {
try {
await hourlyCheck()
} catch (e) {
console.error(`Failed to perform hourly check, got error: ${e} in JSON: ${JSON.stringify(e)}`)
}
}
schedule.scheduleJob('*/10 * * * *', wrapHourlyCheck)
}
initSchedule()
wildbuttonApp(asyncEventHandler).listen(process.env.PORT, () => {
console.log(`A wild BUTTON appeared (standalone) listening on port ${process.env.PORT}`)
})
// In standalone mode, we can call the async router almost directly, except that we defer it
// to the next event loop to allow us to acknowledge Slack before doing event.
async function asyncEventHandler (eventObject) {
setImmediate(() => asyncEventRouter(eventObject))
}