regarding the Stop comments: i believe we can simplify (and clarify) the whole startup/cleanup of services using context. it would require a small refactor but i think it's worth it.
right now we have this in prod
|
|
|
// shutdownServices gracefully shuts down all services |
|
func shutdownServices(services *Services) { |
|
if services == nil { |
|
return |
|
} |
|
// Stop services in reverse order of startup |
|
if services.Sequencer != nil { |
|
services.Sequencer.Stop() |
|
} |
|
if services.API != nil { |
|
services.API.Stop() |
|
} |
|
if services.CensusDownloader != nil { |
|
services.CensusDownloader.Stop() |
|
} |
|
if services.ProcessMon != nil { |
|
services.ProcessMon.Stop() |
|
} |
|
services.TxManager.Stop() // Stop transaction manager |
|
services.Storage.Close() // Close storage last |
|
} |
and even this in tests
|
// Create a combined cleanup function |
|
cleanup := func() { |
|
seqCancel() |
|
api.Stop() |
|
stateSync.Stop() |
|
cd.Stop() |
|
pm.Stop() |
|
vp.Stop() |
|
stg.Close() |
|
c3cleanup() |
|
web3Cleanup() |
|
} |
but those funcs just basically do a ctx.Cancel, can't we just handle that through the parent context anyway?
sequencer creates a single parent context, where all those services live. (test setup follows a similar pattern, obviously)
|
// Create context with cancellation for graceful shutdown |
|
ctx, cancel := context.WithCancel(context.Background()) |
|
defer cancel() |
|
|
|
// Setup services |
|
services, err := setupServices(ctx, cfg) |
|
if err != nil { |
cancelling the parent context, cancels all derived contexts.
and whatever cleanup needed, could simply happen on <-ctx.Done
Originally posted by @altergui in #284 (comment)
regarding the Stop comments: i believe we can simplify (and clarify) the whole startup/cleanup of services using context. it would require a small refactor but i think it's worth it.
right now we have this in prod
davinci-node/cmd/davinci-sequencer/main.go
Lines 296 to 317 in 4d77454
and even this in tests
davinci-node/tests/helpers_test.go
Lines 573 to 584 in 4d77454
but those funcs just basically do a ctx.Cancel, can't we just handle that through the parent context anyway?
sequencer creates a single parent context, where all those services live. (test setup follows a similar pattern, obviously)
davinci-node/cmd/davinci-sequencer/main.go
Lines 64 to 70 in 4d77454
cancelling the parent context, cancels all derived contexts.
and whatever cleanup needed, could simply happen on <-ctx.Done
Originally posted by @altergui in #284 (comment)