s_debug.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "time"
  5. )
  6. // ----------------------------------------------------------------------------------
  7. // Processor Name: debugger
  8. // ----------------------------------------------------------------------------------
  9. // Description : Log received emails
  10. // ----------------------------------------------------------------------------------
  11. // Config Options: log_reads bool - log if true
  12. // : sleep_seconds - how many seconds to pause for, useful to force a
  13. // : timeout. If sleep_seconds is 1 then a panic will be induced
  14. // --------------:-------------------------------------------------------------------
  15. // Input : email envelope
  16. // ----------------------------------------------------------------------------------
  17. // Output : none (only output to the log if enabled)
  18. // ----------------------------------------------------------------------------------
  19. func init() {
  20. Streamers["debug"] = func() *StreamDecorator {
  21. return StreamDebug()
  22. }
  23. }
  24. type streamDebuggerConfig struct {
  25. LogReads bool `json:"log_reads"`
  26. SleepSec int `json:"sleep_seconds,omitempty"`
  27. }
  28. func StreamDebug() *StreamDecorator {
  29. sd := &StreamDecorator{}
  30. var config streamDebuggerConfig
  31. var envelope *mail.Envelope
  32. sd.Configure = func(cfg ConfigGroup) error {
  33. return sd.ExtractConfig(cfg, &config)
  34. }
  35. sd.Decorate =
  36. func(sp StreamProcessor, a ...interface{}) StreamProcessor {
  37. sd.Open = func(e *mail.Envelope) error {
  38. envelope = e
  39. return nil
  40. }
  41. return StreamProcessWith(func(p []byte) (int, error) {
  42. if config.LogReads {
  43. Log().Fields("queuedID", envelope.QueuedId, "payload", string(p)).Info("debug stream")
  44. }
  45. if config.SleepSec > 0 {
  46. Log().Fields("queuedID", envelope.QueuedId, "sleep", config.SleepSec).Info("sleeping")
  47. time.Sleep(time.Second * time.Duration(config.SleepSec))
  48. Log().Fields("queuedID", envelope.QueuedId).Info("woke up")
  49. if config.SleepSec == 1 {
  50. panic("panic on purpose")
  51. }
  52. }
  53. return sp.Write(p)
  54. })
  55. }
  56. return sd
  57. }