Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of onClose calls #1510

Closed
smichovan opened this issue Jan 26, 2023 Discussed in #1507 · 5 comments · May be fixed by #2020
Closed

Order of onClose calls #1510

smichovan opened this issue Jan 26, 2023 Discussed in #1507 · 5 comments · May be fixed by #2020
Labels
ktor question Usage question status:wontfix type:improvement Improving a current feature

Comments

@smichovan
Copy link

Discussed in #1507

Originally posted by smichovan January 20, 2023
Is there a way how to change order in witch the onClose methods of components are called?

Lets have two services where the latter is dependent of the former:

val appModule = module {
single { Database() } onClose {it?.closeConnectionPool()}
single { ServiceUsingDb(get()) } onClose {it?.useDatabaseForCleanup()}
}

Database component maintains a connection to some DB and the second component accesses the DB through the first component. The "ServiceUsingDb" needs to have DB available in onClose method. As the "ServiceUsingDb" is actually dependent on "Database" I would expect Koin to ensure the right closing order.

I made some research in the Koin code and if I understand it well, the components (instances, mappings) are stored in HashMap, means no specific order is guaranteed. In my case, it may (and does) happen that when ServiceUsingDb onClose is called, the DB is already closed and the cleanup fails.

Have I missed anything? Is there any way how to influence the onClose methods order?

@arnaudgiuliani arnaudgiuliani added ktor question Usage question labels Jan 27, 2023
@arnaudgiuliani
Copy link
Member

Yes, Koin will unload things in order of its index. The onClose function could be predicted to a common component to help you clear resources.

A scope closure could help you trigger one function to close your resources.

I imagine here, more a shutdown process for the overall Koin app, no? 🤔

@arnaudgiuliani arnaudgiuliani added the type:improvement Improving a current feature label Jan 27, 2023
@stale
Copy link

stale bot commented Jun 26, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status:wontfix label Jun 26, 2023
@stale stale bot closed this as completed Jul 9, 2023
@alturkovic
Copy link

I am running into the same problem as @smichovan where services are closed in no specific order. What is the recommended way to close services in the example that @smichovan provided? A short code example would be greatly appreciated.

@jacekgajek
Copy link

jacekgajek commented Jun 7, 2024

I think that Koin should automatically infer order from dependencies relation. Plus it could throw an error in case there is a cyclic depenency so the orded is not possible to predict, the same as with dependency injection.

@jacekgajek
Copy link

jacekgajek commented Jun 7, 2024

About predicting of order I'm also out in blue. My code is as follows:

fun Application.configureKoin() {
	install(Koin) {
			configureSqs
			configureSns
			configureConsumer
	}
}

Each of them registers a onClose handler which executes a println. The output in the console is consistent and always looks like this:

SQS closing
Consumer closing
SNS closing

It doesn't change if I switch order of configure statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ktor question Usage question status:wontfix type:improvement Improving a current feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants