-
Notifications
You must be signed in to change notification settings - Fork 204
refactor: service lifecycle into optional Init Run Shutdown #2021
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
refactor: service lifecycle into optional Init Run Shutdown #2021
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## reboot #2021 +/- ##
==========================================
- Coverage 91.50% 90.59% -0.91%
==========================================
Files 17 17
Lines 1000 999 -1
==========================================
- Hits 915 905 -10
- Misses 66 75 +9
Partials 19 19 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
87e9295
to
bb9452f
Compare
This commit modifies the start up behavior. It refactors the service lifecycle management, replacing the Start and Stop methods with * Init: used to initiliaze a service and can assume it isn't called from go routines * Run: Runs the service and blocks until context is cancelled or an error is encountered * `Shutdown`: methods is the cleanup and indicates that you can't call `Run` after `Shutdown`. Instead of mandating a service to have all Init, Run, Shutdown, services can now have any of these methods. Main calls Init if it exits, followed by Run (if it exits) and then Shutdown This change also aims to separate initialization which is run serially and allows services to initialize without worrying about thread-safety. I.E. Its okay for an Init() to call another Init(). Thus this change separates initialization, runtime, and cleanup phases for better clarity and control. Tests are updated keep up with the change. Signed-off-by: Sunil Thaha <[email protected]>
bb9452f
to
6c8bfd5
Compare
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls check if we need a context for initialization. no Init is using it, and it is getting cancelled immediately after all services initialized
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, we can add context to Init
if there is a need.
Can I address that in a separate patch so that the rest of the PRs can be merged?
func runServices(logger *slog.Logger, services []service.Service) error { | ||
logger.Info("Running all services") | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no cancel()
function call in func(error)
of g.Add
? as was case before this refactor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cancel gets called in waitForInterrupt
and is also deferred.
}, | ||
) | ||
} | ||
g.Add(waitForInterrupt(ctx, cancel, logger, os.Interrupt)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add handling for SIGTERM
. this is used in kubernetes to gracefully shutdown a pod.
err := <-errCh | ||
duration := time.Since(start) | ||
assert.NoError(t, err, "Run() should not return an error") | ||
assert.GreaterOrEqual(t, duration, 50*time.Millisecond, "Run() should run until the context is cancelled") | ||
|
||
err = meter.Stop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stop
can be removed since fake do need to implement service.Shutdown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fake implements CPUPowerMeter and isn't a service so to satisfy that interface, we need a Stop method.
Init(ctx context.Context) error | ||
|
||
// Run() power meter for reading energy or power | ||
Run(ctx context.Context) error | ||
|
||
// Stop() stops the power meter and releases any resources held | ||
Stop() error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to Shutdown
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea behind shutdown was to indicate that you can't start
after shutdown
where as for devices, I am not sure (at this point in time) if those should be treated as a Service. Any thoughts?
func (r *raplPowerMeter) Run(ctx context.Context) error { | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since real and fake power meter do not actually Run
and Stop/Shutdown
, no need to implement Runner
and Shutdown
. the runServices
checks for interface implementation anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PowerMeters aren't services. It implements PowerMeter so these are specific to that interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prometheus service does not implement Shutdown
. it can unregister the prometheus handler from server
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perometheus service does not implement Runner
👍🏽
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to unregister idea but with shutdown being called, there is no point in unregister since all services are being shutdown -right?
} | ||
|
||
// Shutdown is the interface that all services must implement that are to be shutdown / cleaned up | ||
type Shutdown interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change name to Shutdowner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:) Happy to do that. Separate PR?
// Init() initializes the power meter and makes it ready for use. This method | ||
// is not required to be thread-safe | ||
Init(ctx context.Context) error | ||
|
||
// Run() power meter for reading energy or power | ||
Run(ctx context.Context) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets make use of service.Runner and service.Initializer to replace the Init in CPUPowerMeter.
Power Meter will only return Zones()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
with few changes to be done in a followup PR.
2ad09ba
into
sustainable-computing-io:reboot
This commit addresses comments on sustainable-computing-io#2021 Simplify service lifecycle management by: - Removing context.Context from Initializer.Init() method - Renaming Shutdown to Shutdowner for clarity - Removing Init, Run, and Stop methods from powerMeter interface - Removing Shutdown from PowerMonitor and Stop from power meters - Refactoring createPowerMonitor into createServices Enhance signal handling by adding syscall.SIGTERM support in waitForInterrupt. Add debug logging for skipped service initialization and shutdown. Update tests to reflect simplified lifecycle management. Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
Changes include: * Introduce service.NewSignalHandler for SIGINT and SIGTERM, replacing custom signal handling. * Replace runServices and initServices with service.Run and service.Init for standardized lifecycle management. * Simplify powerMeter interface by removing Init, Run, and Stop methods. * Rename Shutdown to Shutdowner and remove context from Initializer.Init. * Inline createPowerMonitor into createServices and reorder services. * Add debug logging for skipped initializations and successful shutdowns. * Update tests to reflect simplified lifecycle and context removal. * Addresses comments on: sustainable-computing-io#2021 Signed-off-by: Sunil Thaha <[email protected]>
This commit modifies the start up behavior. It refactors the service lifecycle management, replacing the Start and Stop methods with
Init
: used to initiliaze a service and can assume it isn't called from go routines and must be called beforeRun
.Run
: Runs the service and blocks until context is cancelled or an error is encounteredShutdown
: method is the cleanup and indicates that you can't callRun
afterShutdown
.This change aims to separate initialization which is run serially and allows services to initialize without worrying about thread-safety. I.E. Its okay for an Init() to call another Init().
Thus, this change separates initialization, runtime, and cleanup phases for better clarity and control. All Methods are optional and main calls the methods if they are defined.
Tests are updated keep up with the change.