-
Notifications
You must be signed in to change notification settings - Fork 465
[Feat] Pre exechook #990
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
base: master
Are you sure you want to change the base?
[Feat] Pre exechook #990
Changes from all commits
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 |
|---|---|---|
|
|
@@ -230,6 +230,16 @@ func main() { | |
| envDuration(3*time.Second, "GITSYNC_EXECHOOK_BACKOFF", "GIT_SYNC_EXECHOOK_BACKOFF"), | ||
| "the time to wait before retrying a failed exechook") | ||
|
|
||
| flPreExechookCommand := pflag.String("pre-exechook-command", | ||
| envString("", "GITSYNC_PRE_EXECHOOK_COMMAND", "GIT_SYNC_PRE_EXECHOOK_COMMAND"), | ||
| "an optional command to be run before syncs complete (must be idempotent)") | ||
| flPreExechookTimeout := pflag.Duration("pre-exechook-timeout", | ||
| envDuration(30*time.Second, "GITSYNC_PRE_EXECHOOK_TIMEOUT", "GIT_SYNC_PRE_EXECHOOK_TIMEOUT"), | ||
| "the timeout for the pre-exechook") | ||
| flPreExechookBackoff := pflag.Duration("pre-exechook-backoff", | ||
| envDuration(3*time.Second, "GITSYNC_PRE_EXECHOOK_BACKOFF", "GIT_SYNC_PRE_EXECHOOK_BACKOFF"), | ||
| "the time to wait before retrying a failed pre-exechook") | ||
|
|
||
| flWebhookURL := pflag.String("webhook-url", | ||
| envString("", "GITSYNC_WEBHOOK_URL", "GIT_SYNC_WEBHOOK_URL"), | ||
| "a URL for optional webhook notifications when syncs complete (must be idempotent)") | ||
|
|
@@ -541,6 +551,15 @@ func main() { | |
| } | ||
| } | ||
|
|
||
| if *flPreExechookCommand != "" { | ||
| if *flPreExechookTimeout < time.Second { | ||
| fatalConfigErrorf(log, true, "invalid flag: --pre-exechook-timeout must be at least 1s") | ||
| } | ||
| if *flPreExechookBackoff < time.Second { | ||
| fatalConfigErrorf(log, true, "invalid flag: --pre-exechook-backoff must be at least 1s") | ||
| } | ||
| } | ||
|
|
||
| if *flWebhookURL != "" { | ||
| if *flWebhookStatusSuccess == -1 { | ||
| // Back-compat: -1 and 0 mean the same things | ||
|
|
@@ -859,8 +878,10 @@ func main() { | |
| // Startup exechooks goroutine | ||
| var exechookRunner *hook.HookRunner | ||
| if *flExechookCommand != "" { | ||
| log := log.WithName("exechook") | ||
| logname := "exechook" | ||
| log := log.WithName(logname) | ||
| exechook := hook.NewExechook( | ||
| logname, | ||
| cmd.NewRunner(log), | ||
| *flExechookCommand, | ||
| func(hash string) string { | ||
|
|
@@ -880,6 +901,32 @@ func main() { | |
| go exechookRunner.Run(context.Background()) | ||
| } | ||
|
|
||
| // Startup pre-exechooks goroutine | ||
| var preExechookRunner *hook.HookRunner | ||
| if *flPreExechookCommand != "" { | ||
| logname := "pre-exechook" | ||
| log := log.WithName(logname) | ||
| exechook := hook.NewExechook( | ||
| logname, | ||
| cmd.NewRunner(log), | ||
| *flPreExechookCommand, | ||
| func(hash string) string { | ||
| return git.worktreeFor(hash).Path().String() | ||
| }, | ||
| []string{}, | ||
| *flPreExechookTimeout, | ||
| log, | ||
| ) | ||
| preExechookRunner = hook.NewHookRunner( | ||
| exechook, | ||
| *flPreExechookBackoff, | ||
| hook.NewHookData(), | ||
| log, | ||
| *flOneTime, | ||
| ) | ||
| go preExechookRunner.Run(context.Background()) | ||
| } | ||
|
|
||
| // Setup signal notify channel | ||
| sigChan := make(chan os.Signal, 1) | ||
| if syncSig != 0 { | ||
|
|
@@ -935,6 +982,7 @@ func main() { | |
| syncCount := uint64(0) | ||
| initialSyncDone := false | ||
| waitTime := *flInitPeriod | ||
| prevHash := "" | ||
| // getMaxFailures returns the effective max-failure limit for the current | ||
| // phase. During the initial sync phase, --init-max-failures (if set) | ||
| // overrides --max-failures; otherwise --max-failures applies. | ||
|
|
@@ -948,6 +996,10 @@ func main() { | |
| start := time.Now() | ||
| ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout) | ||
|
|
||
| if preExechookRunner != nil { | ||
| preExechookRunner.Send(prevHash) | ||
| } | ||
|
|
||
| if changed, hash, err := git.SyncRepo(ctx, refreshCreds); err != nil { | ||
| failCount++ | ||
| updateSyncMetrics(metricKeyError, start) | ||
|
|
@@ -981,6 +1033,9 @@ func main() { | |
| if exechookRunner != nil { | ||
| exechookRunner.Send(hash) | ||
| } | ||
| if preExechookRunner != nil { | ||
| prevHash = hash | ||
| } | ||
| updateSyncMetrics(metricKeySuccess, start) | ||
| } else { | ||
| updateSyncMetrics(metricKeyNoOp, start) | ||
|
|
@@ -2528,6 +2583,21 @@ OPTIONS | |
| The timeout for the --exechook-command. If not specifid, this | ||
| defaults to 30 seconds ("30s"). | ||
|
|
||
| --pre-exechook-backoff <duration>, $GITSYNC_PRE_EXECHOOK_BACKOFF | ||
|
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. Flags should be in alphabetic-sorted order, sorry |
||
| The time to wait before retrying a failed --pre-exechook-command. If | ||
| not specified, this defaults to 3 seconds ("3s"). | ||
|
|
||
| --pre-exechook-command <string>, $GITSYNC_PRE_EXECHOOK_COMMAND | ||
| An optional command to be executed before syncing a new hash of the | ||
|
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. This isn't quite right. I think we should update the docs on """ Then this can be: """ The rest should the same. What do you think? |
||
| remote repository. This command does not take any arguments and | ||
| executes with the synced repo as its working directory. The | ||
| $GITSYNC_HASH environment variable will be set to the previous git hash that | ||
| was synced. This hook will always be invoked as it runs before any sync attempt. | ||
|
|
||
| --pre-exechook-timeout <duration>, $GITSYNC_PRE_EXECHOOK_TIMEOUT | ||
| The timeout for the --pre-exechook-command. If not specifid, this | ||
| defaults to 30 seconds ("30s"). | ||
|
|
||
| --filter <string>, $GITSYNC_FILTER | ||
| Use partial clone with the specified filter. This can reduce | ||
| the amount of data transferred when cloning large repositories. | ||
|
|
||
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 doesn't seem right? This will send the old hash on every loop, then sync a new hash. Unless I totally misunderstood the request, you want to try to sync, and ONLY IF there is a new hash, call the hook before publishing the symlink.
Did I misunderstand?
Unfortunately, the sync and the publish are inside
git.SyncRepo. You can either decompose that (which would need to share a bunch of state) or you could pass function pointers into it like we do forrefreshCreds- pass a struct like: