made CacheCOntroller() static
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jannik 2020-02-16 17:17:39 +01:00
parent f20279a4b4
commit be95af43c2
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
1 changed files with 100 additions and 128 deletions

View File

@ -44,14 +44,11 @@ import kotlin.concurrent.scheduleAtFixedRate
class CacheController {
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
init {
initUpdates()
scheduledUpdates()
}
// cache objects
companion object{
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
@ -77,18 +74,13 @@ class CacheController {
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
val jobTimetable = GlobalScope.async {
val jobTimetable = async {
timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
}
jobTimetable.await()
timetableList.add(
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink),
timetable
)
)
timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
logger.info("added new timetable for $courseName, week $weekIndex")
}
@ -133,146 +125,126 @@ class CacheController {
return lessonList
}
}
/**
* this function updates the courseList
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateCourseList() = GlobalScope.launch {
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
courseList = CoursesList(
CoursesMeta(System.currentTimeMillis() / 1000, it.size), it
)
}
// private cache functions
logger.info("Updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
}
/**
* this function updates the mensa menu list
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateMensa() = GlobalScope.launch {
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.mensaMenuURL)
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.mensaMenuURL))
// only update if we get valid data
if (mensaCurrentWeek != null && mensaNextWeek != null) {
mensaMenu = MensaMenu(
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
)
}
logger.info("Updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
}
/**
* this function updates all existing timetables
* during the update process the old data will be returned for a API request
* a FixedThreadPool is used to make parallel requests for faster updates
*/
private fun asyncUpdateTimetables() = GlobalScope.launch {
logger.info("Updating ${timetableList.size} timetables ...")
// create a new ThreadPool with 5 threads
val executor = Executors.newFixedThreadPool(5)
try {
timetableList.forEach { timetableCourse ->
executor.execute {
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
saveTimetableToCache(timetableCourse) // save the updated timetable to the cache directory
}
}
} catch (ex: Exception) {
logger.error("Error while updating the timetables", ex)
} finally {
executor.shutdown()
}
}
/**
* save a timetable to the cache directory
* this is only call on async updates, it is NOT call when first getting the timetable
* @param timetable a timetable of the type [TimetableCourseWeek]
*/
private fun saveTimetableToCache(timetable: TimetableCourseWeek) {
println(timetable.timetable.toString())
val file = File(StartupController.dirTcorCache, "timetable-${timetable.meta.courseName}-${timetable.meta.weekIndex}.json")
val writer = BufferedWriter(FileWriter(file))
writer.write(Gson().toJson(timetable))
writer.close()
}
/**
* before the APIController is up, get the data fist
* runBlocking: otherwise the api would return no data to requests for a few seconds after startup
*/
private fun initUpdates() = runBlocking {
// get all course links on startup, make sure there are course links
val jobCourseUpdate = GlobalScope.async {
/**
* this function updates the courseList
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateCourseList() = GlobalScope.launch {
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
courseList = CoursesList(
CoursesMeta(System.currentTimeMillis() / 1000, it.size), it
)
courseList = CoursesList(CoursesMeta(System.currentTimeMillis() / 1000, it.size), it)
}
logger.info("Updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
}
// get the current and next weeks mensa menu
val jobMensa = GlobalScope.async{
/**
* this function updates the mensa menu list
* during the update process the old data will be returned for a API request
*/
private fun asyncUpdateMensa() = GlobalScope.launch {
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.mensaMenuURL)
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.mensaMenuURL))
// only update if we get valid data
if (mensaCurrentWeek != null && mensaNextWeek != null) {
mensaMenu = MensaMenu(
MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek
)
mensaMenu = MensaMenu(MensaMeta(System.currentTimeMillis() / 1000, StartupController.mensaName), mensaCurrentWeek, mensaNextWeek)
}
logger.info("Updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
}
/**
* this function updates all existing timetables
* during the update process the old data will be returned for a API request
* a FixedThreadPool is used to make parallel requests for faster updates
*/
private fun asyncUpdateTimetables() = GlobalScope.launch {
logger.info("Updating ${timetableList.size} timetables ...")
// create a new ThreadPool with 5 threads
val executor = Executors.newFixedThreadPool(5)
try {
timetableList.forEach { timetableCourse ->
executor.execute {
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
saveTimetableToCache(timetableCourse) // save the updated timetable to the cache directory
}
}
} catch (ex: Exception) {
logger.error("Error while updating the timetables", ex)
} finally {
executor.shutdown()
}
}
jobCourseUpdate.await()
jobMensa.await()
/**
* save a timetable to the cache directory
* this is only call on async updates, it is NOT call when first getting the timetable
* @param timetable a timetable of the type [TimetableCourseWeek]
*/
private fun saveTimetableToCache(timetable: TimetableCourseWeek) {
println(timetable.timetable.toString())
logger.info("Initial updates successful")
}
/**
* update the CourseList every 24h, the Timetables every 3h and the Mensa Menu every hour
* doesn't account the change between winter and summer time!
*/
private fun scheduledUpdates() {
val currentTime = System.currentTimeMillis()
val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000
// update courseList every 24 hours (time in ms)
Timer().scheduleAtFixedRate(initDelay24h, 86400000) {
asyncUpdateCourseList()
val file = File(StartupController.dirTcorCache, "timetable-${timetable.meta.courseName}-${timetable.meta.weekIndex}.json")
val writer = BufferedWriter(FileWriter(file))
writer.write(Gson().toJson(timetable))
writer.close()
}
// update all already existing timetables every 3 hours (time in ms)
Timer().scheduleAtFixedRate(initDelay3h, 10800000) {
asyncUpdateTimetables()
/**
* before the APIController is up, get the data fist
* runBlocking: otherwise the api would return no data to requests for a few seconds after startup
*/
private fun initUpdates() = runBlocking {
// get all course links on startup, make sure there are course links
val jobCourseUpdate = asyncUpdateCourseList()
val jobMensa = asyncUpdateMensa()
jobCourseUpdate.join()
jobMensa.join()
logger.info("Initial updates successful")
}
// update courses every hour (time in ms)
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
asyncUpdateMensa()
}
/**
* update the CourseList every 24h, the Timetables every 3h and the Mensa Menu every hour
* doesn't account the change between winter and summer time!
*/
private fun scheduledUpdates() {
val currentTime = System.currentTimeMillis()
val initDelay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
val initDelay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
val initDelay1h = (3600000 - ((currentTime + 3600000) % 3600000)) + 60000
// post to status.mosad.xyz every hour, if an API key is present
if (StartupController.cachetAPIKey != "0") {
// update courseList every 24 hours (time in ms)
Timer().scheduleAtFixedRate(initDelay24h, 86400000) {
asyncUpdateCourseList()
}
// update all already existing timetables every 3 hours (time in ms)
Timer().scheduleAtFixedRate(initDelay3h, 10800000) {
asyncUpdateTimetables()
}
// update courses every hour (time in ms)
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
CachetAPIController.postTotalRequests()
asyncUpdateMensa()
}
// post to status.mosad.xyz every hour, if an API key is present
if (StartupController.cachetAPIKey != "0") {
Timer().scheduleAtFixedRate(initDelay1h, 3600000) {
CachetAPIController.postTotalRequests()
}
}
}
}
}
}