Skip to content

Commit

Permalink
recover service after crash
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 3, 2025
1 parent 6049b79 commit da65496
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.greenart7c3.citrine.BuildConfig
import com.greenart7c3.citrine.Citrine
import com.greenart7c3.citrine.database.AppDatabase
import com.greenart7c3.citrine.database.toEventWithTags
import com.greenart7c3.citrine.service.CustomWebSocketService
import com.greenart7c3.citrine.service.LocalPreferences
import com.greenart7c3.citrine.utils.isParameterizedReplaceable
import com.greenart7c3.citrine.utils.shouldDelete
Expand All @@ -17,6 +18,8 @@ import com.vitorpamplona.quartz.events.Event
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationStarted
import io.ktor.server.application.ApplicationStopped
import io.ktor.server.application.install
import io.ktor.server.cio.CIO
import io.ktor.server.cio.CIOApplicationEngine
Expand Down Expand Up @@ -276,6 +279,15 @@ class CustomWebSocketServer(
port = port,
host = host,
) {
monitor.subscribe(ApplicationStarted) {
Log.d(Citrine.TAG, "Server started on $host:$port")
CustomWebSocketService.hasStarted = true
}

monitor.subscribe(ApplicationStopped) {
Log.d(Citrine.TAG, "Server stopped")
CustomWebSocketService.hasStarted = false
}
install(WebSockets) {
pingPeriodMillis = 1000L
timeoutMillis = 300000L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import com.greenart7c3.citrine.server.CustomWebSocketServer

object CustomWebSocketService {
var server: CustomWebSocketServer? = null
var hasStarted = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class WebSocketServerService : Service() {
fun getService(): WebSocketServerService = this@WebSocketServerService
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(Citrine.TAG, "onStartCommand ${CustomWebSocketService.hasStarted}")
return START_STICKY
}

@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onCreate() {
super.onCreate()
Expand Down

0 comments on commit da65496

Please sign in to comment.