server_manager.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package server
  2. import (
  3. "heckel.io/ntfy/log"
  4. "heckel.io/ntfy/util"
  5. "strings"
  6. )
  7. func (s *Server) execManager() {
  8. // WARNING: Make sure to only selectively lock with the mutex, and be aware that this
  9. // there is no mutex for the entire function.
  10. // Prune all the things
  11. s.pruneVisitors()
  12. s.pruneTokens()
  13. s.pruneAttachments()
  14. s.pruneMessages()
  15. // Message count per topic
  16. var messagesCached int
  17. messageCounts, err := s.messageCache.MessageCounts()
  18. if err != nil {
  19. log.Tag(tagManager).Err(err).Warn("Cannot get message counts")
  20. messageCounts = make(map[string]int) // Empty, so we can continue
  21. }
  22. for _, count := range messageCounts {
  23. messagesCached += count
  24. }
  25. // Remove subscriptions without subscribers
  26. var emptyTopics, subscribers int
  27. log.
  28. Tag(tagManager).
  29. Timing(func() {
  30. s.mu.Lock()
  31. defer s.mu.Unlock()
  32. for _, t := range s.topics {
  33. subs, lastAccess := t.Stats()
  34. ev := log.Tag(tagManager).With(t)
  35. if t.Stale() {
  36. if ev.IsTrace() {
  37. ev.Trace("- topic %s: Deleting stale topic (%d subscribers, accessed %s)", t.ID, subs, util.FormatTime(lastAccess))
  38. }
  39. emptyTopics++
  40. delete(s.topics, t.ID)
  41. } else {
  42. if ev.IsTrace() {
  43. ev.Trace("- topic %s: %d subscribers, accessed %s", t.ID, subs, util.FormatTime(lastAccess))
  44. }
  45. subscribers += subs
  46. }
  47. }
  48. }).
  49. Debug("Removed %d empty topic(s)", emptyTopics)
  50. // Mail stats
  51. var receivedMailTotal, receivedMailSuccess, receivedMailFailure int64
  52. if s.smtpServerBackend != nil {
  53. receivedMailTotal, receivedMailSuccess, receivedMailFailure = s.smtpServerBackend.Counts()
  54. }
  55. var sentMailTotal, sentMailSuccess, sentMailFailure int64
  56. if s.smtpSender != nil {
  57. sentMailTotal, sentMailSuccess, sentMailFailure = s.smtpSender.Counts()
  58. }
  59. // Print stats
  60. s.mu.Lock()
  61. messagesCount, topicsCount, visitorsCount := s.messages, len(s.topics), len(s.visitors)
  62. s.mu.Unlock()
  63. log.
  64. Tag(tagManager).
  65. Fields(log.Context{
  66. "messages_published": messagesCount,
  67. "messages_cached": messagesCached,
  68. "topics_active": topicsCount,
  69. "subscribers": subscribers,
  70. "visitors": visitorsCount,
  71. "emails_received": receivedMailTotal,
  72. "emails_received_success": receivedMailSuccess,
  73. "emails_received_failure": receivedMailFailure,
  74. "emails_sent": sentMailTotal,
  75. "emails_sent_success": sentMailSuccess,
  76. "emails_sent_failure": sentMailFailure,
  77. }).
  78. Info("Server stats")
  79. }
  80. func (s *Server) pruneVisitors() {
  81. staleVisitors := 0
  82. log.
  83. Tag(tagManager).
  84. Timing(func() {
  85. s.mu.Lock()
  86. defer s.mu.Unlock()
  87. for ip, v := range s.visitors {
  88. if v.Stale() {
  89. log.Tag(tagManager).With(v).Trace("Deleting stale visitor")
  90. delete(s.visitors, ip)
  91. staleVisitors++
  92. }
  93. }
  94. }).
  95. Field("stale_visitors", staleVisitors).
  96. Debug("Deleted %d stale visitor(s)", staleVisitors)
  97. }
  98. func (s *Server) pruneTokens() {
  99. if s.userManager != nil {
  100. log.
  101. Tag(tagManager).
  102. Timing(func() {
  103. if err := s.userManager.RemoveExpiredTokens(); err != nil {
  104. log.Tag(tagManager).Err(err).Warn("Error expiring user tokens")
  105. }
  106. if err := s.userManager.RemoveDeletedUsers(); err != nil {
  107. log.Tag(tagManager).Err(err).Warn("Error deleting soft-deleted users")
  108. }
  109. }).
  110. Debug("Removed expired tokens and users")
  111. }
  112. }
  113. func (s *Server) pruneAttachments() {
  114. if s.fileCache == nil {
  115. return
  116. }
  117. log.
  118. Tag(tagManager).
  119. Timing(func() {
  120. ids, err := s.messageCache.AttachmentsExpired()
  121. if err != nil {
  122. log.Tag(tagManager).Err(err).Warn("Error retrieving expired attachments")
  123. } else if len(ids) > 0 {
  124. if log.Tag(tagManager).IsDebug() {
  125. log.Tag(tagManager).Debug("Deleting attachments %s", strings.Join(ids, ", "))
  126. }
  127. if err := s.fileCache.Remove(ids...); err != nil {
  128. log.Tag(tagManager).Err(err).Warn("Error deleting attachments")
  129. }
  130. if err := s.messageCache.MarkAttachmentsDeleted(ids...); err != nil {
  131. log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
  132. }
  133. } else {
  134. log.Tag(tagManager).Debug("No expired attachments to delete")
  135. }
  136. }).
  137. Debug("Deleted expired attachments")
  138. }
  139. func (s *Server) pruneMessages() {
  140. log.
  141. Tag(tagManager).
  142. Timing(func() {
  143. expiredMessageIDs, err := s.messageCache.MessagesExpired()
  144. if err != nil {
  145. log.Tag(tagManager).Err(err).Warn("Error retrieving expired messages")
  146. } else if len(expiredMessageIDs) > 0 {
  147. if s.fileCache != nil {
  148. if err := s.fileCache.Remove(expiredMessageIDs...); err != nil {
  149. log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages")
  150. }
  151. }
  152. if err := s.messageCache.DeleteMessages(expiredMessageIDs...); err != nil {
  153. log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
  154. }
  155. } else {
  156. log.Tag(tagManager).Debug("No expired messages to delete")
  157. }
  158. }).
  159. Debug("Pruned messages")
  160. }