Skip to content

Commit

Permalink
fix: dont clear last_success upon sync failure (#52)
Browse files Browse the repository at this point in the history
* doc: update README

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>

* fix: dont clear last_success upon sync failure

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>

---------

Signed-off-by: Jian Zeng <anonymousknight96@gmail.com>
  • Loading branch information
knight42 committed Jan 13, 2024
1 parent 2309213 commit e41a8ef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -27,6 +27,7 @@ wget https://github.com/ustclug/Yuki/releases/latest/download/yukid_linux_amd64

sudo cp yukictl_linux_amd64 /usr/local/bin/yukictl
sudo cp yukid_linux_amd64 /usr/local/bin/yukid
sudo chmod +x /usr/local/bin/{yukid,yukictl}
```

Configure yukid:
Expand Down
3 changes: 1 addition & 2 deletions pkg/docker/fake/cli.go
Expand Up @@ -34,7 +34,7 @@ func (f *Client) WaitContainerWithTimeout(id string, timeout time.Duration) (int
f.mu.Lock()
defer f.mu.Unlock()

ct, ok := f.containers[id]
_, ok := f.containers[id]
if !ok {
return 0, fmt.Errorf("container %s not found", id)
}
Expand All @@ -44,7 +44,6 @@ func (f *Client) WaitContainerWithTimeout(id string, timeout time.Duration) (int
return 0, context.DeadlineExceeded
}
time.Sleep(delay)
f.containers[id] = ct
return 0, nil
}

Expand Down
15 changes: 7 additions & 8 deletions pkg/server/utils.go
Expand Up @@ -82,20 +82,19 @@ func (s *Server) waitForSync(name, ctID, storageDir string) {
l.Error("Fail to remove container", slogErrAttr(err))
}

var lastSuccess int64
updates := map[string]any{
"size": s.getSize(storageDir),
"exit_code": code,
"syncing": false,
}
if code == 0 {
lastSuccess = time.Now().Unix()
updates["last_success"] = time.Now().Unix()
}

err = s.db.
Model(&model.RepoMeta{}).
Where(model.RepoMeta{Name: name}).
Updates(map[string]any{
"size": s.getSize(storageDir),
"exit_code": code,
"last_success": lastSuccess,
"syncing": false,
}).Error
Updates(updates).Error
if err != nil {
l.Error("Fail to update RepoMeta", slogErrAttr(err))
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/server/utils_test.go
Expand Up @@ -128,3 +128,60 @@ func TestWaitRunningContainers(t *testing.T) {
return !meta.Syncing
})
}

func TestWaitForSync(t *testing.T) {
const name = "repo0"
t.Run("last_success should be updated", func(t *testing.T) {
te := NewTestEnv(t)
require.NoError(t, te.server.db.Create([]model.RepoMeta{
{
Name: name,
Syncing: true,
ExitCode: 2,
Size: 3,
},
}).Error)

id, err := te.server.dockerCli.RunContainer(context.TODO(), docker.RunContainerConfig{
Name: name,
})
require.NoError(t, err)
te.server.waitForSync(name, id, "")

meta := model.RepoMeta{Name: name}
require.NoError(t, te.server.db.Take(&meta).Error)
require.EqualValues(t, name, meta.Name)
require.EqualValues(t, -1, meta.Size)
require.False(t, meta.Syncing)
require.Empty(t, meta.ExitCode)
require.NotEmpty(t, meta.LastSuccess)
})

t.Run("last_success should not be updated upon sync failure", func(t *testing.T) {
te := NewTestEnv(t)
lastSuccess := time.Now().Add(-time.Hour * 24).Unix()
require.NoError(t, te.server.db.Create([]model.RepoMeta{
{
Name: name,
LastSuccess: lastSuccess,
ExitCode: 2,
Size: 3,
},
}).Error)

te.server.config.SyncTimeout = time.Second
id, err := te.server.dockerCli.RunContainer(context.TODO(), docker.RunContainerConfig{
Name: name,
})
require.NoError(t, err)
te.server.waitForSync(name, id, "")

meta := model.RepoMeta{Name: name}
require.NoError(t, te.server.db.Take(&meta).Error)
require.EqualValues(t, name, meta.Name)
require.EqualValues(t, -1, meta.Size)
require.False(t, meta.Syncing)
require.EqualValues(t, -2, meta.ExitCode)
require.EqualValues(t, lastSuccess, meta.LastSuccess)
})
}

0 comments on commit e41a8ef

Please sign in to comment.