Skip to content

Commit

Permalink
feat:[close #1] Keep articles cache updated in background
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Aug 26, 2023
1 parent 3240a23 commit ba96e60
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 26 deletions.
1 change: 1 addition & 0 deletions config/chronos.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"port": "8080",
"backgroundCacheUpdate": "true",
"gitRepos": [
{
"id": "remote1",
Expand Down
35 changes: 33 additions & 2 deletions core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/go-git/go-git/v5"
)

func synGitRepo(repo string) error {
func synGitRepo(repo string, force bool) error {
repoDir := reposDir + strings.ReplaceAll(repo, "/", "_")

if _, err := os.Stat(repoDir); os.IsNotExist(err) {
Expand All @@ -43,7 +43,13 @@ func synGitRepo(repo string) error {
origin := remotes[0].Config().URLs[0]

if origin != repo {
confirmation := askForConfirmation("The Git repository has been modified. Do you want to overwrite the current one?")
var confirmation bool

if !force {
confirmation = askForConfirmation("The Git repository has been modified. Do you want to overwrite the current one?")
} else {
confirmation = true
}

if confirmation {
err := os.RemoveAll(repoDir)
Expand Down Expand Up @@ -74,3 +80,28 @@ func synGitRepo(repo string) error {

return nil
}

func detectGitChanges(repo string) (bool, error) {
repoDir := reposDir + strings.ReplaceAll(repo, "/", "_")

r, err := git.PlainOpen(repoDir)
if err != nil {
return false, fmt.Errorf("failed to open Git repository: %v", err)
}

w, err := r.Worktree()
if err != nil {
return false, fmt.Errorf("failed to open Git worktree: %v", err)
}

status, err := w.Status()
if err != nil {
return false, fmt.Errorf("failed to get Git status: %v", err)
}

if status.IsClean() {
return false, nil
}

return true, nil
}
64 changes: 46 additions & 18 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/vanilla-os/Chronos/settings"
"github.com/vanilla-os/Chronos/structs"
Expand All @@ -33,32 +34,25 @@ func LoadChronos() {

prepareCache()

err := prepareRepos()
if err != nil {
panic(err)
if settings.Cnf.BackgroundCacheUpdate {
go backgroundCacheUpdate(15 * time.Minute)
}
}

// prepareRepos prepares both local and Git repositories.
func prepareRepos() error {
func prepareRepos(needSyncGit bool) error {
var repos []structs.Repo
var err error

cRepos, err := cacheManager.Get(context.Background(), "Repos")
if err == nil {
fmt.Println("Repos cache found")
err = json.Unmarshal(cRepos, &repos)
if err != nil {
return err
}
} else {
fmt.Println("Repos cache not found, resetting")
}
fmt.Println("Preparing Git repositories cache")

for _, repo := range settings.Cnf.GitRepos {
fmt.Printf("Synchronizing Git repository: %s\n", repo.Url)
err := synGitRepo(repo.Url)
if err != nil {
return fmt.Errorf("failed to synchronize Git repository: %v", err)
if needSyncGit {
fmt.Printf("Synchronizing Git repository: %s\n", repo.Url)
err := synGitRepo(repo.Url, false)
if err != nil {
return fmt.Errorf("failed to synchronize Git repository: %v", err)
}
}

_repo := structs.Repo{
Expand All @@ -84,6 +78,8 @@ func prepareRepos() error {
repos = append(repos, _repo)
}

fmt.Println("Preparing local repositories cache")

for _, repo := range settings.Cnf.LocalRepos {
_repo := structs.Repo{
Id: repo.Id,
Expand Down Expand Up @@ -115,9 +111,41 @@ func prepareRepos() error {

cacheManager.Set(context.Background(), "Repos", reposBytes)

fmt.Printf("Finished preparing repositories cache: %d repos\n", len(repos))

return nil
}

// backgroundCacheUpdate updates the cache in the background.
func backgroundCacheUpdate(interval time.Duration) {
for {
fmt.Printf("\nStarting background cache update")

for _, repo := range settings.Cnf.GitRepos {
changed, err := detectGitChanges(repo.Url)
if err != nil {
fmt.Printf("Failed to detect Git changes: %v\n", err)
}

if changed {
err := synGitRepo(repo.Url, true)
if err != nil {
fmt.Printf("Failed to synchronize Git repository: %v\n", err)
}
}
}

err := prepareRepos(false)
if err != nil {
panic(err)
}

fmt.Println("Finished background cache update")

time.Sleep(interval)
}
}

// getRepoLanguages populates the language cache.
func getRepoLanguages(repo structs.Repo) ([]string, error) {
langs, err := loadLanguagesFromRepo(repo)
Expand Down
14 changes: 8 additions & 6 deletions settings/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
)

type Config struct {
Port string `json:"port"`
GitRepos []ConfigGitRepo `json:"gitRepos"`
LocalRepos []ConfigLocalRepo `json:"localRepos"`
Port string `json:"port"`
GitRepos []ConfigGitRepo `json:"gitRepos"`
LocalRepos []ConfigLocalRepo `json:"localRepos"`
BackgroundCacheUpdate bool `json:"backgroundCacheUpdate"`
}

type ConfigGitRepo struct {
Expand Down Expand Up @@ -58,8 +59,9 @@ func init() {
}

Cnf = &Config{
Port: viper.GetString("port"),
GitRepos: gitRepos,
LocalRepos: localRepos,
Port: viper.GetString("port"),
GitRepos: gitRepos,
LocalRepos: localRepos,
BackgroundCacheUpdate: viper.GetBool("backgroundCacheUpdate"),
}
}

0 comments on commit ba96e60

Please sign in to comment.