| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"heckel.io/ntfy/v2/log" |
| 5 |
"heckel.io/ntfy/v2/metrics" |
| 6 |
"heckel.io/ntfy/v2/util" |
| 7 |
) |
| 8 |
|
| 9 |
func (s *Server) execManager() { |
| 10 |
// WARNING: Make sure to only selectively lock with the mutex, and be aware that this |
| 11 |
// there is no mutex for the entire function. |
| 12 |
|
| 13 |
// Prune all the things |
| 14 |
s.pruneVisitors() |
| 15 |
s.pruneTokens() |
| 16 |
s.pruneAttachments() |
| 17 |
s.pruneMessages() |
| 18 |
s.pruneAndNotifyWebPushSubscriptions() |
| 19 |
|
| 20 |
// Message count |
| 21 |
messagesCached, err := s.messageCache.MessagesCount() |
| 22 |
if err != nil { |
| 23 |
log.Tag(tagManager).Err(err).Warn("Cannot get messages count") |
| 24 |
} |
| 25 |
|
| 26 |
// Remove subscriptions without subscribers |
| 27 |
var emptyTopics, subscribers int |
| 28 |
log. |
| 29 |
Tag(tagManager). |
| 30 |
Timing(func() { |
| 31 |
s.mu.Lock() |
| 32 |
defer s.mu.Unlock() |
| 33 |
for _, t := range s.topics { |
| 34 |
subs, lastAccess := t.Stats() |
| 35 |
ev := log.Tag(tagManager).With(t) |
| 36 |
if t.Stale() { |
| 37 |
if ev.IsTrace() { |
| 38 |
ev.Trace("- topic %s: Deleting stale topic (%d subscribers, accessed %s)", t.ID, subs, util.FormatTime(lastAccess)) |
| 39 |
} |
| 40 |
emptyTopics++ |
| 41 |
delete(s.topics, t.ID) |
| 42 |
} else { |
| 43 |
if ev.IsTrace() { |
| 44 |
ev.Trace("- topic %s: %d subscribers, accessed %s", t.ID, subs, util.FormatTime(lastAccess)) |
| 45 |
} |
| 46 |
subscribers += subs |
| 47 |
} |
| 48 |
} |
| 49 |
}). |
| 50 |
Debug("Removed %d empty topic(s)", emptyTopics) |
| 51 |
|
| 52 |
// Mail stats |
| 53 |
var receivedMailTotal, receivedMailSuccess, receivedMailFailure int64 |
| 54 |
if s.smtpServerBackend != nil { |
| 55 |
receivedMailTotal, receivedMailSuccess, receivedMailFailure = s.smtpServerBackend.Counts() |
| 56 |
} |
| 57 |
var sentMailTotal, sentMailSuccess, sentMailFailure int64 |
| 58 |
if s.mailer != nil { |
| 59 |
sentMailTotal, sentMailSuccess, sentMailFailure = s.mailer.NotificationCounts() |
| 60 |
} |
| 61 |
|
| 62 |
// Users |
| 63 |
var usersCount int64 |
| 64 |
if s.userManager != nil { |
| 65 |
usersCount, err = s.userManager.UsersCount() |
| 66 |
if err != nil { |
| 67 |
log.Tag(tagManager).Err(err).Warn("Error counting users") |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Print stats |
| 72 |
s.mu.RLock() |
| 73 |
messagesCount, topicsCount, visitorsCount := s.messages, len(s.topics), len(s.visitors) |
| 74 |
s.mu.RUnlock() |
| 75 |
|
| 76 |
// Update stats |
| 77 |
s.updateAndWriteStats(messagesCount) |
| 78 |
|
| 79 |
// Log stats |
| 80 |
log. |
| 81 |
Tag(tagManager). |
| 82 |
Fields(log.Context{ |
| 83 |
"messages_published": messagesCount, |
| 84 |
"messages_cached": messagesCached, |
| 85 |
"topics_active": topicsCount, |
| 86 |
"subscribers": subscribers, |
| 87 |
"visitors": visitorsCount, |
| 88 |
"users": usersCount, |
| 89 |
"emails_received": receivedMailTotal, |
| 90 |
"emails_received_success": receivedMailSuccess, |
| 91 |
"emails_received_failure": receivedMailFailure, |
| 92 |
"emails_sent": sentMailTotal, |
| 93 |
"emails_sent_success": sentMailSuccess, |
| 94 |
"emails_sent_failure": sentMailFailure, |
| 95 |
}). |
| 96 |
Info("Server stats") |
| 97 |
metrics.MessagesCached.Set(float64(messagesCached)) |
| 98 |
metrics.Visitors.Set(float64(visitorsCount)) |
| 99 |
metrics.Users.Set(float64(usersCount)) |
| 100 |
metrics.Subscribers.Set(float64(subscribers)) |
| 101 |
metrics.Topics.Set(float64(topicsCount)) |
| 102 |
if s.attachment != nil { |
| 103 |
metrics.AttachmentsTotalSize.Set(float64(s.attachment.Size())) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func (s *Server) pruneVisitors() { |
| 108 |
staleVisitors := 0 |
| 109 |
log. |
| 110 |
Tag(tagManager). |
| 111 |
Timing(func() { |
| 112 |
s.mu.Lock() |
| 113 |
defer s.mu.Unlock() |
| 114 |
for ip, v := range s.visitors { |
| 115 |
if v.Stale() { |
| 116 |
log.Tag(tagManager).With(v).Trace("Deleting stale visitor") |
| 117 |
delete(s.visitors, ip) |
| 118 |
staleVisitors++ |
| 119 |
} |
| 120 |
} |
| 121 |
}). |
| 122 |
Field("stale_visitors", staleVisitors). |
| 123 |
Debug("Finished deleting stale visitors") |
| 124 |
} |
| 125 |
|
| 126 |
func (s *Server) pruneTokens() { |
| 127 |
if s.userManager != nil { |
| 128 |
log. |
| 129 |
Tag(tagManager). |
| 130 |
Timing(func() { |
| 131 |
if err := s.userManager.RemoveExpiredTokens(); err != nil { |
| 132 |
log.Tag(tagManager).Err(err).Warn("Error expiring user tokens") |
| 133 |
} |
| 134 |
if err := s.userManager.RemoveDeletedUsers(); err != nil { |
| 135 |
log.Tag(tagManager).Err(err).Warn("Error deleting soft-deleted users") |
| 136 |
} |
| 137 |
}). |
| 138 |
Debug("Finished deleting expired tokens and users") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func (s *Server) pruneAttachments() { |
| 143 |
if s.attachment == nil { |
| 144 |
return |
| 145 |
} |
| 146 |
// Only mark as deleted in DB. The actual storage files are cleaned up |
| 147 |
// by the attachment store's sync() loop, which periodically reconciles |
| 148 |
// storage with the database and removes orphaned files. |
| 149 |
log. |
| 150 |
Tag(tagManager). |
| 151 |
Timing(func() { |
| 152 |
count, err := s.messageCache.MarkExpiredAttachmentsDeleted(s.config.ManagerBatchSize) |
| 153 |
if err != nil { |
| 154 |
log.Tag(tagManager).Err(err).Warn("Error marking expired attachments as deleted") |
| 155 |
} else if count > 0 { |
| 156 |
log.Tag(tagManager).Debug("Marked %d expired attachment(s) as deleted", count) |
| 157 |
} else { |
| 158 |
log.Tag(tagManager).Debug("No expired attachments to delete") |
| 159 |
} |
| 160 |
}). |
| 161 |
Debug("Finished marking expired attachments as deleted") |
| 162 |
} |
| 163 |
|
| 164 |
func (s *Server) pruneMessages() { |
| 165 |
// Only delete DB rows. Attachment storage files are cleaned up by the |
| 166 |
// attachment store's sync() loop, which periodically reconciles storage |
| 167 |
// with the database and removes orphaned files. |
| 168 |
log. |
| 169 |
Tag(tagManager). |
| 170 |
Timing(func() { |
| 171 |
count, err := s.messageCache.DeleteExpiredMessages(s.config.ManagerBatchSize) |
| 172 |
if err != nil { |
| 173 |
log.Tag(tagManager).Err(err).Warn("Error deleting expired messages") |
| 174 |
} else if count > 0 { |
| 175 |
log.Tag(tagManager).Debug("Deleted %d expired message(s)", count) |
| 176 |
} else { |
| 177 |
log.Tag(tagManager).Debug("No expired messages to delete") |
| 178 |
} |
| 179 |
}). |
| 180 |
Debug("Finished deleting expired messages") |
| 181 |
} |