Hi there! In our Ktor+Koin app we're using Ehcache...
# koin
i
Hi there! In our Ktor+Koin app we're using Ehcache as a singleton scoped dependency. In development mode, when some code changes, Ktor reloads the application which seems to destroy the dependency 'store' previously instantiated by Koin. Therefore when requested again, Koin will try to recreate the Ehcache cachemanager which will fail because a cachemanager instance has already locked the cache directory. Any ideas how tha handle this?
1
l
Moving Koin instantiation around seems like an idea to me. Perhaps to a point where ktor doesn't destroy it, if that's available Also, forcefully garbage collecting seems like an option during development to remove the Ehcache instance and thus dropping the lock, but I'm not sure if it works that way. Any idea on dropping the lock forcefully would probably work.
As you're talking about specific issues within the development cycle, could you set EhCache to create a new cache directory every time, for instance in the /tmp/ directory? That would also solve your issue without having to touch Koin or Ktor
i
Thanks for the suggestions! Nicely garbage collecting would be the best. I actually take care of this in an
ApplicationStopPreparing
event handler. At this hook DI is still available and I can get the cache manager and close it properly. This works just fine at app shutdown. Hot-reload however does not fire this event for some reason, only
ApplicationStopping
and
ApplicationStopped
where DI is not working anymore. I could of course create and hold my instances in my plugin scope instead of the Koin module but that seems kind of an anti-pattern to me.
a
Koin is monitoring Ktor with the following events:
after you singleton you can specify
onClose { }
with something to do on DI closing sequence
you receive the instance that is being closed in this lambda
i
Wow, this works like charm! Thanks a lot! I suggest adding this to the Koin+Ktor docs page, it's really useful.
❤️ 1
a
yes that's a good one. And adding Ehcache to Koin, can be a good tool 🙂