wire icon indicating copy to clipboard operation
wire copied to clipboard

Can wire help us to close server in order?

Open somniapotato opened this issue 3 years ago • 3 comments

For example, if my provider like this: wire.Build(InitServer1, InitServer2, InitServer3) Server3 depends on Server2 Server2 depends on Server1

When the whole server want to gracefully stop, can wire help us to close Server3 first, then Server2, Server1? Considering that wire already know the injection sequence, I thought it could know the quit sequence too.

somniapotato avatar Feb 23 '22 09:02 somniapotato

Hey, @somniapotato did you find any workflow that works for you? There are Cleanup functions but I'm thinking is it good for graceful stop or is there some limitations

MistaTwista avatar Sep 22 '22 13:09 MistaTwista

This is very useful. Now when I exit gracefully, Redis or DB will be closed first (because they were created first), but other components depend on them, causing errors in other components, failing to exit gracefully, and often triggering errors or panics

shuqingzai avatar Apr 03 '23 15:04 shuqingzai

@MistaTwista I tried testing cleanup functionality and you still have to manually call the cleanups. I think what @somniapotato is trying to say here is to automatically call the cleanups.

Correct me if I am wrong here @MistaTwista

This is what I tried.

package main

import (
	"fmt"

	"github.com/google/wire"
)

type Foo struct {
	Name string
}

func NewProvider() (Foo, func()) {
	return Foo{Name: "hello!"}, func() {
		fmt.Println("running cleanup for Foo!")
	}
}

func main() {
	foo, cleanup := InitializeEvent()
	cleanup()
	fmt.Println("The name is: ", foo.Name)
}

func InitializeEvent() (Foo, func()) {
	wire.Build(NewProvider)
	return Foo{}, func() {}
}

ProgrammingMuffin avatar Dec 04 '23 08:12 ProgrammingMuffin