Skip to content

Commit

Permalink
Refactor slack messaging methods and add slack pkg consts for ASCII c…
Browse files Browse the repository at this point in the history
…hars and message levels
  • Loading branch information
rk1274 committed Sep 20, 2024
1 parent 872575c commit f3ae07d
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 118 deletions.
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
gas "github.com/wtsi-hgi/go-authserver"
"github.com/wtsi-hgi/ibackup/put"
"github.com/wtsi-hgi/ibackup/set"
"github.com/wtsi-hgi/ibackup/slack"
"github.com/wtsi-ssg/wrstat/v4/scheduler"
)

Expand Down Expand Up @@ -257,7 +258,7 @@ func (s *Server) ttrc(data interface{}) queue.SubQueue {
// stop is called when the server is Stop()ped, cleaning up our additional
// properties.
func (s *Server) stop() {
s.sendSlackMessage("🟧 server stopped") //nolint:errcheck
s.sendSlackMessage(slack.Warn, "server stopped") //nolint:errcheck

if s.serverAliveCh != nil {
close(s.serverAliveCh)
Expand Down
43 changes: 17 additions & 26 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/wtsi-hgi/ibackup/internal"
"github.com/wtsi-hgi/ibackup/put"
"github.com/wtsi-hgi/ibackup/set"
"github.com/wtsi-hgi/ibackup/slack"
)

const (
Expand All @@ -55,20 +56,6 @@ const (

var errNotDiscovered = errors.New("not discovered")

type mockSlacker struct {
logger *gas.StringLogger
}

func newMockSlacker(logger *gas.StringLogger) *mockSlacker {
return &mockSlacker{logger: logger}
}

func (s *mockSlacker) SendMessage(msg string) error {
_, err := s.logger.Write([]byte(msg))

return err
}

func TestClient(t *testing.T) {
Convey("maxTimeForUpload works with small and large requests", t, func() {
min := 10 * time.Millisecond
Expand Down Expand Up @@ -127,7 +114,7 @@ func TestServer(t *testing.T) {
slackWriter := gas.NewStringLogger()
conf := Config{
HTTPLogger: logWriter,
Slacker: newMockSlacker(slackWriter),
Slacker: slack.NewMock(slackWriter),
}

Convey("You can make a Server with a logger configured and no slacker", func() {
Expand Down Expand Up @@ -164,7 +151,8 @@ func TestServer(t *testing.T) {
So(err, ShouldBeNil)
})

const serverStartMessage = "⬜️ server starting, loading database🟩 server loaded database"
const serverStartMessage = slack.BoxPrefixInfo + "server starting, loading database" +
slack.BoxPrefixSuccess + "server loaded database"

makeAndStartServer := func() (*Server, string, func() error) {
s, errn := New(conf)
Expand Down Expand Up @@ -198,7 +186,7 @@ func TestServer(t *testing.T) {

time.Sleep(conf.StillRunningMsgFreq)

expectedMsg := "⬜️ server is still running"
expectedMsg := slack.BoxPrefixInfo + "server is still running"
So(slackWriter.String(), ShouldEqual, expectedMsg)

time.Sleep(conf.StillRunningMsgFreq)
Expand All @@ -210,7 +198,7 @@ func TestServer(t *testing.T) {

time.Sleep(conf.StillRunningMsgFreq)

So(slackWriter.String(), ShouldEqual, expectedMsg+expectedMsg+"🟧 server stopped")
So(slackWriter.String(), ShouldEqual, expectedMsg+expectedMsg+slack.BoxPrefixWarn+"server stopped")
})

Convey("You can make a Server with a logger configured and setup Auth, MakeQueueEndPoints and LoadSetDB", func() {
Expand All @@ -228,7 +216,7 @@ func TestServer(t *testing.T) {
errd := dfunc()
So(errd, ShouldBeNil)

So(slackWriter.String(), ShouldEndWith, "🟧 server stopped")
So(slackWriter.String(), ShouldContainSubstring, slack.BoxPrefixWarn+"server stopped")

slackWriter.Reset()
}()
Expand Down Expand Up @@ -333,7 +321,7 @@ func TestServer(t *testing.T) {
So(gotSet.NumFiles, ShouldEqual, 0)
So(gotSet.Symlinks, ShouldEqual, 0)

So(slackWriter.String(), ShouldEqual, "⬜️ `jim.set1` stored in db")
So(slackWriter.String(), ShouldEqual, slack.BoxPrefixInfo+"`jim.set1` stored in db")
slackWriter.Reset()

tn := time.Now()
Expand All @@ -357,7 +345,8 @@ func TestServer(t *testing.T) {
So(errg, ShouldBeNil)
So(len(entries), ShouldEqual, len(files)+len(discovers))

So(slackWriter.String(), ShouldEqual, fmt.Sprintf("⬜️ `jim.set1` completed discovery: %d files", len(entries)))
So(slackWriter.String(), ShouldEqual, fmt.Sprintf("%s`jim.set1` completed discovery: %d files",
slack.BoxPrefixInfo, len(entries)))

So(entries[0].Status, ShouldEqual, set.Pending)
So(entries[1].Status, ShouldEqual, set.Missing)
Expand Down Expand Up @@ -737,6 +726,8 @@ func TestServer(t *testing.T) {
err = client.TriggerDiscovery(emptySet.ID())
So(err, ShouldBeNil)

<-time.After(100 * time.Millisecond)

gotSet, err = client.GetSetByID(emptySet.Requester, emptySet.ID())
So(err, ShouldBeNil)
So(gotSet.Status, ShouldEqual, set.Complete)
Expand Down Expand Up @@ -1338,7 +1329,7 @@ func TestServer(t *testing.T) {

err = client.TriggerDiscovery(badSet2.ID())
So(err, ShouldNotBeNil)
So(slackWriter.String(), ShouldEqual, "🟥 `jim.setbad2` is invalid: invalid transformer")
So(slackWriter.String(), ShouldEqual, slack.BoxPrefixError+"`jim.setbad2` is invalid: invalid transformer")

slackWriter.Reset()

Expand All @@ -1352,8 +1343,8 @@ func TestServer(t *testing.T) {
So(errg, ShouldBeNil)
So(gotSet.Error, ShouldNotBeNil)
So(slackWriter.String(), ShouldEqual,
fmt.Sprintf("⬜️ `jim.setbad` completed discovery: 4 files"+
"🟥 `jim.setbad` is invalid: not a valid humgen lustre path [%s]", expected[0]))
fmt.Sprintf(slack.BoxPrefixInfo+"`jim.setbad` completed discovery: 4 files"+
slack.BoxPrefixError+"`jim.setbad` is invalid: not a valid humgen lustre path [%s]", expected[0]))

err = dfunc()
So(err, ShouldBeNil)
Expand All @@ -1372,7 +1363,7 @@ func TestServer(t *testing.T) {
So(logWriter.String(), ShouldEqual, fmt.Sprintf("failed to recover set setbad for jim: "+
"not a valid humgen lustre path [%s]\n", expected[0]))
So(slackWriter.String(), ShouldEqual, fmt.Sprintf(serverStartMessage+
"🟥 `jim.setbad` could not be recovered: "+
slack.BoxPrefixError+"`jim.setbad` could not be recovered: "+
"not a valid humgen lustre path [%s]", expected[0]))
})
})
Expand Down Expand Up @@ -1773,7 +1764,7 @@ func TestServer(t *testing.T) {

var forceSlowRead put.FileReadTester = func(ctx context.Context, path string) error {
if path == discovers[0] {
<-time.After(10 * time.Millisecond)
<-time.After(100 * time.Millisecond)
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions server/setdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/wtsi-hgi/grand"
"github.com/wtsi-hgi/ibackup/put"
"github.com/wtsi-hgi/ibackup/set"
"github.com/wtsi-hgi/ibackup/slack"
"github.com/wtsi-ssg/wrstat/v4/walk"
)

Expand Down Expand Up @@ -184,7 +185,7 @@ func (s *Server) LoadSetDB(path, backupPath string) error {
}

func (s *Server) setupDB(path, backupPath string, authGroup *gin.RouterGroup) error {
err := s.sendSlackMessage("⬜️ server starting, loading database")
err := s.sendSlackMessage(slack.Info, "server starting, loading database")
if err != nil {
return err
}
Expand All @@ -194,7 +195,7 @@ func (s *Server) setupDB(path, backupPath string, authGroup *gin.RouterGroup) er
return err
}

err = s.sendSlackMessage("🟩 server loaded database")
err = s.sendSlackMessage(slack.Success, "server loaded database")
if err != nil {
return err
}
Expand All @@ -210,12 +211,12 @@ func (s *Server) setupDB(path, backupPath string, authGroup *gin.RouterGroup) er
return nil
}

func (s *Server) sendSlackMessage(msg string) error {
func (s *Server) sendSlackMessage(level slack.Level, msg string) error {
if s.slacker == nil {
return nil
}

return s.slacker.SendMessage(msg)
return s.slacker.SendMessage(level, msg)
}

func (s *Server) tellSlackStillRunning() {
Expand Down Expand Up @@ -243,7 +244,7 @@ func (s *Server) tellSlackStillRunning() {
}

func (s *Server) serverStillRunning() error {
return s.slacker.SendMessage("⬜️ server is still running")
return s.slacker.SendMessage(slack.Info, "server is still running")
}

// EnableRemoteDBBackups causes the database backup file to also be backed up to
Expand Down
45 changes: 17 additions & 28 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import (
"github.com/dgryski/go-farm"
"github.com/dustin/go-humanize" //nolint:misspell
"github.com/wtsi-hgi/ibackup/put"
"github.com/wtsi-hgi/ibackup/slack"
)

type Status int

type Slacker interface {
SendMessage(msg string) error
SendMessage(level slack.Level, msg string) error
}

const (
Expand Down Expand Up @@ -396,7 +397,7 @@ func (s *Set) entryStatusToSetCounts(entry *Entry) error {
if entry.Attempts >= AttemptsToBeConsideredFailing {
s.Status = Failing

return s.messageError("has failed uploads")
return s.sendSlackMessage(slack.Error, "has failed uploads")
}
case Missing:
s.Missing++
Expand All @@ -407,16 +408,16 @@ func (s *Set) entryStatusToSetCounts(entry *Entry) error {
return nil
}

func (s *Set) messageError(msg string) error {
return s.createAndSendMessage("🟥", msg)
}

func (s *Set) createAndSendMessage(prefix, msg string) error {
func (s *Set) sendSlackMessage(level slack.Level, msg string) error {
if s.slacker == nil {
return nil
}

return s.slacker.SendMessage(fmt.Sprintf("%s `%s.%s` %s", prefix, s.Requester, s.Name, msg))
return s.slacker.SendMessage(level, s.createSlackMessage(msg))
}

func (s *Set) createSlackMessage(msg string) string {
return fmt.Sprintf("`%s.%s` %s", s.Requester, s.Name, msg)
}

func (s *Set) entryTypeToSetCounts(entry *Entry) {
Expand All @@ -437,11 +438,7 @@ func (s *Set) LogChangesToSlack(slacker Slacker) {
// SuccessfullyStoredInDB should be called when you successfully store the set
// in DB.
func (s *Set) SuccessfullyStoredInDB() error {
return s.messageInfo("stored in db")
}

func (s *Set) messageInfo(msg string) error {
return s.createAndSendMessage("⬜️", msg)
return s.sendSlackMessage(slack.Info, "stored in db")
}

// DiscoveryCompleted should be called when you complete discovering a set. Pass
Expand All @@ -454,16 +451,12 @@ func (s *Set) DiscoveryCompleted(numFiles uint64) error {
s.Status = Complete
s.LastCompleted = time.Now()

return s.messageWarn("completed discovery and backup due to no files")
return s.sendSlackMessage(slack.Warn, "completed discovery and backup due to no files")
}

s.Status = PendingUpload

return s.messageInfo(fmt.Sprintf("completed discovery: %d files", numFiles))
}

func (s *Set) messageWarn(msg string) error {
return s.createAndSendMessage("🟧", msg)
return s.sendSlackMessage(slack.Info, fmt.Sprintf("completed discovery: %d files", numFiles))
}

// UpdateBasedOnEntry updates set status values based on an updated Entry
Expand Down Expand Up @@ -495,7 +488,7 @@ func (s *Set) checkIfUploading() error {

s.Status = Uploading

return s.messageInfo("started uploading files")
return s.sendSlackMessage(slack.Info, "started uploading files")
}

func (s *Set) checkIfComplete() error {
Expand All @@ -508,15 +501,11 @@ func (s *Set) checkIfComplete() error {
s.LastCompletedCount = s.Uploaded + s.Failed
s.LastCompletedSize = s.SizeFiles

return s.messageSuccess(fmt.Sprintf("completed backup "+
return s.sendSlackMessage(slack.Success, fmt.Sprintf("completed backup "+
"(%d uploaded; %d failed; %d missing; %d abnormal; %s of data)",
s.Uploaded, s.Failed, s.Missing, s.Abnormal, s.Size()))
}

func (s *Set) messageSuccess(msg string) error {
return s.createAndSendMessage("🟩", msg)
}

// fixCounts resets the set counts to 0 and goes through all the entries for
// the set in the db to recaluclate them. The supplied entry should be one you
// newly updated and that wasn't in the db before the transaction we're in.
Expand Down Expand Up @@ -566,19 +555,19 @@ func (s *Set) updateAllCounts(entries []*Entry, entry *Entry) error {
func (s *Set) SetError(errMsg string) error {
s.Error = errMsg

return s.messageError("is invalid: " + errMsg)
return s.sendSlackMessage(slack.Error, "is invalid: "+errMsg)
}

// SetWarning records the given warning against the set, indicating it has an
// issue.
func (s *Set) SetWarning(warnMsg string) error {
s.Warning = warnMsg

return s.messageWarn("has an issue: " + warnMsg)
return s.sendSlackMessage(slack.Warn, "has an issue: "+warnMsg)
}

func (s *Set) RecoveryError(err error) error {
return s.messageError("could not be recovered: " + err.Error())
return s.sendSlackMessage(slack.Error, "could not be recovered: "+err.Error())
}

// copyUserProperties copies data from one set into another.
Expand Down
Loading

0 comments on commit f3ae07d

Please sign in to comment.