Browse Source

don't use lateinit in CacheController()

pull/18/head
Jannik 2 years ago
parent
commit
3aa27dff4a
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 4
      src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt
  2. 4
      src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt
  3. 26
      src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt

4
src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt

@ -60,12 +60,12 @@ class APIController {
// TODO remove this with API version 2.0.0
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
@RequestMapping("/courses")
fun courses(): CourseList {
fun courses(): CoursesList {
return courseList()
}
@RequestMapping("/courseList")
fun courseList(): CourseList {
fun courseList(): CoursesList {
logger.info("courseList request at ${LocalDateTime.now()}!")
updateTotalRequests()
return courseList

4
src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt

@ -28,9 +28,9 @@ import java.util.*
// data classes for the course part
data class Course(val courseName: String, val courseLink: String)
data class CourseMeta(val updateTime: Long, val totalCourses: Int)
data class CoursesMeta(val updateTime: Long, val totalCourses: Int)
data class CourseList(val meta: CourseMeta, val courses: ArrayList<Course>)
data class CoursesList(val meta: CoursesMeta, val courses: ArrayList<Course>)
// data classes for the Mensa part
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)

26
src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt

@ -55,8 +55,8 @@ class CacheController {
companion object{
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
lateinit var courseList: CourseList
lateinit var mensaMenu: MensaMenu
var courseList = CoursesList(CoursesMeta(0, 0), ArrayList())
var mensaMenu = MensaMenu(MensaMeta(0,""), MensaWeek(), MensaWeek())
var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
/**
@ -141,12 +141,12 @@ class CacheController {
*/
private fun asyncUpdateCourseList() = GlobalScope.launch {
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
courseList = CourseList(
CourseMeta(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)}")
logger.info("Updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
}
/**
@ -164,7 +164,7 @@ class CacheController {
)
}
logger.info("updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
logger.info("Updated mensamenu successful at ${Date(mensaMenu.meta.updateTime * 1000)}")
}
/**
@ -173,7 +173,7 @@ class CacheController {
* a FixedThreadPool is used to make parallel requests for faster updates
*/
private fun asyncUpdateTimetables() = GlobalScope.launch {
logger.info("updating ${timetableList.size} timetables ...")
logger.info("Updating ${timetableList.size} timetables ...")
// create a new ThreadPool with 5 threads
val executor = Executors.newFixedThreadPool(5)
@ -189,7 +189,7 @@ class CacheController {
}
} catch (ex: Exception) {
logger.error("error while updating the timetables", ex)
logger.error("Error while updating the timetables", ex)
} finally {
executor.shutdown()
}
@ -214,16 +214,16 @@ class CacheController {
* runBlocking: otherwise the api would return no data to requests for a few seconds after startup
*/
private fun initUpdates() = runBlocking {
// get all courses on startup
// get all course links on startup, make sure there are course links
val jobCourseUpdate = GlobalScope.async {
CourseListParser().getCourseLinks(StartupController.courseListURL)?.let {
courseList = CourseList(
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
courseList = CoursesList(
CoursesMeta(System.currentTimeMillis() / 1000, it.size), it
)
}
}
// get the current and next weeks mensa menus
// get the current and next weeks mensa menu
val jobMensa = GlobalScope.async{
val mensaCurrentWeek = MensaParser().getMensaMenu(StartupController.mensaMenuURL)
val mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(StartupController.mensaMenuURL))
@ -239,7 +239,7 @@ class CacheController {
jobCourseUpdate.await()
jobMensa.await()
logger.info("init updates successful")
logger.info("Initial updates successful")
}
/**

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close