Skip to content

Commit

Permalink
optimize job logging
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Oct 21, 2024
1 parent f42220d commit 37f2383
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 4 additions & 1 deletion handlers/job/script/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ func (s *ActionScript) warmUp(j *job.Job, m string, u string, su string, size in
}
for {
select {
case ev := <-ch:
case ev, ok := <-ch:
if !ok {
return
}
j.StatusUpdate(fmt.Sprintf("%v peers", ev.Peers))
case <-ctx2.Done():
return
Expand Down
25 changes: 14 additions & 11 deletions services/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ type Job struct {
storage Storage
main bool
purge bool
curLevel LogItemLevel
curStatus string
}

type LogItemLevel string
Expand Down Expand Up @@ -200,11 +198,13 @@ func (s *Job) logToLogger(l LogItem) {
log.WithFields(log.Fields{
"ID": s.ID,
"Queue": s.Queue,
"Main": s.main,
"Tag": l.Tag,
"Location": l.Location,
"Template": l.Template,
"Body": l.Body,
"Status": l.Status,
"Level": l.Level,
}).Log(levelMap[l.Level], message)
}

Expand All @@ -215,17 +215,13 @@ func (s *Job) log(l LogItem) error {
} else {
l.Tag = s.curTag
}
if l.Level == StatusUpdate && s.curTag == l.Tag && s.curLevel != InProgress && s.curLevel != StatusUpdate {
return nil
}
if l.Level == StatusUpdate && s.curTag == l.Tag && s.curStatus == l.Status {

replaced, ok := s.addToLog(l)

if !ok {
return nil
}

s.curLevel = l.Level
s.curStatus = l.Status
replaced := s.addToLog(l)

if s.main {
err := s.pubToStorage(replaced, l)
if err != nil {
Expand All @@ -242,17 +238,24 @@ func (s *Job) log(l LogItem) error {
return nil
}

func (s *Job) addToLog(l LogItem) (replaced bool) {
func (s *Job) addToLog(l LogItem) (replaced bool, ok bool) {
s.logMux.Lock()
defer s.logMux.Unlock()
if len(s.l) > 0 {
last := s.l[len(s.l)-1]
if l.Level == StatusUpdate && last.Tag == l.Tag && last.Level != InProgress && last.Level != StatusUpdate {
return false, false
}
if l.Level == StatusUpdate && last.Tag == l.Tag && last.Status == l.Status {
return false, false
}
if last.Level == StatusUpdate && s.curTag == last.Tag {
s.l = s.l[:len(s.l)-1]
replaced = true
}
}
s.l = append(s.l, l)

return
}

Expand Down

0 comments on commit 37f2383

Please sign in to comment.