Add runner periodic cleanup check

Adds a periodic cleanup function that cross checks runners between github,
the provider and the GARM database. If an inconsistency is found, GARM will
attempt to fix it.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-01 13:35:56 +00:00
parent 73340da322
commit 059734f064
12 changed files with 479 additions and 43 deletions

View file

@ -490,6 +490,30 @@ func (g *githubClient) GithubBaseURL() *url.URL {
return g.cli.BaseURL
}
func NewRateLimitClient(ctx context.Context, credentials params.GithubCredentials) (common.RateLimitClient, error) {
httpClient, err := credentials.GetHTTPClient(ctx)
if err != nil {
return nil, errors.Wrap(err, "fetching http client")
}
slog.DebugContext(
ctx, "creating rate limit client",
"base_url", credentials.APIBaseURL,
"upload_url", credentials.UploadBaseURL)
ghClient, err := github.NewClient(httpClient).WithEnterpriseURLs(
credentials.APIBaseURL, credentials.UploadBaseURL)
if err != nil {
return nil, errors.Wrap(err, "fetching github client")
}
cli := &githubClient{
rateLimit: ghClient.RateLimit,
cli: ghClient,
}
return cli, nil
}
func Client(ctx context.Context, entity params.GithubEntity) (common.GithubClient, error) {
// func GithubClient(ctx context.Context, entity params.GithubEntity) (common.GithubClient, error) {
httpClient, err := entity.Credentials.GetHTTPClient(ctx)

View file

@ -141,16 +141,17 @@ func (m *MessageSession) maybeRefreshToken(ctx context.Context) error {
if m.session == nil {
return fmt.Errorf("session is nil")
}
// add some jitter
randInt, err := rand.Int(rand.Reader, big.NewInt(5000))
if err != nil {
return fmt.Errorf("failed to get a random number")
}
expiresAt, err := m.session.ExiresAt()
if err != nil {
return fmt.Errorf("failed to get expires at: %w", err)
}
expiresIn := time.Duration(randInt.Int64())*time.Millisecond + 10*time.Minute
// add some jitter (30 second interval)
randInt, err := rand.Int(rand.Reader, big.NewInt(30))
if err != nil {
return fmt.Errorf("failed to get a random number")
}
expiresIn := time.Duration(randInt.Int64())*time.Second + 10*time.Minute
slog.DebugContext(ctx, "checking if message session token needs refresh", "expires_at", expiresAt)
if m.session.ExpiresIn(expiresIn) {
if err := m.Refresh(ctx); err != nil {