purge.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package web
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "time"
  7. "github.com/gin-contrib/sessions"
  8. "github.com/gin-gonic/gin"
  9. "code.osinet.fr/fgm/sqs_demo/back/services/redriver"
  10. )
  11. func makePurgeHandler(rd redriver.Redriver) gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. ctx := c.Request.Context()
  14. qName := c.Param("name")
  15. redirect := "/queue/" + qName
  16. sess := sessions.Default(c)
  17. // Do not consume sess.Flashes(): this is a redirect-only handler, and they would be lost.
  18. defer func() {
  19. _ = sess.Save()
  20. c.Redirect(http.StatusSeeOther, redirect)
  21. }()
  22. t0 := time.Now()
  23. err := rd.Purge(ctx, qName)
  24. latency := time.Since(t0)
  25. if err != nil {
  26. log.Printf("failed purging queue %q: %v", qName, err)
  27. sess.AddFlash(fmt.Sprintf("Failed purging queue %q", qName))
  28. return
  29. }
  30. sess.AddFlash(fmt.Sprintf("Purged submitted for queue %s in %v", qName,
  31. latency))
  32. sess.AddFlash("The actual purge process takes up to 60 seconds, " +
  33. "so not be surprised if some messages are still visible on the queue messsages list." +
  34. "We recommend waiting for 60 seconds to refresh, regardless of your queue's size.")
  35. }
  36. }