Skip to content

fix: only check cred helper repo once per 24h #572

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
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/credentials/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
)

type CredentialHelperDirs struct {
RevisionFile, BinDir, RepoDir string
RevisionFile, LastCheckedFile, BinDir, RepoDir string
}

func GetCredentialHelperDirs(cacheDir string) CredentialHelperDirs {
return CredentialHelperDirs{
RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"),
BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"),
RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"),
RevisionFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "revision"),
LastCheckedFile: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "last-checked"),
BinDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "bin"),
RepoDir: filepath.Join(cacheDir, "repos", "gptscript-credential-helpers", "repo"),
}
}
19 changes: 19 additions & 0 deletions pkg/repos/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"
"sync"
"time"

"github.com/BurntSushi/locker"
"github.com/gptscript-ai/gptscript/pkg/config"
Expand Down Expand Up @@ -112,11 +113,29 @@ func (m *Manager) deferredSetUpCredentialHelpers(ctx context.Context, cliCfg *co
locker.Lock("gptscript-credential-helpers")
defer locker.Unlock("gptscript-credential-helpers")

// Load the last-checked file to make sure we haven't checked the repo in the last 24 hours.
lastChecked, err := os.ReadFile(m.credHelperDirs.LastCheckedFile)
if err == nil {
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(lastChecked))); err == nil && time.Since(t) < 24*time.Hour {
// Make sure the binary still exists, and if it does, return.
if _, err := os.Stat(filepath.Join(m.credHelperDirs.BinDir, "gptscript-credential-"+helperName+suffix)); err == nil {
log.Debugf("Not checking for new version of credential helper %s, last checked %v", helperName, t)
return nil
}
}
}

// Load the credential helpers repo information.
_, repo, _, err := github.Load(ctx, nil, credentialHelpersRepo)
if err != nil {
return err
}

// Update the last-checked file.
if err := os.WriteFile(m.credHelperDirs.LastCheckedFile, []byte(time.Now().Format(time.RFC3339)), 0644); err != nil {
return err
}

var needsBuild bool

// Check the last revision shasum and see if it is different from the current one.
Expand Down