-
Notifications
You must be signed in to change notification settings - Fork 323
Add ops the ability to clean orphan commands #1434
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
Changes from 4 commits
dea8bbc
fabf13a
c7d7148
cf8d0dc
04a5385
105bbaa
c0aaaf5
bd2af05
0512b9f
68021e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["orphancommandcleaner.go"], | ||
| importpath = "github.com/pipe-cd/pipe/pkg/app/ops/orphancommandcleaner", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//pkg/datastore:go_default_library", | ||
| "//pkg/model:go_default_library", | ||
| "@org_uber_go_zap//:go_default_library", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package orphancommandcleaner | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/datastore" | ||
| "github.com/pipe-cd/pipe/pkg/model" | ||
| ) | ||
|
|
||
| var ( | ||
| commandTimeOut = 7 * 24 * time.Hour | ||
| interval = 24 * time.Hour | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What values should be set?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ) | ||
|
|
||
| type OrphanCommandCleaner struct { | ||
| commandstore datastore.CommandStore | ||
| logger *zap.Logger | ||
| } | ||
|
|
||
| func NewOrphanCommandCleaner( | ||
| ds datastore.DataStore, | ||
| logger *zap.Logger, | ||
| ) *OrphanCommandCleaner { | ||
| return &OrphanCommandCleaner{ | ||
| commandstore: datastore.NewCommandStore(ds), | ||
| logger: logger.Named("orphan-command-finder"), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| func (o *OrphanCommandCleaner) Run(ctx context.Context) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| for { | ||
| if err := o.updateOrphanCommandsStatus(ctx); err != nil { | ||
| o.logger.Error("failed to update orphan commands", zap.Error(err)) | ||
| } | ||
| time.Sleep(interval) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
| } | ||
| } | ||
|
|
||
| func (o *OrphanCommandCleaner) updateOrphanCommandsStatus(ctx context.Context) error { | ||
| timeoutedTime := time.Now().Add(-commandTimeOut).Unix() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| opts := datastore.ListOptions{ | ||
| Filters: []datastore.ListFilter{ | ||
| { | ||
| Field: "Status", | ||
| Operator: "==", | ||
| Value: model.CommandStatus_COMMAND_NOT_HANDLED_YET, | ||
| }, | ||
| { | ||
| Field: "CreatedAt", | ||
| Operator: "<=", | ||
| Value: timeoutedTime, | ||
| }, | ||
| }, | ||
| } | ||
| commands, err := o.commandstore.ListCommands(ctx, opts) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, c := range commands { | ||
| o.commandstore.UpdateCommand(ctx, c.Id, func(c *model.Command) error { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a log to show that the command was successfully updated or not? |
||
| c.Status = model.CommandStatus_COMMAND_TIMEOUT | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| 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.
This should be wrapped by
errgroupto terminateopswhencleanerexit because of an error.Uh oh!
There was an error while loading. Please reload this page.
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.
IMO, there was no need to stop ops because errors that occur in cleaner is probably caused by a network error when communicating with datastore.
Of course, it is necessary to output the error log to show that an error has occurred. However, I don't think it is necessary to stop ops because updateOrphanCommandsStatus will include the update that should have been done with the previous process if the previous process failed.
Or how about stopping
opsif it fails several times in a row?What do you think about it? 🤔
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.
Nice comment. I got your points.
Agree with you that the
cleanermay have some errors while executing its jobs.But that is the problem of
cleaner, not thisrunfunction.This
runshould not care about what each component is doing, it just cares that they are fine or not. If any of them had a problem,opsshould be stopped to notify that there was a problem from one of its components.In other words,
runshould see all of its components (cleaner,http-server,insightcollector...) in the same way.About the network problem or something that can cause the error while
cleanerexecuting the job,cleanercan ignore them, just log the error message, and does not return.By that way, the
opswill not be stopped by those errors and we can keeprunto be nice too.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.
OK, I see 👍