Skip to content

Commit e769c24

Browse files
author
Gusted
committed
Improve loop behavior
1 parent 74325d0 commit e769c24

File tree

4 files changed

+71
-42
lines changed

4 files changed

+71
-42
lines changed

models/issue_stopwatch.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, ex
6666
return
6767
}
6868

69+
// UserIDCount is a simple coalition of UserID and Count
70+
type UserStopwatch struct {
71+
UserID int64
72+
StopWatches []*Stopwatch
73+
}
74+
75+
// GetUIDsAndNotificationCounts between the two provided times
76+
func GetUIDsAndStopwatch() ([]*UserStopwatch, error) {
77+
sws := []*Stopwatch{}
78+
if err := db.GetEngine(db.DefaultContext).Find(&sws); err != nil {
79+
return nil, err
80+
}
81+
if len(sws) == 0 {
82+
return []*UserStopwatch{}, nil
83+
}
84+
85+
lastUserID := int64(-1)
86+
res := []*UserStopwatch{}
87+
for _, sw := range sws {
88+
if lastUserID == sw.UserID {
89+
lastUserStopwatch := res[len(res)-1]
90+
lastUserStopwatch.StopWatches = append(lastUserStopwatch.StopWatches, sw)
91+
} else {
92+
res = append(res, &UserStopwatch{
93+
UserID: sw.UserID,
94+
StopWatches: []*Stopwatch{sw},
95+
})
96+
}
97+
}
98+
return res, nil
99+
}
100+
69101
// GetUserStopwatches return list of all stopwatches of a user
70102
func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
71103
sws := make([]*Stopwatch, 0, 8)

modules/eventsource/manager_run.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"time"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/convert"
1213
"code.gitea.io/gitea/modules/graceful"
14+
"code.gitea.io/gitea/modules/json"
1315
"code.gitea.io/gitea/modules/log"
1416
"code.gitea.io/gitea/modules/setting"
1517
"code.gitea.io/gitea/modules/timeutil"
@@ -63,6 +65,29 @@ loop:
6365
}
6466
}
6567

68+
usersStopwatches, err := models.GetUIDsAndStopwatch()
69+
if err != nil {
70+
log.Error("Unable to get GetUIDsAndStopwatch: %v", err)
71+
return
72+
}
73+
74+
for _, userStopwatches := range usersStopwatches {
75+
apiSWs, err := convert.ToStopWatches(userStopwatches.StopWatches)
76+
if err != nil {
77+
log.Error("Unable to APIFormat stopwatches: %v", err)
78+
continue
79+
}
80+
dataBs, err := json.Marshal(apiSWs)
81+
if err != nil {
82+
log.Error("Unable to marshal stopwatches: %v", err)
83+
continue
84+
}
85+
m.SendMessage(userStopwatches.UserID, &Event{
86+
Name: "stopwatches",
87+
Data: string(dataBs),
88+
})
89+
}
90+
6691
now := timeutil.TimeStampNow().Add(-2)
6792

6893
uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)

routers/web/events/events.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ import (
88
"net/http"
99
"time"
1010

11-
"code.gitea.io/gitea/models"
12-
"code.gitea.io/gitea/models/db"
1311
"code.gitea.io/gitea/modules/context"
14-
"code.gitea.io/gitea/modules/convert"
1512
"code.gitea.io/gitea/modules/eventsource"
1613
"code.gitea.io/gitea/modules/graceful"
17-
"code.gitea.io/gitea/modules/json"
1814
"code.gitea.io/gitea/modules/log"
19-
"code.gitea.io/gitea/modules/setting"
2015
"code.gitea.io/gitea/routers/web/auth"
2116
)
2217

@@ -71,9 +66,6 @@ func Events(ctx *context.Context) {
7166

7267
timer := time.NewTicker(30 * time.Second)
7368

74-
stopwatchTimer := time.NewTicker(setting.UI.Notification.MinTimeout)
75-
prevStopwatchEmpty := false
76-
7769
loop:
7870
for {
7971
select {
@@ -94,40 +86,6 @@ loop:
9486
case <-shutdownCtx.Done():
9587
go unregister()
9688
break loop
97-
case <-stopwatchTimer.C:
98-
sws, err := models.GetUserStopwatches(ctx.User.ID, db.ListOptions{})
99-
if err != nil {
100-
log.Error("Unable to GetUserStopwatches: %v", err)
101-
continue
102-
}
103-
if len(sws) == 0 {
104-
if prevStopwatchEmpty {
105-
continue
106-
}
107-
prevStopwatchEmpty = true
108-
} else {
109-
prevStopwatchEmpty = false
110-
}
111-
apiSWs, err := convert.ToStopWatches(sws)
112-
if err != nil {
113-
log.Error("Unable to APIFormat stopwatches: %v", err)
114-
continue
115-
}
116-
dataBs, err := json.Marshal(apiSWs)
117-
if err != nil {
118-
log.Error("Unable to marshal stopwatches: %v", err)
119-
continue
120-
}
121-
_, err = (&eventsource.Event{
122-
Name: "stopwatches",
123-
Data: string(dataBs),
124-
}).WriteTo(ctx.Resp)
125-
if err != nil {
126-
log.Error("Unable to write to EventStream for user %s: %v", ctx.User.Name, err)
127-
go unregister()
128-
break loop
129-
}
130-
ctx.Resp.Flush()
13189
case event, ok := <-messageChan:
13290
if !ok {
13391
break loop

routers/web/repo/issue_stopwatch.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/models/db"
1213
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/eventsource"
1315
)
1416

1517
// IssueStopwatch creates or stops a stopwatch for the given issue.
@@ -59,6 +61,18 @@ func CancelStopwatch(c *context.Context) {
5961
return
6062
}
6163

64+
stopwatches, err := models.GetUserStopwatches(c.User.ID, db.ListOptions{})
65+
if err != nil {
66+
c.ServerError("GetUserStopwatches", err)
67+
return
68+
}
69+
if len(stopwatches) == 0 {
70+
eventsource.GetManager().SendMessage(c.User.ID, &eventsource.Event{
71+
Name: "stopwatches",
72+
Data: "{}",
73+
})
74+
}
75+
6276
url := issue.HTMLURL()
6377
c.Redirect(url, http.StatusSeeOther)
6478
}

0 commit comments

Comments
 (0)