aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Loomans <noahloomans@gmail.com>2017-09-05 20:28:32 +0200
committerNoah Loomans <noahloomans@gmail.com>2017-09-05 20:28:32 +0200
commit600128bda0390f2051ad20ee42930f036c79323c (patch)
treed8af17fd1f30dc4b11f44763c3addba104636f40
parente3947f0d11864f40a2c0a202af99e0b14ff14dcf (diff)
Clear cache automaticly
-rw-r--r--lib/getMeetingpointData.js (renamed from lib/getUserIndex.js)33
-rw-r--r--routes/index.js23
2 files changed, 40 insertions, 16 deletions
diff --git a/lib/getUserIndex.js b/lib/getMeetingpointData.js
index 393eab0..fb4c6e4 100644
--- a/lib/getUserIndex.js
+++ b/lib/getMeetingpointData.js
@@ -5,8 +5,8 @@ const cheerio = require('cheerio')
const _ = require('lodash')
const request = Promise.promisify(require('request'))
-exports = {}
-module.exports = exports
+let meetingpointData
+let lastUpdate
function getUsers (page) {
const script = page('script').eq(1).text()
@@ -57,16 +57,35 @@ function getValidWeekNumbers(page) {
}
function requestData() {
+ lastUpdate = new Date()
+
return request(`http://www.meetingpointmco.nl/Roosters-AL/doc/dagroosters/frames/navbar.htm`).then((response) => {
const page = cheerio.load(response.body)
const users = getUsers(page)
const validWeekNumbers = getValidWeekNumbers(page)
- return { users, validWeekNumbers }
+ meetingpointData = { users, validWeekNumbers }
+
+ return meetingpointData
})
}
-requestData().then(({ users, validWeekNumbers }) => {
- exports.users = users
- exports.validWeekNumbers = validWeekNumbers
-})
+function getMeetingpointData () {
+ if (lastUpdate == null) {
+ return requestData()
+ } else if (!meetingpointData) {
+ // The first request hasn't resolved yet, throw an error.
+ return Promise.reject()
+ } else if (new Date() - lastUpdate > 10 * 60 * 1000) { // 10 minutes
+ return requestData().catch(function () {
+ console.warn('Unable to update userIndex, using cached.')
+
+ return meetingpointData
+ })
+ }
+ else {
+ return Promise.resolve(meetingpointData)
+ }
+}
+
+module.exports = getMeetingpointData
diff --git a/routes/index.js b/routes/index.js
index 570fe5f..68f29b7 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -2,19 +2,24 @@
const express = require('express')
const router = express.Router()
-const data = require('../lib/getUserIndex')
+const getMeetingpointData = require('../lib/getMeetingpointData')
/* GET home page. */
router.get('/', function (req, res, next) {
- let flags = []
- if (req.query.nfd != null) {
- flags.push('NO_FEATURE_DETECT')
- }
+ getMeetingpointData().then(data => {
+ let flags = []
+ if (req.query.nfd != null) {
+ flags.push('NO_FEATURE_DETECT')
+ }
- const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`
- const usersStr = `var USERS = ${JSON.stringify(data.users)};`
- const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`
- res.render('index', { flagsStr, usersStr, validWeekNumbersStr })
+ const flagsStr = `var FLAGS = ${JSON.stringify(flags)};`
+ const usersStr = `var USERS = ${JSON.stringify(data.users)};`
+ const validWeekNumbersStr = `var VALID_WEEK_NUMBERS = ${JSON.stringify(data.validWeekNumbers)}`
+ res.render('index', { flagsStr, usersStr, validWeekNumbersStr })
+ }).catch(function () {
+ console.error('Unable to get user info, emergency redirect!')
+ res.redirect('http://www.meetingpointmco.nl/Roosters-AL/doc/')
+ })
})
module.exports = router