Moving app from singular to being created for every request (#9889)
* Moving app from singular to being created for every request. * Automatic refactor * Adding license header * Feedback fixes
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
1bcf08aa4b
Коммит
da265fbaf7
@@ -23,15 +23,15 @@ const (
|
||||
EMAIL_BATCHING_TASK_NAME = "Email Batching"
|
||||
)
|
||||
|
||||
func (a *App) InitEmailBatching() {
|
||||
if *a.Config().EmailSettings.EnableEmailBatching {
|
||||
if a.Srv.EmailBatching == nil {
|
||||
a.Srv.EmailBatching = NewEmailBatchingJob(a, *a.Config().EmailSettings.EmailBatchingBufferSize)
|
||||
func (s *Server) InitEmailBatching() {
|
||||
if *s.Config().EmailSettings.EnableEmailBatching {
|
||||
if s.EmailBatching == nil {
|
||||
s.EmailBatching = NewEmailBatchingJob(s, *s.Config().EmailSettings.EmailBatchingBufferSize)
|
||||
}
|
||||
|
||||
// note that we don't support changing EmailBatchingBufferSize without restarting the server
|
||||
|
||||
a.Srv.EmailBatching.Start()
|
||||
s.EmailBatching.Start()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,24 +55,24 @@ type batchedNotification struct {
|
||||
}
|
||||
|
||||
type EmailBatchingJob struct {
|
||||
app *App
|
||||
server *Server
|
||||
newNotifications chan *batchedNotification
|
||||
pendingNotifications map[string][]*batchedNotification
|
||||
task *model.ScheduledTask
|
||||
taskMutex sync.Mutex
|
||||
}
|
||||
|
||||
func NewEmailBatchingJob(a *App, bufferSize int) *EmailBatchingJob {
|
||||
func NewEmailBatchingJob(s *Server, bufferSize int) *EmailBatchingJob {
|
||||
return &EmailBatchingJob{
|
||||
app: a,
|
||||
server: s,
|
||||
newNotifications: make(chan *batchedNotification, bufferSize),
|
||||
pendingNotifications: make(map[string][]*batchedNotification),
|
||||
}
|
||||
}
|
||||
|
||||
func (job *EmailBatchingJob) Start() {
|
||||
mlog.Debug(fmt.Sprintf("Email batching job starting. Checking for pending emails every %v seconds.", *job.app.Config().EmailSettings.EmailBatchingInterval))
|
||||
newTask := model.CreateRecurringTask(EMAIL_BATCHING_TASK_NAME, job.CheckPendingEmails, time.Duration(*job.app.Config().EmailSettings.EmailBatchingInterval)*time.Second)
|
||||
mlog.Debug(fmt.Sprintf("Email batching job starting. Checking for pending emails every %v seconds.", *job.server.Config().EmailSettings.EmailBatchingInterval))
|
||||
newTask := model.CreateRecurringTask(EMAIL_BATCHING_TASK_NAME, job.CheckPendingEmails, time.Duration(*job.server.Config().EmailSettings.EmailBatchingInterval)*time.Second)
|
||||
|
||||
job.taskMutex.Lock()
|
||||
oldTask := job.task
|
||||
@@ -105,7 +105,7 @@ func (job *EmailBatchingJob) CheckPendingEmails() {
|
||||
|
||||
// it's a bit weird to pass the send email function through here, but it makes it so that we can test
|
||||
// without actually sending emails
|
||||
job.checkPendingNotifications(time.Now(), job.app.sendBatchedEmailNotification)
|
||||
job.checkPendingNotifications(time.Now(), job.server.sendBatchedEmailNotification)
|
||||
|
||||
mlog.Debug(fmt.Sprintf("Email batching job ran. %v user(s) still have notifications pending.", len(job.pendingNotifications)))
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
|
||||
continue
|
||||
}
|
||||
|
||||
result := <-job.app.Srv.Store.Team().GetByName(notifications[0].teamName)
|
||||
result := <-job.server.Store.Team().GetByName(notifications[0].teamName)
|
||||
if result.Err != nil {
|
||||
mlog.Error(fmt.Sprint("Unable to find Team id for notification", result.Err))
|
||||
continue
|
||||
@@ -152,7 +152,7 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
|
||||
|
||||
// if the user has viewed any channels in this team since the notification was queued, delete
|
||||
// all queued notifications
|
||||
result = <-job.app.Srv.Store.Channel().GetMembersForUser(inspectedTeamNames[notification.teamName], userId)
|
||||
result = <-job.server.Store.Channel().GetMembersForUser(inspectedTeamNames[notification.teamName], userId)
|
||||
if result.Err != nil {
|
||||
mlog.Error(fmt.Sprint("Unable to find ChannelMembers for user", result.Err))
|
||||
continue
|
||||
@@ -171,7 +171,7 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
|
||||
|
||||
// get how long we need to wait to send notifications to the user
|
||||
var interval int64
|
||||
pchan := job.app.Srv.Store.Preference().Get(userId, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL)
|
||||
pchan := job.server.Store.Preference().Get(userId, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL)
|
||||
if result := <-pchan; result.Err != nil {
|
||||
// use the default batching interval if an error ocurrs while fetching user preferences
|
||||
interval, _ = strconv.ParseInt(model.PREFERENCE_EMAIL_INTERVAL_BATCHING_SECONDS, 10, 64)
|
||||
@@ -188,7 +188,7 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
|
||||
|
||||
// send the email notification if it's been long enough
|
||||
if now.Sub(time.Unix(batchStartTime/1000, 0)) > time.Duration(interval)*time.Second {
|
||||
job.app.Srv.Go(func(userId string, notifications []*batchedNotification) func() {
|
||||
job.server.Go(func(userId string, notifications []*batchedNotification) func() {
|
||||
return func() {
|
||||
handler(userId, notifications)
|
||||
}
|
||||
@@ -198,8 +198,8 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) sendBatchedEmailNotification(userId string, notifications []*batchedNotification) {
|
||||
result := <-a.Srv.Store.User().Get(userId)
|
||||
func (s *Server) sendBatchedEmailNotification(userId string, notifications []*batchedNotification) {
|
||||
result := <-s.Store.User().Get(userId)
|
||||
if result.Err != nil {
|
||||
mlog.Warn("Unable to find recipient for batched email notification")
|
||||
return
|
||||
@@ -207,18 +207,18 @@ func (a *App) sendBatchedEmailNotification(userId string, notifications []*batch
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
translateFunc := utils.GetUserTranslations(user.Locale)
|
||||
displayNameFormat := *a.Config().TeamSettings.TeammateNameDisplay
|
||||
displayNameFormat := *s.Config().TeamSettings.TeammateNameDisplay
|
||||
|
||||
var contents string
|
||||
for _, notification := range notifications {
|
||||
result := <-a.Srv.Store.User().Get(notification.post.UserId)
|
||||
result := <-s.Store.User().Get(notification.post.UserId)
|
||||
if result.Err != nil {
|
||||
mlog.Warn("Unable to find sender of post for batched email notification")
|
||||
continue
|
||||
}
|
||||
sender := result.Data.(*model.User)
|
||||
|
||||
result = <-a.Srv.Store.Channel().Get(notification.post.ChannelId, true)
|
||||
result = <-s.Store.Channel().Get(notification.post.ChannelId, true)
|
||||
if result.Err != nil {
|
||||
mlog.Warn("Unable to find channel of post for batched email notification")
|
||||
continue
|
||||
@@ -226,43 +226,43 @@ func (a *App) sendBatchedEmailNotification(userId string, notifications []*batch
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
|
||||
if license := a.License(); license != nil && *license.Features.EmailNotificationContents {
|
||||
emailNotificationContentsType = *a.Config().EmailSettings.EmailNotificationContentsType
|
||||
if license := s.License(); license != nil && *license.Features.EmailNotificationContents {
|
||||
emailNotificationContentsType = *s.Config().EmailSettings.EmailNotificationContentsType
|
||||
}
|
||||
|
||||
contents += a.renderBatchedPost(notification, channel, sender, *a.Config().ServiceSettings.SiteURL, displayNameFormat, translateFunc, user.Locale, emailNotificationContentsType)
|
||||
contents += s.renderBatchedPost(notification, channel, sender, *s.Config().ServiceSettings.SiteURL, displayNameFormat, translateFunc, user.Locale, emailNotificationContentsType)
|
||||
}
|
||||
|
||||
tm := time.Unix(notifications[0].post.CreateAt/1000, 0)
|
||||
|
||||
subject := translateFunc("api.email_batching.send_batched_email_notification.subject", len(notifications), map[string]interface{}{
|
||||
"SiteName": a.Config().TeamSettings.SiteName,
|
||||
"SiteName": s.Config().TeamSettings.SiteName,
|
||||
"Year": tm.Year(),
|
||||
"Month": translateFunc(tm.Month().String()),
|
||||
"Day": tm.Day(),
|
||||
})
|
||||
|
||||
body := a.NewEmailTemplate("post_batched_body", user.Locale)
|
||||
body.Props["SiteURL"] = *a.Config().ServiceSettings.SiteURL
|
||||
body := s.FakeApp().NewEmailTemplate("post_batched_body", user.Locale)
|
||||
body.Props["SiteURL"] = *s.Config().ServiceSettings.SiteURL
|
||||
body.Props["Posts"] = template.HTML(contents)
|
||||
body.Props["BodyText"] = translateFunc("api.email_batching.send_batched_email_notification.body_text", len(notifications))
|
||||
|
||||
if err := a.SendMail(user.Email, subject, body.Render()); err != nil {
|
||||
if err := s.FakeApp().SendMail(user.Email, subject, body.Render()); err != nil {
|
||||
mlog.Warn(fmt.Sprintf("Unable to send batched email notification err=%v", err), mlog.String("email", user.Email))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) renderBatchedPost(notification *batchedNotification, channel *model.Channel, sender *model.User, siteURL string, displayNameFormat string, translateFunc i18n.TranslateFunc, userLocale string, emailNotificationContentsType string) string {
|
||||
func (s *Server) renderBatchedPost(notification *batchedNotification, channel *model.Channel, sender *model.User, siteURL string, displayNameFormat string, translateFunc i18n.TranslateFunc, userLocale string, emailNotificationContentsType string) string {
|
||||
// don't include message contents if email notification contents type is set to generic
|
||||
var template *utils.HTMLTemplate
|
||||
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
|
||||
template = a.NewEmailTemplate("post_batched_post_full", userLocale)
|
||||
template = s.FakeApp().NewEmailTemplate("post_batched_post_full", userLocale)
|
||||
} else {
|
||||
template = a.NewEmailTemplate("post_batched_post_generic", userLocale)
|
||||
template = s.FakeApp().NewEmailTemplate("post_batched_post_generic", userLocale)
|
||||
}
|
||||
|
||||
template.Props["Button"] = translateFunc("api.email_batching.render_batched_post.go_to_post")
|
||||
template.Props["PostMessage"] = a.GetMessageForNotification(notification.post, translateFunc)
|
||||
template.Props["PostMessage"] = s.FakeApp().GetMessageForNotification(notification.post, translateFunc)
|
||||
template.Props["PostLink"] = siteURL + "/" + notification.teamName + "/pl/" + notification.post.Id
|
||||
template.Props["SenderName"] = sender.GetDisplayName(displayNameFormat)
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user