Fixing plugin API not having proper access to Cluster. (#10257)

Этот коммит содержится в:
Christopher Speller
2019-02-08 18:13:52 -08:00
коммит произвёл Lev
родитель 4dbeaffdf0
Коммит 03a96d71d8

Просмотреть файл

@@ -23,37 +23,35 @@ import (
// Don't add anything new here, new initilization should be done in the server and // Don't add anything new here, new initilization should be done in the server and
// performed in the NewServer function. // performed in the NewServer function.
func (s *Server) RunOldAppInitalization() error { func (s *Server) RunOldAppInitalization() error {
a := s.FakeApp() s.FakeApp().CreatePushNotificationsHub()
s.FakeApp().StartPushNotificationsHubWorkers()
a.CreatePushNotificationsHub() if err := utils.InitTranslations(s.FakeApp().Config().LocalizationSettings); err != nil {
a.StartPushNotificationsHubWorkers()
if err := utils.InitTranslations(a.Config().LocalizationSettings); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files") return errors.Wrapf(err, "unable to load Mattermost translation files")
} }
a.Srv.configListenerId = a.AddConfigListener(func(_, _ *model.Config) { s.FakeApp().Srv.configListenerId = s.FakeApp().AddConfigListener(func(_, _ *model.Config) {
a.configOrLicenseListener() s.FakeApp().configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil)
message.Add("config", a.ClientConfigWithComputed()) message.Add("config", s.FakeApp().ClientConfigWithComputed())
a.Srv.Go(func() { s.Go(func() {
a.Publish(message) s.FakeApp().Publish(message)
}) })
}) })
a.Srv.licenseListenerId = a.AddLicenseListener(func() { s.FakeApp().Srv.licenseListenerId = s.FakeApp().AddLicenseListener(func() {
a.configOrLicenseListener() s.FakeApp().configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil) message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil)
message.Add("license", a.GetSanitizedClientLicense()) message.Add("license", s.FakeApp().GetSanitizedClientLicense())
a.Srv.Go(func() { s.Go(func() {
a.Publish(message) s.FakeApp().Publish(message)
}) })
}) })
if err := a.SetupInviteEmailRateLimiting(); err != nil { if err := s.FakeApp().SetupInviteEmailRateLimiting(); err != nil {
return err return err
} }
@@ -61,67 +59,67 @@ func (s *Server) RunOldAppInitalization() error {
s.initEnterprise() s.initEnterprise()
if a.Srv.newStore == nil { if s.FakeApp().Srv.newStore == nil {
a.Srv.newStore = func() store.Store { s.FakeApp().Srv.newStore = func() store.Store {
return store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Config().SqlSettings, a.Metrics), a.Metrics, a.Cluster) return store.NewLayeredStore(sqlstore.NewSqlSupplier(s.FakeApp().Config().SqlSettings, s.Metrics), s.Metrics, s.Cluster)
} }
} }
if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil { if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil {
mlog.Error(fmt.Sprintf("Failed to parse server templates %v", err)) mlog.Error(fmt.Sprintf("Failed to parse server templates %v", err))
} else { } else {
a.Srv.htmlTemplateWatcher = htmlTemplateWatcher s.FakeApp().Srv.htmlTemplateWatcher = htmlTemplateWatcher
} }
a.Srv.Store = a.Srv.newStore() s.FakeApp().Srv.Store = s.FakeApp().Srv.newStore()
if err := a.ensureAsymmetricSigningKey(); err != nil { if err := s.FakeApp().ensureAsymmetricSigningKey(); err != nil {
return errors.Wrapf(err, "unable to ensure asymmetric signing key") return errors.Wrapf(err, "unable to ensure asymmetric signing key")
} }
if err := a.ensureInstallationDate(); err != nil { if err := s.FakeApp().ensureInstallationDate(); err != nil {
return errors.Wrapf(err, "unable to ensure installation date") return errors.Wrapf(err, "unable to ensure installation date")
} }
a.EnsureDiagnosticId() s.FakeApp().EnsureDiagnosticId()
a.regenerateClientConfig() s.FakeApp().regenerateClientConfig()
a.Srv.clusterLeaderListenerId = a.Srv.AddClusterLeaderChangedListener(func() { s.FakeApp().Srv.clusterLeaderListenerId = s.FakeApp().Srv.AddClusterLeaderChangedListener(func() {
mlog.Info("Cluster leader changed. Determining if job schedulers should be running:", mlog.Bool("isLeader", a.IsLeader())) mlog.Info("Cluster leader changed. Determining if job schedulers should be running:", mlog.Bool("isLeader", s.FakeApp().IsLeader()))
if a.Srv.Jobs != nil { if s.FakeApp().Srv.Jobs != nil {
a.Srv.Jobs.Schedulers.HandleClusterLeaderChange(a.IsLeader()) s.FakeApp().Srv.Jobs.Schedulers.HandleClusterLeaderChange(s.FakeApp().IsLeader())
} }
}) })
subpath, err := utils.GetSubpathFromConfig(a.Config()) subpath, err := utils.GetSubpathFromConfig(s.FakeApp().Config())
if err != nil { if err != nil {
return errors.Wrap(err, "failed to parse SiteURL subpath") return errors.Wrap(err, "failed to parse SiteURL subpath")
} }
a.Srv.Router = a.Srv.RootRouter.PathPrefix(subpath).Subrouter() s.FakeApp().Srv.Router = s.FakeApp().Srv.RootRouter.PathPrefix(subpath).Subrouter()
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}", a.ServePluginRequest) s.FakeApp().Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}", s.FakeApp().ServePluginRequest)
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", a.ServePluginRequest) s.FakeApp().Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", s.FakeApp().ServePluginRequest)
// If configured with a subpath, redirect 404s at the root back into the subpath. // If configured with a subpath, redirect 404s at the root back into the subpath.
if subpath != "/" { if subpath != "/" {
a.Srv.RootRouter.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s.FakeApp().Srv.RootRouter.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = path.Join(subpath, r.URL.Path) r.URL.Path = path.Join(subpath, r.URL.Path)
http.Redirect(w, r, r.URL.String(), http.StatusFound) http.Redirect(w, r, r.URL.String(), http.StatusFound)
}) })
} }
a.Srv.Router.NotFoundHandler = http.HandlerFunc(a.Handle404) s.FakeApp().Srv.Router.NotFoundHandler = http.HandlerFunc(s.FakeApp().Handle404)
a.Srv.WebSocketRouter = &WebSocketRouter{ s.FakeApp().Srv.WebSocketRouter = &WebSocketRouter{
app: a, app: s.FakeApp(),
handlers: make(map[string]webSocketHandler), handlers: make(map[string]webSocketHandler),
} }
mailservice.TestConnection(a.Config()) mailservice.TestConnection(s.FakeApp().Config())
if _, err := url.ParseRequestURI(*a.Config().ServiceSettings.SiteURL); err != nil { if _, err := url.ParseRequestURI(*s.FakeApp().Config().ServiceSettings.SiteURL); err != nil {
mlog.Error("SiteURL must be set. Some features will operate incorrectly if the SiteURL is not set. See documentation for details: http://about.mattermost.com/default-site-url") mlog.Error("SiteURL must be set. Some features will operate incorrectly if the SiteURL is not set. See documentation for details: http://about.mattermost.com/default-site-url")
} }
backend, appErr := a.FileBackend() backend, appErr := s.FakeApp().FileBackend()
if appErr == nil { if appErr == nil {
appErr = backend.TestConnection() appErr = backend.TestConnection()
} }
@@ -130,20 +128,20 @@ func (s *Server) RunOldAppInitalization() error {
} }
if model.BuildEnterpriseReady == "true" { if model.BuildEnterpriseReady == "true" {
a.LoadLicense() s.FakeApp().LoadLicense()
} }
a.DoAdvancedPermissionsMigration() s.FakeApp().DoAdvancedPermissionsMigration()
a.DoEmojisPermissionsMigration() s.FakeApp().DoEmojisPermissionsMigration()
a.InitPostMetadata() s.FakeApp().InitPostMetadata()
a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory) s.FakeApp().InitPlugins(*s.Config().PluginSettings.Directory, *s.Config().PluginSettings.ClientDirectory)
a.AddConfigListener(func(prevCfg, cfg *model.Config) { s.FakeApp().AddConfigListener(func(prevCfg, cfg *model.Config) {
if *cfg.PluginSettings.Enable { if *cfg.PluginSettings.Enable {
a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory) s.FakeApp().InitPlugins(*cfg.PluginSettings.Directory, *s.Config().PluginSettings.ClientDirectory)
} else { } else {
a.ShutDownPlugins() s.FakeApp().ShutDownPlugins()
} }
}) })
@@ -151,11 +149,10 @@ func (s *Server) RunOldAppInitalization() error {
} }
func (s *Server) RunOldAppShutdown() { func (s *Server) RunOldAppShutdown() {
a := s.FakeApp() s.FakeApp().HubStop()
a.HubStop() s.FakeApp().StopPushNotificationsHubWorkers()
a.StopPushNotificationsHubWorkers() s.FakeApp().ShutDownPlugins()
a.ShutDownPlugins() s.FakeApp().RemoveLicenseListener(s.licenseListenerId)
a.RemoveLicenseListener(s.licenseListenerId)
s.RemoveClusterLeaderChangedListener(s.clusterLeaderListenerId) s.RemoveClusterLeaderChangedListener(s.clusterLeaderListenerId)
} }