mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-10 22:31:13 +00:00
Compare commits
4 Commits
b1432c662f
...
debug
Author | SHA1 | Date | |
---|---|---|---|
|
f27c0da775 | ||
|
c528448b5e | ||
|
db78bf3ff6 | ||
|
d7d4228063 |
@@ -28,6 +28,7 @@ import java.lang.Thread
|
|||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
object ChzzkHandler {
|
object ChzzkHandler {
|
||||||
private val handlers = mutableListOf<UserHandler>()
|
private val handlers = mutableListOf<UserHandler>()
|
||||||
@@ -36,6 +37,9 @@ object ChzzkHandler {
|
|||||||
@Volatile private var running: Boolean = false
|
@Volatile private var running: Boolean = false
|
||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
|
private val lastRunMap = ConcurrentHashMap<String, Long>()
|
||||||
|
private val requiredWait = 300_000L
|
||||||
|
|
||||||
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
||||||
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = LocalDateTime.now()))
|
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = LocalDateTime.now()))
|
||||||
}
|
}
|
||||||
@@ -128,6 +132,7 @@ object ChzzkHandler {
|
|||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.info("Thread 1 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}")
|
logger.info("Thread 1 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}")
|
||||||
} finally {
|
} finally {
|
||||||
|
lastRunMap[it.channel.channelId] = System.currentTimeMillis()
|
||||||
Thread.sleep(5000)
|
Thread.sleep(5000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,12 +141,21 @@ object ChzzkHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val threadRunner2 = Runnable {
|
val threadRunner2 = Runnable {
|
||||||
logger.info("Thread 2 started!")
|
|
||||||
logger.info("Thread 2 started!")
|
logger.info("Thread 2 started!")
|
||||||
while (running) {
|
while (running) {
|
||||||
handlers.forEach {
|
handlers.forEach {
|
||||||
if (!running) return@forEach
|
if (!running) return@forEach
|
||||||
try {
|
try {
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
val lastRun = lastRunMap[it.channel.channelId] ?: 0L
|
||||||
|
val waitedTime = now - lastRun
|
||||||
|
|
||||||
|
if(waitedTime < requiredWait) {
|
||||||
|
val sleepTime = requiredWait - waitedTime
|
||||||
|
logger.info("Thread 2 Sleep: ${it.channel.channelName} / ${sleepTime}ms")
|
||||||
|
Thread.sleep(sleepTime)
|
||||||
|
}
|
||||||
|
|
||||||
val streamInfo = Connector.getLive(it.channel.channelId)
|
val streamInfo = Connector.getLive(it.channel.channelId)
|
||||||
if (streamInfo?.isOnline == true && !it.isActive) {
|
if (streamInfo?.isOnline == true && !it.isActive) {
|
||||||
try {
|
try {
|
||||||
|
@@ -14,7 +14,7 @@ object Metrics {
|
|||||||
.register(registry)
|
.register(registry)
|
||||||
|
|
||||||
var activeStreamer = 0.0
|
var activeStreamer = 0.0
|
||||||
val activateGauge: Gauge = Gauge.builder("active_streamer_gauge", this) { streamer }
|
val activateGauge: Gauge = Gauge.builder("active_streamer_gauge", this) { activeStreamer }
|
||||||
.description("Current Active Streamer Count")
|
.description("Current Active Streamer Count")
|
||||||
.register(registry)
|
.register(registry)
|
||||||
|
|
||||||
@@ -24,8 +24,10 @@ object Metrics {
|
|||||||
.register(registry)
|
.register(registry)
|
||||||
|
|
||||||
fun refreshStreamerMetrics() {
|
fun refreshStreamerMetrics() {
|
||||||
streamer = UserService.getAllUsers().size.toDouble()
|
val streamers = UserService.getAllUsers()
|
||||||
activeStreamer = UserService.getAllUsers().filter { !it.isDisabled }.size.toDouble()
|
|
||||||
|
streamer = streamers.size.toDouble()
|
||||||
|
activeStreamer = streamers.filter { !it.isDisabled }.size.toDouble()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun increaseStreaming(inc: Int = 1) {
|
fun increaseStreaming(inc: Int = 1) {
|
||||||
@@ -34,4 +36,8 @@ object Metrics {
|
|||||||
fun decreaseStreaming(dec: Int = 1) {
|
fun decreaseStreaming(dec: Int = 1) {
|
||||||
streaming -= dec
|
streaming -= dec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
refreshStreamerMetrics()
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user