Skip to content

Handling DB cleanup correctly #765

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

Closed
Galaf opened this issue May 26, 2024 · 5 comments
Closed

Handling DB cleanup correctly #765

Galaf opened this issue May 26, 2024 · 5 comments
Assignees

Comments

@Galaf
Copy link

Galaf commented May 26, 2024

Hello!

Thanks for your good work!

I was wondering what would be the best practice to handle DB (or otherwise) cleanup.
Components offer a way to do Init() but there is no hook for deinitialization, therefore I don't know how best to express something like that:

client, err := ent.Open("driver", "host")
// err validation
defer client.Close() // <-- this.

I'm thinking that perhaps before serving the app, I could create a global variable and wait for the shutdown signal in a goroutine to do this but I'd like to have your opinion on this subject, if possible!

Thanks a lot for your time!

@renanbastos93
Copy link

Hi dude, I believe that your pain is valid to implement a new method like Init but with Shutdown. Even though not solve for whole your pain it will rather than. Well, I don't like using global variables. I'd make methods in my app to CloseDB(), which will call when you shut down your main Run method.

See below:

func Serve(ctx context.Context, e *implBFF) error {
	fmt.Printf("BFF listener available on %v\n", e.bff.String())

	f := fiber.New()

	e.createRouter(ctx, f)

	defer func(){
		e.db.Close()
	}()

	return f.Listener(e.bff)
}

@rgrandl What do you think of us implementing a Close(ctx) error method, like Init(ctx) error?

@Galaf
Copy link
Author

Galaf commented May 27, 2024

Yes, I thought about that but it beats the purpose of using Init(ctx) in the first place if I initialize everything in main(), I believe :)

@Galaf
Copy link
Author

Galaf commented May 28, 2024

Similar to: #275

rgrandl added a commit that referenced this issue May 28, 2024
In the current implementation, we provide the user the ability to
initialize things by implementing an `Init` method. However, they don't
have the ability to do a clean shutdown easily.

The user filled multiple github issues (#275 and #765) asking for a `Shutdown`
method.

This PR adds a `Shutdown` method to each component. Note that there is
no guarantee that this method can run - e.g., the app receives a
SIGKILL because the machine suddenly crashes. However, a `Shutdown`
method can enable the user to achieve graceful shutdown in normal
scenarios.

In tests, if someone wants to test the `Shutdown` method, they can get a
pointer to the implementation of the component and explicitly call the
`Shutdown` method.
@rgrandl
Copy link
Contributor

rgrandl commented May 28, 2024

I created #766 to add a Shutdown method.

@rgrandl rgrandl self-assigned this May 28, 2024
rgrandl added a commit that referenced this issue May 28, 2024
In the current implementation, we provide the user the ability to
initialize things by implementing an `Init` method. However, they don't
have the ability to do a clean shutdown easily.

The user filled multiple github issues (#275 and #765) asking for a `Shutdown`
method.

This PR adds a `Shutdown` method to each component. Note that there is
no guarantee that this method can run - e.g., the app receives a
SIGKILL because the machine suddenly crashes. However, a `Shutdown`
method can enable the user to achieve graceful shutdown in normal
scenarios.

In tests, if someone wants to test the `Shutdown` method, they can get a
pointer to the implementation of the component and explicitly call the
`Shutdown` method.
rgrandl added a commit that referenced this issue May 28, 2024
In the current implementation, we provide the user the ability to
initialize things by implementing an `Init` method. However, they don't
have the ability to do a clean shutdown easily.

The user filled multiple github issues (#275 and #765) asking for a `Shutdown`
method.

This PR adds a `Shutdown` method to each component. Note that there is
no guarantee that this method can run - e.g., the app receives a
SIGKILL because the machine suddenly crashes. However, a `Shutdown`
method can enable the user to achieve graceful shutdown in normal
scenarios.

In tests, if someone wants to test the `Shutdown` method, they can get a
pointer to the implementation of the component and explicitly call the
`Shutdown` method.
rgrandl added a commit that referenced this issue May 28, 2024
In the current implementation, we provide the user the ability to
initialize things by implementing an `Init` method. However, they don't
have the ability to do a clean shutdown easily.

The user filled multiple github issues (#275 and #765) asking for a `Shutdown`
method.

This PR adds a `Shutdown` method to each component. Note that there is
no guarantee that this method can run - e.g., the app receives a
SIGKILL because the machine suddenly crashes. However, a `Shutdown`
method can enable the user to achieve graceful shutdown in normal
scenarios.

In tests, if someone wants to test the `Shutdown` method, they can get a
pointer to the implementation of the component and explicitly call the
`Shutdown` method.
rgrandl added a commit that referenced this issue May 30, 2024
In the current implementation, we provide the user the ability to
initialize things by implementing an `Init` method. However, they don't
have the ability to do a clean shutdown easily.

The user filled multiple github issues (#275 and #765) asking for a `Shutdown`
method.

This PR adds a `Shutdown` method to each component. Note that there is
no guarantee that this method can run - e.g., the app receives a
SIGKILL because the machine suddenly crashes. However, a `Shutdown`
method can enable the user to achieve graceful shutdown in normal
scenarios.

In tests, if someone wants to test the `Shutdown` method, they can get a
pointer to the implementation of the component and explicitly call the
`Shutdown` method.
@rgrandl
Copy link
Contributor

rgrandl commented May 30, 2024

If you install the latest weaver version (v0.24.2) you should be able to add a Shutdown command to your application as mentioned in https://serviceweaver.dev/docs.html#components-interfaces.

@rgrandl rgrandl closed this as completed May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants