server_manager.go 4.7 KB

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