Repository navigation
feat(pluginkit): keep each plugin's state and pause a plugin that fails at boot #143
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
225018b
test(pluginkit): pin how a boot keeps each plugin's state and pauses …
SirLouen ff8de90
feat(pluginkit): define the plugin store and the contract every store…
SirLouen bd5da33
feat(pluginkit): carry a boot from the plugin records to the start an…
SirLouen 4e5213b
docs(pluginkit): list the plugin records changes under Unreleased
SirLouen d9262b4
test(pluginkit): run the boot scenarios
SirLouen ab2b509
fix(pluginkit): pause nothing when the context ends while plugins reg…
SirLouen 794b231
test(pluginkit): check that a store keeps every other record when it …
SirLouen 002822a
test(pluginkit): pin a context that ends while plugins register and n…
SirLouen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,106 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package pluginkit | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // Boot is one start of the application's plugins, from their records to their start. | ||
| type Boot struct { | ||
| store PluginStore | ||
| records map[string]PluginRecord | ||
| } | ||
|
|
||
| // NewBoot reads every record of store once. | ||
| func NewBoot(ctx context.Context, store PluginStore) (*Boot, error) { | ||
| var records map[string]PluginRecord | ||
| err := guarded(ctx, "read the plugin records", func(ctx context.Context) error { | ||
| var readErr error | ||
| records, readErr = store.Records(ctx) | ||
| return readErr | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &Boot{store: store, records: records}, nil | ||
| } | ||
|
|
||
| // Register records each new entry active, registers the active and paused ones, and pauses each that fails. | ||
| func Register[D, P any](ctx context.Context, boot *Boot, entries []Registration[D, P], | ||
| depsFor func(id string) D) (RegisterOutcome[P], error) { | ||
| wanted, err := admitted(ctx, boot, entries) | ||
| if err != nil { | ||
| return RegisterOutcome[P]{}, err | ||
| } | ||
| outcome := RegisterEach(wanted, depsFor) | ||
| return outcome, boot.pauseEach(ctx, outcome.Failed) | ||
| } | ||
|
|
||
| // admitted returns the entries boot registers, in order, recording each new one active. | ||
| func admitted[D, P any](ctx context.Context, boot *Boot, entries []Registration[D, P]) ([]Registration[D, P], error) { | ||
| var wanted []Registration[D, P] | ||
| for _, entry := range entries { | ||
| registers, err := boot.admit(ctx, entry.ID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if registers { | ||
| wanted = append(wanted, entry) | ||
| } | ||
| } | ||
| return wanted, nil | ||
| } | ||
|
|
||
| // pauseEach pauses each failed plugin but one whose migration record failed, and none once ctx ended. | ||
| func (b *Boot) pauseEach(ctx context.Context, failures []Failure) error { | ||
| if err := ctx.Err(); err != nil { | ||
| return fmt.Errorf("pluginkit: the boot's context ended: %w", err) | ||
| } | ||
| for _, failure := range failures { | ||
| if failure.Step == StepRecord { | ||
| continue | ||
| } | ||
| if err := b.Pause(ctx, failure.ID, failure.Err); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // admit records the plugin id active when boot has never seen it, and reports whether a boot registers it. | ||
| func (b *Boot) admit(ctx context.Context, id string) (bool, error) { | ||
| if record, known := b.records[id]; known { | ||
| return record.State == PluginActive || record.State == PluginPaused, nil | ||
| } | ||
| return true, b.activate(ctx, id) | ||
| } | ||
|
|
||
| // Pause pauses the plugin id for failure, a step that failed or a reason of the application's own. | ||
| func (b *Boot) Pause(ctx context.Context, id string, failure error) error { | ||
| return guarded(ctx, "pause "+id, func(ctx context.Context) error { | ||
| return b.store.Pause(ctx, id, failure.Error()) | ||
| }) | ||
| } | ||
|
|
||
| // activate marks the plugin id active and clears its error. | ||
| func (b *Boot) activate(ctx context.Context, id string) error { | ||
| return guarded(ctx, "activate "+id, func(ctx context.Context) error { return b.store.Activate(ctx, id) }) | ||
| } | ||
|
|
||
| // Started pauses each plugin that failed to migrate or start, and activates each paused plugin that started. | ||
| func (b *Boot) Started(ctx context.Context, outcome StartOutcome) error { | ||
| if err := b.pauseEach(ctx, outcome.Failed); err != nil { | ||
| return err | ||
| } | ||
| for _, started := range outcome.Started { | ||
| if b.records[started.ID].State != PluginPaused { | ||
| continue | ||
| } | ||
| if err := b.activate(ctx, started.ID); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.