use a FixedThreadPool to execute the timetable updates
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
closes #9
This commit is contained in:
parent
db57059727
commit
3177be1bf0
|
@ -33,7 +33,7 @@ repositories {
|
|||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1"
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2"
|
||||
implementation 'org.jsoup:jsoup:1.12.1'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
|
||||
|
|
|
@ -39,7 +39,6 @@ import org.springframework.web.bind.annotation.RequestParam
|
|||
import org.springframework.web.bind.annotation.RestController
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
@RestController
|
||||
class APIController {
|
||||
|
|
|
@ -33,7 +33,9 @@ import org.mosad.thecitadelofricks.hsoparser.TimetableParser
|
|||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.collections.HashSet
|
||||
import kotlin.concurrent.scheduleAtFixedRate
|
||||
|
||||
class CacheController {
|
||||
|
@ -153,12 +155,8 @@ class CacheController {
|
|||
*/
|
||||
private fun asyncUpdateCourseList() = GlobalScope.launch {
|
||||
CourseListParser().getCourseLinks(courseListURL)?.let {
|
||||
courseList =
|
||||
CourseList(
|
||||
CourseMeta(
|
||||
System.currentTimeMillis() / 1000,
|
||||
it.size
|
||||
), it
|
||||
courseList = CourseList(
|
||||
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -175,12 +173,8 @@ class CacheController {
|
|||
|
||||
// only update if we get valid data
|
||||
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
||||
mensaMenu =
|
||||
MensaMenu(
|
||||
MensaMeta(
|
||||
System.currentTimeMillis() / 1000,
|
||||
mensaName
|
||||
), mensaCurrentWeek, mensaNextWeek
|
||||
mensaMenu = MensaMenu(
|
||||
MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -190,14 +184,27 @@ class CacheController {
|
|||
/**
|
||||
* 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 ->
|
||||
val updateURL = timetableCourse.meta.link
|
||||
timetableCourse.timetable = TimetableParser().getTimeTable(updateURL)
|
||||
executor.execute {
|
||||
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
|
||||
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
||||
}
|
||||
logger.info("updated ${timetableList.size} timetables successful!")
|
||||
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
logger.error("error while updating the timetables", ex)
|
||||
} finally {
|
||||
executor.shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -208,12 +215,8 @@ class CacheController {
|
|||
// get all courses on startup
|
||||
val jobCourseUpdate = GlobalScope.async {
|
||||
CourseListParser().getCourseLinks(courseListURL)?.let {
|
||||
courseList =
|
||||
CourseList(
|
||||
CourseMeta(
|
||||
System.currentTimeMillis() / 1000,
|
||||
it.size
|
||||
), it
|
||||
courseList = CourseList(
|
||||
CourseMeta(System.currentTimeMillis() / 1000, it.size), it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -225,12 +228,8 @@ class CacheController {
|
|||
|
||||
// only update if we get valid data
|
||||
if (mensaCurrentWeek != null && mensaNextWeek != null) {
|
||||
mensaMenu =
|
||||
MensaMenu(
|
||||
MensaMeta(
|
||||
System.currentTimeMillis() / 1000,
|
||||
mensaName
|
||||
), mensaCurrentWeek, mensaNextWeek
|
||||
mensaMenu = MensaMenu(
|
||||
MensaMeta(System.currentTimeMillis() / 1000, mensaName), mensaCurrentWeek, mensaNextWeek
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,8 @@ class StatusController {
|
|||
when (timetableRequests.stream().filter { x -> x.courseName == courseName }.findAny().orElse(null)) {
|
||||
null -> timetableRequests.add(TimetableCounter(courseName, 0))
|
||||
}
|
||||
timetableRequests.stream().filter { x ->x.courseName == courseName }.findFirst().ifPresent { x -> x.requests++ }
|
||||
timetableRequests.stream().filter { x -> x.courseName == courseName }.findFirst()
|
||||
.ifPresent { x -> x.requests++ }
|
||||
|
||||
// TODO Java 11
|
||||
// timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
|
||||
|
|
Loading…
Reference in New Issue