-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/services: add MultiStart for staring multiple services together (#…
…7501) * core/services: add MultiStart for staring multiple services together * core/services/chainlink: use MultiStart in ChainlinkApplication to clean up failed start * sigscanner: attempt to fix auth * Update .github/workflows/sigscanner.yml Co-authored-by: chainchad <96362174+chainchad@users.noreply.github.com> Co-authored-by: chainchad <96362174+chainchad@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
149 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/multierr" | ||
) | ||
|
||
// StartClose is a subset of the ServiceCtx interface. | ||
type StartClose interface { | ||
Start(context.Context) error | ||
Close() error | ||
} | ||
|
||
// MultiStart is a utility for starting multiple services together. | ||
// The set of started services is tracked internally, so that they can be closed if any single service fails to start. | ||
type MultiStart struct { | ||
started []StartClose | ||
} | ||
|
||
// Start attempts to Start all services. If any service fails to start, the previously started services will be | ||
// Closed, and an error returned. | ||
func (m *MultiStart) Start(ctx context.Context, srvcs ...StartClose) (err error) { | ||
for _, s := range srvcs { | ||
err = m.start(ctx, s) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (m *MultiStart) start(ctx context.Context, s StartClose) (err error) { | ||
err = s.Start(ctx) | ||
if err != nil { | ||
err = multierr.Append(err, m.Close()) | ||
} else { | ||
m.started = append(m.started, s) | ||
} | ||
return | ||
} | ||
|
||
// Close closes all started services, in reverse order. | ||
func (m *MultiStart) Close() (err error) { | ||
for i := len(m.started) - 1; i >= 0; i-- { | ||
s := m.started[i] | ||
err = multierr.Append(err, s.Close()) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type Healthy string | ||
|
||
func (h Healthy) Start(ctx context.Context) error { | ||
fmt.Println(h, "started") | ||
return nil | ||
} | ||
|
||
func (h Healthy) Close() error { | ||
fmt.Println(h, "closed") | ||
return nil | ||
} | ||
|
||
type CloseFailure string | ||
|
||
func (c CloseFailure) Start(ctx context.Context) error { | ||
fmt.Println(c, "started") | ||
return nil | ||
} | ||
|
||
func (c CloseFailure) Close() error { | ||
fmt.Println(c, "close failure") | ||
return fmt.Errorf("failed to close: %s", c) | ||
} | ||
|
||
type WontStart string | ||
|
||
func (f WontStart) Start(ctx context.Context) error { | ||
fmt.Println(f, "start failure") | ||
return fmt.Errorf("failed to start: %s", f) | ||
} | ||
|
||
func (f WontStart) Close() error { | ||
fmt.Println(f, "close failure") | ||
return fmt.Errorf("cannot call Close after failed Start: %s", f) | ||
} | ||
|
||
func ExampleMultiStart() { | ||
ctx := context.Background() | ||
|
||
a := Healthy("a") | ||
b := CloseFailure("b") | ||
c := WontStart("c") | ||
|
||
var ms MultiStart | ||
if err := ms.Start(ctx, a, b, c); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// Output: | ||
// a started | ||
// b started | ||
// c start failure | ||
// b close failure | ||
// a closed | ||
// failed to start: c; failed to close: b | ||
} |