Add MirrorStatus.LastStarted property

- status.Status is in PreSyncing, and
- curStatus.Status is not in PreSyncing
This commit is contained in:
Phy 2020-04-04 22:34:07 -04:00
parent b8edc1f714
commit c1641b6714
No known key found for this signature in database
GPG Key ID: D475AA55A8144239
6 changed files with 116 additions and 85 deletions

View File

@ -8,16 +8,17 @@ import (
// A MirrorStatus represents a msg when // A MirrorStatus represents a msg when
// a worker has done syncing // a worker has done syncing
type MirrorStatus struct { type MirrorStatus struct {
Name string `json:"name"` Name string `json:"name"`
Worker string `json:"worker"` Worker string `json:"worker"`
IsMaster bool `json:"is_master"` IsMaster bool `json:"is_master"`
Status SyncStatus `json:"status"` Status SyncStatus `json:"status"`
LastUpdate time.Time `json:"last_update"` LastUpdate time.Time `json:"last_update"`
LastEnded time.Time `json:"last_ended"` LastStarted time.Time `json:"last_started"`
Scheduled time.Time `json:"next_schedule"` LastEnded time.Time `json:"last_ended"`
Upstream string `json:"upstream"` Scheduled time.Time `json:"next_schedule"`
Size string `json:"size"` Upstream string `json:"upstream"`
ErrorMsg string `json:"error_msg"` Size string `json:"size"`
ErrorMsg string `json:"error_msg"`
} }
// A WorkerStatus is the information struct that describe // A WorkerStatus is the information struct that describe

View File

@ -38,31 +38,35 @@ func (t *stampTime) UnmarshalJSON(b []byte) error {
// WebMirrorStatus is the mirror status to be shown in the web page // WebMirrorStatus is the mirror status to be shown in the web page
type WebMirrorStatus struct { type WebMirrorStatus struct {
Name string `json:"name"` Name string `json:"name"`
IsMaster bool `json:"is_master"` IsMaster bool `json:"is_master"`
Status SyncStatus `json:"status"` Status SyncStatus `json:"status"`
LastUpdate textTime `json:"last_update"` LastUpdate textTime `json:"last_update"`
LastUpdateTs stampTime `json:"last_update_ts"` LastUpdateTs stampTime `json:"last_update_ts"`
LastEnded textTime `json:"last_ended"` LastStarted textTime `json:"last_started"`
LastEndedTs stampTime `json:"last_ended_ts"` LastStartedTs stampTime `json:"last_started_ts"`
Scheduled textTime `json:"next_schedule"` LastEnded textTime `json:"last_ended"`
ScheduledTs stampTime `json:"next_schedule_ts"` LastEndedTs stampTime `json:"last_ended_ts"`
Upstream string `json:"upstream"` Scheduled textTime `json:"next_schedule"`
Size string `json:"size"` // approximate size ScheduledTs stampTime `json:"next_schedule_ts"`
Upstream string `json:"upstream"`
Size string `json:"size"` // approximate size
} }
func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus { func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus {
return WebMirrorStatus{ return WebMirrorStatus{
Name: m.Name, Name: m.Name,
IsMaster: m.IsMaster, IsMaster: m.IsMaster,
Status: m.Status, Status: m.Status,
LastUpdate: textTime{m.LastUpdate}, LastUpdate: textTime{m.LastUpdate},
LastUpdateTs: stampTime{m.LastUpdate}, LastUpdateTs: stampTime{m.LastUpdate},
LastEnded: textTime{m.LastEnded}, LastStarted: textTime{m.LastStarted},
LastEndedTs: stampTime{m.LastEnded}, LastStartedTs: stampTime{m.LastStarted},
Scheduled: textTime{m.Scheduled}, LastEnded: textTime{m.LastEnded},
ScheduledTs: stampTime{m.Scheduled}, LastEndedTs: stampTime{m.LastEnded},
Upstream: m.Upstream, Scheduled: textTime{m.Scheduled},
Size: m.Size, ScheduledTs: stampTime{m.Scheduled},
Upstream: m.Upstream,
Size: m.Size,
} }
} }

View File

@ -15,16 +15,18 @@ func TestStatus(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
t := time.Date(2016, time.April, 16, 23, 8, 10, 0, loc) t := time.Date(2016, time.April, 16, 23, 8, 10, 0, loc)
m := WebMirrorStatus{ m := WebMirrorStatus{
Name: "tunalinux", Name: "tunalinux",
Status: Success, Status: Success,
LastUpdate: textTime{t}, LastUpdate: textTime{t},
LastUpdateTs: stampTime{t}, LastUpdateTs: stampTime{t},
LastEnded: textTime{t}, LastStarted: textTime{t},
LastEndedTs: stampTime{t}, LastStartedTs: stampTime{t},
Scheduled: textTime{t}, LastEnded: textTime{t},
ScheduledTs: stampTime{t}, LastEndedTs: stampTime{t},
Size: "5GB", Scheduled: textTime{t},
Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/", ScheduledTs: stampTime{t},
Size: "5GB",
Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/",
} }
b, err := json.Marshal(m) b, err := json.Marshal(m)
@ -40,6 +42,10 @@ func TestStatus(t *testing.T) {
So(m2.LastUpdateTs.Unix(), ShouldEqual, m.LastUpdate.Unix()) So(m2.LastUpdateTs.Unix(), ShouldEqual, m.LastUpdate.Unix())
So(m2.LastUpdate.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano()) So(m2.LastUpdate.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
So(m2.LastUpdateTs.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano()) So(m2.LastUpdateTs.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
So(m2.LastStarted.Unix(), ShouldEqual, m.LastStarted.Unix())
So(m2.LastStartedTs.Unix(), ShouldEqual, m.LastStarted.Unix())
So(m2.LastStarted.UnixNano(), ShouldEqual, m.LastStarted.UnixNano())
So(m2.LastStartedTs.UnixNano(), ShouldEqual, m.LastStarted.UnixNano())
So(m2.LastEnded.Unix(), ShouldEqual, m.LastEnded.Unix()) So(m2.LastEnded.Unix(), ShouldEqual, m.LastEnded.Unix())
So(m2.LastEndedTs.Unix(), ShouldEqual, m.LastEnded.Unix()) So(m2.LastEndedTs.Unix(), ShouldEqual, m.LastEnded.Unix())
So(m2.LastEnded.UnixNano(), ShouldEqual, m.LastEnded.UnixNano()) So(m2.LastEnded.UnixNano(), ShouldEqual, m.LastEnded.UnixNano())
@ -53,15 +59,16 @@ func TestStatus(t *testing.T) {
}) })
Convey("BuildWebMirrorStatus should work", t, func() { Convey("BuildWebMirrorStatus should work", t, func() {
m := MirrorStatus{ m := MirrorStatus{
Name: "arch-sync3", Name: "arch-sync3",
Worker: "testWorker", Worker: "testWorker",
IsMaster: true, IsMaster: true,
Status: Failed, Status: Failed,
LastUpdate: time.Now().Add(-time.Minute * 30), LastUpdate: time.Now().Add(-time.Minute * 30),
LastEnded: time.Now(), LastStarted: time.Now().Add(-time.Minute * 1),
Scheduled: time.Now().Add(time.Minute * 5), LastEnded: time.Now(),
Upstream: "mirrors.tuna.tsinghua.edu.cn", Scheduled: time.Now().Add(time.Minute * 5),
Size: "4GB", Upstream: "mirrors.tuna.tsinghua.edu.cn",
Size: "4GB",
} }
var m2 WebMirrorStatus var m2 WebMirrorStatus
@ -73,6 +80,10 @@ func TestStatus(t *testing.T) {
So(m2.LastUpdateTs.Unix(), ShouldEqual, m.LastUpdate.Unix()) So(m2.LastUpdateTs.Unix(), ShouldEqual, m.LastUpdate.Unix())
So(m2.LastUpdate.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano()) So(m2.LastUpdate.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
So(m2.LastUpdateTs.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano()) So(m2.LastUpdateTs.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
So(m2.LastStarted.Unix(), ShouldEqual, m.LastStarted.Unix())
So(m2.LastStartedTs.Unix(), ShouldEqual, m.LastStarted.Unix())
So(m2.LastStarted.UnixNano(), ShouldEqual, m.LastStarted.UnixNano())
So(m2.LastStartedTs.UnixNano(), ShouldEqual, m.LastStarted.UnixNano())
So(m2.LastEnded.Unix(), ShouldEqual, m.LastEnded.Unix()) So(m2.LastEnded.Unix(), ShouldEqual, m.LastEnded.Unix())
So(m2.LastEndedTs.Unix(), ShouldEqual, m.LastEnded.Unix()) So(m2.LastEndedTs.Unix(), ShouldEqual, m.LastEnded.Unix())
So(m2.LastEnded.UnixNano(), ShouldEqual, m.LastEnded.UnixNano()) So(m2.LastEnded.UnixNano(), ShouldEqual, m.LastEnded.UnixNano())

View File

@ -78,34 +78,37 @@ func TestBoltAdapter(t *testing.T) {
Convey("update mirror status", func() { Convey("update mirror status", func() {
status := []MirrorStatus{ status := []MirrorStatus{
MirrorStatus{ MirrorStatus{
Name: "arch-sync1", Name: "arch-sync1",
Worker: testWorkerIDs[0], Worker: testWorkerIDs[0],
IsMaster: true, IsMaster: true,
Status: Success, Status: Success,
LastUpdate: time.Now(), LastUpdate: time.Now(),
LastEnded: time.Now(), LastStarted: time.Now().Add(-time.Minute),
Upstream: "mirrors.tuna.tsinghua.edu.cn", LastEnded: time.Now(),
Size: "3GB", Upstream: "mirrors.tuna.tsinghua.edu.cn",
Size: "3GB",
}, },
MirrorStatus{ MirrorStatus{
Name: "arch-sync2", Name: "arch-sync2",
Worker: testWorkerIDs[1], Worker: testWorkerIDs[1],
IsMaster: true, IsMaster: true,
Status: Disabled, Status: Disabled,
LastUpdate: time.Now().Add(-time.Hour), LastUpdate: time.Now().Add(-time.Hour),
LastEnded: time.Now(), LastStarted: time.Now().Add(-time.Minute),
Upstream: "mirrors.tuna.tsinghua.edu.cn", LastEnded: time.Now(),
Size: "4GB", Upstream: "mirrors.tuna.tsinghua.edu.cn",
Size: "4GB",
}, },
MirrorStatus{ MirrorStatus{
Name: "arch-sync3", Name: "arch-sync3",
Worker: testWorkerIDs[1], Worker: testWorkerIDs[1],
IsMaster: true, IsMaster: true,
Status: Success, Status: Success,
LastUpdate: time.Now().Add(-time.Second), LastUpdate: time.Now().Add(-time.Minute),
LastEnded: time.Now(), LastStarted: time.Now().Add(-time.Second),
Upstream: "mirrors.tuna.tsinghua.edu.cn", LastEnded: time.Now(),
Size: "4GB", Upstream: "mirrors.tuna.tsinghua.edu.cn",
Size: "4GB",
}, },
} }

View File

@ -297,14 +297,21 @@ func (s *Manager) updateJobOfWorker(c *gin.Context) {
curStatus, _ := s.adapter.GetMirrorStatus(workerID, mirrorName) curStatus, _ := s.adapter.GetMirrorStatus(workerID, mirrorName)
curTime := time.Now()
if status.Status == PreSyncing && curStatus.Status != PreSyncing {
status.LastStarted = curTime
} else {
status.LastStarted = curStatus.LastStarted
}
// Only successful syncing needs last_update // Only successful syncing needs last_update
if status.Status == Success { if status.Status == Success {
status.LastUpdate = time.Now() status.LastUpdate = curTime
} else { } else {
status.LastUpdate = curStatus.LastUpdate status.LastUpdate = curStatus.LastUpdate
} }
if status.Status == Success || status.Status == Failed { if status.Status == Success || status.Status == Failed {
status.LastEnded = time.Now() status.LastEnded = curTime
} else { } else {
status.LastEnded = curStatus.LastEnded status.LastEnded = curStatus.LastEnded
} }

View File

@ -151,6 +151,7 @@ func TestHTTPServer(t *testing.T) {
So(m.Size, ShouldEqual, status.Size) So(m.Size, ShouldEqual, status.Size)
So(m.IsMaster, ShouldEqual, status.IsMaster) So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastUpdate), ShouldBeLessThan, 1*time.Second)
So(time.Now().Sub(m.LastStarted), ShouldBeLessThan, 2*time.Minute)
So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second)
}) })
@ -168,6 +169,7 @@ func TestHTTPServer(t *testing.T) {
So(m.Size, ShouldEqual, status.Size) So(m.Size, ShouldEqual, status.Size)
So(m.IsMaster, ShouldEqual, status.IsMaster) So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate.Time), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastUpdate.Time), ShouldBeLessThan, 1*time.Second)
So(time.Now().Sub(m.LastStarted.Time), ShouldBeLessThan, 2*time.Minute)
So(time.Now().Sub(m.LastEnded.Time), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastEnded.Time), ShouldBeLessThan, 1*time.Second)
}) })
@ -198,6 +200,7 @@ func TestHTTPServer(t *testing.T) {
So(m.Size, ShouldEqual, "5GB") So(m.Size, ShouldEqual, "5GB")
So(m.IsMaster, ShouldEqual, status.IsMaster) So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastUpdate), ShouldBeLessThan, 1*time.Second)
So(time.Now().Sub(m.LastStarted), ShouldBeLessThan, 2*time.Minute)
So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second)
}) })
}) })
@ -251,6 +254,7 @@ func TestHTTPServer(t *testing.T) {
So(m.Size, ShouldEqual, status.Size) So(m.Size, ShouldEqual, status.Size)
So(m.IsMaster, ShouldEqual, status.IsMaster) So(m.IsMaster, ShouldEqual, status.IsMaster)
So(time.Now().Sub(m.LastUpdate), ShouldBeGreaterThan, 3*time.Second) So(time.Now().Sub(m.LastUpdate), ShouldBeGreaterThan, 3*time.Second)
So(time.Now().Sub(m.LastStarted), ShouldBeGreaterThan, 1*time.Minute)
So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second) So(time.Now().Sub(m.LastEnded), ShouldBeLessThan, 1*time.Second)
}) })
}) })
@ -258,14 +262,15 @@ func TestHTTPServer(t *testing.T) {
Convey("update mirror status of an inexisted worker", func(ctx C) { Convey("update mirror status of an inexisted worker", func(ctx C) {
invalidWorker := "test_worker2" invalidWorker := "test_worker2"
status := MirrorStatus{ status := MirrorStatus{
Name: "arch-sync2", Name: "arch-sync2",
Worker: invalidWorker, Worker: invalidWorker,
IsMaster: true, IsMaster: true,
Status: Success, Status: Success,
LastUpdate: time.Now(), LastUpdate: time.Now(),
LastEnded: time.Now(), LastStarted: time.Now(),
Upstream: "mirrors.tuna.tsinghua.edu.cn", LastEnded: time.Now(),
Size: "4GB", Upstream: "mirrors.tuna.tsinghua.edu.cn",
Size: "4GB",
} }
resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s", resp, err := PostJSON(fmt.Sprintf("%s/workers/%s/jobs/%s",
baseURL, status.Worker, status.Name), status, nil) baseURL, status.Worker, status.Name), status, nil)