Skip to content

Commit

Permalink
Enable sync policy for extra remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
bhou-crto committed Dec 31, 2024
1 parent 90dc41d commit f56cdd8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Check the update of %s and its commands.
Timeout: updateFlags.Timeout,
EnableCI: enableCI,
PackageLockFile: packageLockFile,
SyncPolicy: "always", // TODO: use constant instead of string
}
cmdUpdater.CheckUpdateAsync()
err := cmdUpdater.Update()
Expand Down
3 changes: 2 additions & 1 deletion internal/backend/package-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewManagedSource(name, repoDir, remoteBaseURL string, syncPolicy string) *P
RemoteBaseURL: remoteBaseURL,
RemoteRegistryURL: fmt.Sprintf("%s/index.json", remoteBaseURL),
IsManaged: true,
SyncPolicy: SYNC_POLICY_ALWAYS,
SyncPolicy: syncPolicy,
}
}

Expand All @@ -71,6 +71,7 @@ func (src *PackageSource) InitUpdater(user *user.User, timeout time.Duration, en
PackageLockFile: lockFile,
VerifyChecksum: verifyChecksum,
VerifySignature: verifySignature,
SyncPolicy: src.SyncPolicy,
}
return src.Updater
}
Expand Down
77 changes: 77 additions & 0 deletions internal/updater/cmd-updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package updater

import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"sync"
"time"

Expand Down Expand Up @@ -34,6 +37,7 @@ type CmdUpdater struct {
PackageLockFile string
VerifyChecksum bool
VerifySignature bool
SyncPolicy string
}

func (u *CmdUpdater) CheckUpdateAsync() {
Expand All @@ -57,6 +61,16 @@ func (u *CmdUpdater) Update() error {

errPool := []error{}

// check if we are following the syncPolicy
// TODO: for now we check the sync policy to block update during the update phase,
// This is no optimal, as we still check remote repository in check update async.
// We should move the sync policy check to the check update async, which will save
// time to check the remote repo in order to make the check done in the timeout period.
if err := u.reachSyncSchedule(); err != nil {
log.Info(err.Error())
return err
}

remoteRepo, err := u.getRemoteRepository()
if err != nil {
// TODO: handle error here
Expand Down Expand Up @@ -138,6 +152,12 @@ func (u *CmdUpdater) Update() error {
}

if len(errPool) == 0 {
// update the sync timestamp
err := u.UpdateSyncTimestamp()
if err != nil {
log.Error(err)
}

fmt.Println("Update done! Enjoy coding!")
return nil
} else {
Expand Down Expand Up @@ -265,3 +285,60 @@ func (u *CmdUpdater) LoadLockedPackages(lockFile string) (map[string]string, err
}
return lockedPkgs, nil
}

// check sync policy
func (u *CmdUpdater) reachSyncSchedule() error {
// check if we are following the syncPolicy
if u.SyncPolicy == "never" {
return errors.New(fmt.Sprintf("Remote '%s': Sync policy is set to never, no update will be performed", u.LocalRepo.Name()))
}
// now load the sync timestamp
localRepoFolder, err := u.LocalRepo.RepositoryFolder()
if err != nil {
return err
}
data, err := os.ReadFile(path.Join(localRepoFolder, "sync.timestamp"))
if err != nil {
// error read the file, we assume the sync time is passed
return nil
}
syncTime, err := time.Parse(time.RFC3339, string(data))
if err != nil {
return err
}

// now check if we passed the sync time
if time.Now().Before(syncTime) {
return errors.New(fmt.Sprintf("Remote '%s': Not yet reach the sync time", u.LocalRepo.Name()))
}

return nil
}

func (u *CmdUpdater) UpdateSyncTimestamp() error {
localRepoFolder, err := u.LocalRepo.RepositoryFolder()
if err != nil {
return err
}

var delay time.Duration = 24
switch u.SyncPolicy {
case "always":
return errors.New(fmt.Sprintf("Remote '%s': Sync policy is set to always, no need to update the sync timestamp", u.LocalRepo.Name()))
case "never":
return errors.New(fmt.Sprintf("Remote '%s': Sync policy is set to never, no need to update the sync timestamp", u.LocalRepo.Name()))
case "hourly":
delay = 1
case "daily":
delay = 24
case "weekly":
delay = 24 * 7
case "monthly":
delay = 24 * 30
}

err = os.WriteFile(path.Join(localRepoFolder, "sync.timestamp"), []byte(time.Now().Add(time.Hour*delay).Format(time.RFC3339)), 0644)

log.Infof("Remote '%s': Sync timestamp updated to %s", u.LocalRepo.Name(), time.Now().Add(time.Hour*delay).Format(time.RFC3339))
return err
}

0 comments on commit f56cdd8

Please sign in to comment.