server_manager.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // Users
  60. var usersCount int64
  61. if s.userManager != nil {
  62. usersCount, err = s.userManager.UsersCount()
  63. if err != nil {
  64. log.Tag(tagManager).Err(err).Warn("Error counting users")
  65. }
  66. }
  67. // Print stats
  68. s.mu.RLock()
  69. messagesCount, topicsCount, visitorsCount := s.messages, len(s.topics), len(s.visitors)
  70. s.mu.RUnlock()
  71. // Update stats
  72. s.updateAndWriteStats(messagesCount)
  73. // Log stats
  74. log.
  75. Tag(tagManager).
  76. Fields(log.Context{
  77. "messages_published": messagesCount,
  78. "messages_cached": messagesCached,
  79. "topics_active": topicsCount,
  80. "subscribers": subscribers,
  81. "visitors": visitorsCount,
  82. "users": usersCount,
  83. "emails_received": receivedMailTotal,
  84. "emails_received_success": receivedMailSuccess,
  85. "emails_received_failure": receivedMailFailure,
  86. "emails_sent": sentMailTotal,
  87. "emails_sent_success": sentMailSuccess,
  88. "emails_sent_failure": sentMailFailure,
  89. }).
  90. Info("Server stats")
  91. mset(metricMessagesCached, messagesCached)
  92. mset(metricVisitors, visitorsCount)
  93. mset(metricUsers, usersCount)
  94. mset(metricSubscribers, subscribers)
  95. mset(metricTopics, topicsCount)
  96. }
  97. func (s *Server) pruneVisitors() {
  98. staleVisitors := 0
  99. log.
  100. Tag(tagManager).
  101. Timing(func() {
  102. s.mu.Lock()
  103. defer s.mu.Unlock()
  104. for ip, v := range s.visitors {
  105. if v.Stale() {
  106. log.Tag(tagManager).With(v).Trace("Deleting stale visitor")
  107. delete(s.visitors, ip)
  108. staleVisitors++
  109. }
  110. }
  111. }).
  112. Field("stale_visitors", staleVisitors).
  113. Debug("Deleted %d stale visitor(s)", staleVisitors)
  114. }
  115. func (s *Server) pruneTokens() {
  116. if s.userManager != nil {
  117. log.
  118. Tag(tagManager).
  119. Timing(func() {
  120. if err := s.userManager.RemoveExpiredTokens(); err != nil {
  121. log.Tag(tagManager).Err(err).Warn("Error expiring user tokens")
  122. }
  123. if err := s.userManager.RemoveDeletedUsers(); err != nil {
  124. log.Tag(tagManager).Err(err).Warn("Error deleting soft-deleted users")
  125. }
  126. }).
  127. Debug("Removed expired tokens and users")
  128. }
  129. }
  130. func (s *Server) pruneAttachments() {
  131. if s.fileCache == nil {
  132. return
  133. }
  134. log.
  135. Tag(tagManager).
  136. Timing(func() {
  137. ids, err := s.messageCache.AttachmentsExpired()
  138. if err != nil {
  139. log.Tag(tagManager).Err(err).Warn("Error retrieving expired attachments")
  140. } else if len(ids) > 0 {
  141. if log.Tag(tagManager).IsDebug() {
  142. log.Tag(tagManager).Debug("Deleting attachments %s", strings.Join(ids, ", "))
  143. }
  144. if err := s.fileCache.Remove(ids...); err != nil {
  145. log.Tag(tagManager).Err(err).Warn("Error deleting attachments")
  146. }
  147. if err := s.messageCache.MarkAttachmentsDeleted(ids...); err != nil {
  148. log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
  149. }
  150. } else {
  151. log.Tag(tagManager).Debug("No expired attachments to delete")
  152. }
  153. }).
  154. Debug("Deleted expired attachments")
  155. }
  156. func (s *Server) pruneMessages() {
  157. log.
  158. Tag(tagManager).
  159. Timing(func() {
  160. expiredMessageIDs, err := s.messageCache.MessagesExpired()
  161. if err != nil {
  162. log.Tag(tagManager).Err(err).Warn("Error retrieving expired messages")
  163. } else if len(expiredMessageIDs) > 0 {
  164. if s.fileCache != nil {
  165. if err := s.fileCache.Remove(expiredMessageIDs...); err != nil {
  166. log.Tag(tagManager).Err(err).Warn("Error deleting attachments for expired messages")
  167. }
  168. }
  169. if err := s.messageCache.DeleteMessages(expiredMessageIDs...); err != nil {
  170. log.Tag(tagManager).Err(err).Warn("Error marking attachments deleted")
  171. }
  172. } else {
  173. log.Tag(tagManager).Debug("No expired messages to delete")
  174. }
  175. }).
  176. Debug("Pruned messages")
  177. }