I have a problem with my ktor project, on my local computer server works well, but when I put it on a virtual machine I have exception
Exception in thread "DefaultDispatcher-worker-1" java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist
.
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:161)
at com.google.firebase.FirebaseApp.getInstance(FirebaseApp.java:132)
at com.google.firebase.database.FirebaseDatabase.getInstance(FirebaseDatabase.java:81)
at com.example.ApplicationKt.deleteOldEvents(Application.kt:532)
at
com.example.ApplicationKt$module$3.invokeSuspend(Application.kt:187)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
I don’t know why this is happening, but I can’t solve this problem for 2 days, help please
I really need help. Please help as soon as possible
code:
@Serializable
data class Request(val receiverUid: String, val senderUid: String)
fun main(args: Array<String>) {
try {
val serviceAccount = FileInputStream("src/main/resources/google-services.json")
val options = FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("there is my url for database")
.build()
if(FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options)
println("FirebaseApp initialized successfully")
}
} catch (e: Exception) {
println("Error initializing FirebaseApp: ${e.message}")
}
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module).start(wait = true)
}
fun Application.module() {
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
}
launch {
while(true) {
deleteOldEvents()
delay(1000 * 3600 * 60 * 24)
}
}
install(Routing) {
get("/") {
call.respondText("Hello World!")
}
#other code
}
}
suspend fun deleteOldEvents() {
val dbEvent = FirebaseDatabase.getInstance().getReference("current_events")
val dbGroups = FirebaseDatabase.getInstance().getReference("groups")
val reports = FirebaseDatabase.getInstance().getReference("reports")
val currentTime = Instant.now().epochSecond
suspendCoroutine<Unit> { continuation ->
dbEvent.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
dataSnapshot.children.forEach { eventSnapshot ->
val timestamp = eventSnapshot.child("timestamp").value as Long
if (currentTime - timestamp > 5 * 24 * 60 * 60) {
eventSnapshot.ref.removeValue() { error, _ ->
continuation.resume(Unit)
}
}
}
}
override fun onCancelled(error: DatabaseError) {
continuation.resumeWithException(error.toException())
}
})
}
}