Skip to content

Commit

Permalink
Adding uuid to log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
peteclark-ft committed Feb 17, 2017
1 parent e995cbb commit 3022124
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions resources/carousel_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Financial-Times/list-notifications-rw/db"
"github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
)

var generatedCarouselTidRegex = regexp.MustCompile(`^(tid_[\S]+)_carousel_[\d]{10}_gentx`)
Expand All @@ -21,14 +22,15 @@ func (f Filters) FilterCarouselPublishes(db db.DB) Filters {
func filterCarouselPublishes(db db.DB, next func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
tid := r.Header.Get(tidHeader)
uuid := mux.Vars(r)["uuid"]

if generatedCarouselTidRegex.MatchString(tid) {
logrus.WithField("transaction_id", tid).Info("Skipping generated carousel publish.")
writeMessage("Skipping generated carousel publish.", 200, w)
return
}

writeNotification, err := shouldWriteNotification(tid, db)
writeNotification, err := shouldWriteNotification(uuid, tid, db)
if err != nil {
writeMessage("An internal server error prevented processing of your request.", 500, w)
return
Expand All @@ -43,44 +45,44 @@ func filterCarouselPublishes(db db.DB, next func(w http.ResponseWriter, r *http.
}
}

func shouldWriteNotification(tid string, db db.DB) (bool, error) {
func shouldWriteNotification(uuid string, tid string, db db.DB) (bool, error) {
if !carouselTidRegex.MatchString(tid) {
return true, nil
}

logrus.WithField("transaction_id", tid).Infof("Received carousel notification.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).Infof("Received carousel notification.")
originalTid := carouselTidRegex.FindStringSubmatch(tid)[1]

tx, err := db.Open()
if err != nil {
logrus.WithError(err).Error("Failed to connect to mongo")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).WithError(err).Error("Failed to connect to mongo")
return false, err
}

defer tx.Close()

notifications, found, err := tx.FindNotification(originalTid)
if err != nil {
logrus.WithField("transaction_id", tid).WithError(err).Error("Failed to find original notification for this carousel publish! Writing new notification.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).WithError(err).Error("Failed to find original notification for this carousel publish! Writing new notification.")
return true, nil
}

if found {
logrus.WithField("transaction_id", tid).WithField("lastModified", (*notifications)[0].LastModified).Info("Skipping carousel publish; the original notification was published successfully.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).WithField("lastModified", (*notifications)[0].LastModified).Info("Skipping carousel publish; the original notification was published successfully.")
return false, nil
}

logrus.WithField("transaction_id", tid).Info("Failed to find notification for original transaction ID, checking for a related carousel transacation.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).Info("Failed to find notification for original transaction ID, checking for a related carousel transacation.")
notifications, found, err = tx.FindNotificationByPartialTransactionID(originalTid + "_carousel")
if err != nil {
logrus.WithField("transaction_id", tid).WithError(err).Error("Failed to find original notification for this carousel publish! Writing new notification.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).WithError(err).Error("Failed to find original notification for this carousel publish! Writing new notification.")
return true, nil
}

if !found {
return true, nil
}

logrus.WithField("transaction_id", tid).WithField("lastModified", (*notifications)[0].LastModified).Info("Skipping carousel publish; the original notification was published successfully.")
logrus.WithField("uuid", uuid).WithField("transaction_id", tid).WithField("lastModified", (*notifications)[0].LastModified).Info("Skipping carousel publish; the original notification was published successfully.")
return false, nil
}

0 comments on commit 3022124

Please sign in to comment.