github.com/spline-fu/mattermost-server@v4.10.10+incompatible/cmd/commands/server.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package commands 5 6 import ( 7 "fmt" 8 "net" 9 "os" 10 "os/signal" 11 "syscall" 12 "time" 13 14 "github.com/mattermost/mattermost-server/api" 15 "github.com/mattermost/mattermost-server/api4" 16 "github.com/mattermost/mattermost-server/app" 17 "github.com/mattermost/mattermost-server/cmd" 18 "github.com/mattermost/mattermost-server/manualtesting" 19 "github.com/mattermost/mattermost-server/mlog" 20 "github.com/mattermost/mattermost-server/model" 21 "github.com/mattermost/mattermost-server/utils" 22 "github.com/mattermost/mattermost-server/web" 23 "github.com/mattermost/mattermost-server/wsapi" 24 "github.com/spf13/cobra" 25 ) 26 27 const ( 28 SESSIONS_CLEANUP_BATCH_SIZE = 1000 29 ) 30 31 var MaxNotificationsPerChannelDefault int64 = 1000000 32 33 var serverCmd = &cobra.Command{ 34 Use: "server", 35 Short: "Run the Mattermost server", 36 RunE: serverCmdF, 37 SilenceUsage: true, 38 } 39 40 func init() { 41 cmd.RootCmd.AddCommand(serverCmd) 42 cmd.RootCmd.RunE = serverCmdF 43 } 44 45 func serverCmdF(command *cobra.Command, args []string) error { 46 config, err := command.Flags().GetString("config") 47 if err != nil { 48 return err 49 } 50 51 disableConfigWatch, _ := command.Flags().GetBool("disableconfigwatch") 52 53 interruptChan := make(chan os.Signal, 1) 54 return runServer(config, disableConfigWatch, interruptChan) 55 } 56 57 func runServer(configFileLocation string, disableConfigWatch bool, interruptChan chan os.Signal) error { 58 options := []app.Option{app.ConfigFile(configFileLocation)} 59 if disableConfigWatch { 60 options = append(options, app.DisableConfigWatch) 61 } 62 63 a, err := app.New(options...) 64 if err != nil { 65 mlog.Critical(err.Error()) 66 return err 67 } 68 defer a.Shutdown() 69 70 utils.TestConnection(a.Config()) 71 72 pwd, _ := os.Getwd() 73 mlog.Info(fmt.Sprintf("Current version is %v (%v/%v/%v/%v)", model.CurrentVersion, model.BuildNumber, model.BuildDate, model.BuildHash, model.BuildHashEnterprise)) 74 mlog.Info(fmt.Sprintf("Enterprise Enabled: %v", model.BuildEnterpriseReady)) 75 mlog.Info(fmt.Sprintf("Current working directory is %v", pwd)) 76 mlog.Info(fmt.Sprintf("Loaded config file from %v", utils.FindConfigFile(configFileLocation))) 77 78 backend, appErr := a.FileBackend() 79 if appErr == nil { 80 appErr = backend.TestConnection() 81 } 82 if appErr != nil { 83 mlog.Error("Problem with file storage settings: " + appErr.Error()) 84 } 85 86 if model.BuildEnterpriseReady == "true" { 87 a.LoadLicense() 88 } 89 90 a.DoAdvancedPermissionsMigration() 91 a.DoPermissionsMigrations() 92 93 a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory, nil) 94 a.AddConfigListener(func(prevCfg, cfg *model.Config) { 95 if *cfg.PluginSettings.Enable { 96 a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory, nil) 97 } else { 98 a.ShutDownPlugins() 99 } 100 }) 101 102 serverErr := a.StartServer() 103 if serverErr != nil { 104 mlog.Critical(serverErr.Error()) 105 return serverErr 106 } 107 108 api4.Init(a, a.Srv.Router, false) 109 api3 := api.Init(a, a.Srv.Router) 110 wsapi.Init(a, a.Srv.WebSocketRouter) 111 web.Init(api3) 112 113 license := a.License() 114 115 if license == nil && len(a.Config().SqlSettings.DataSourceReplicas) > 1 { 116 mlog.Warn("More than 1 read replica functionality disabled by current license. Please contact your system administrator about upgrading your enterprise license.") 117 a.UpdateConfig(func(cfg *model.Config) { 118 cfg.SqlSettings.DataSourceReplicas = cfg.SqlSettings.DataSourceReplicas[:1] 119 }) 120 } 121 122 if license == nil { 123 a.UpdateConfig(func(cfg *model.Config) { 124 cfg.TeamSettings.MaxNotificationsPerChannel = &MaxNotificationsPerChannelDefault 125 }) 126 } 127 128 a.ReloadConfig() 129 130 // Enable developer settings if this is a "dev" build 131 if model.BuildNumber == "dev" { 132 a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableDeveloper = true }) 133 } 134 135 resetStatuses(a) 136 137 // If we allow testing then listen for manual testing URL hits 138 if a.Config().ServiceSettings.EnableTesting { 139 manualtesting.Init(api3) 140 } 141 142 a.EnsureDiagnosticId() 143 144 a.Go(func() { 145 runSecurityJob(a) 146 }) 147 a.Go(func() { 148 runDiagnosticsJob(a) 149 }) 150 a.Go(func() { 151 runSessionCleanupJob(a) 152 }) 153 a.Go(func() { 154 runTokenCleanupJob(a) 155 }) 156 a.Go(func() { 157 runCommandWebhookCleanupJob(a) 158 }) 159 160 if complianceI := a.Compliance; complianceI != nil { 161 complianceI.StartComplianceDailyJob() 162 } 163 164 if a.Cluster != nil { 165 a.RegisterAllClusterMessageHandlers() 166 a.Cluster.StartInterNodeCommunication() 167 } 168 169 if a.Metrics != nil { 170 a.Metrics.StartServer() 171 } 172 173 if a.Elasticsearch != nil { 174 a.Go(func() { 175 if err := a.Elasticsearch.Start(); err != nil { 176 mlog.Error(err.Error()) 177 } 178 }) 179 } 180 181 if *a.Config().JobSettings.RunJobs { 182 a.Jobs.StartWorkers() 183 } 184 if *a.Config().JobSettings.RunScheduler { 185 a.Jobs.StartSchedulers() 186 } 187 188 notifyReady() 189 190 // wait for kill signal before attempting to gracefully shutdown 191 // the running service 192 signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) 193 <-interruptChan 194 195 if a.Cluster != nil { 196 a.Cluster.StopInterNodeCommunication() 197 } 198 199 if a.Metrics != nil { 200 a.Metrics.StopServer() 201 } 202 203 a.Jobs.StopSchedulers() 204 a.Jobs.StopWorkers() 205 206 return nil 207 } 208 209 func runSecurityJob(a *app.App) { 210 doSecurity(a) 211 model.CreateRecurringTask("Security", func() { 212 doSecurity(a) 213 }, time.Hour*4) 214 } 215 216 func runDiagnosticsJob(a *app.App) { 217 doDiagnostics(a) 218 model.CreateRecurringTask("Diagnostics", func() { 219 doDiagnostics(a) 220 }, time.Hour*24) 221 } 222 223 func runTokenCleanupJob(a *app.App) { 224 doTokenCleanup(a) 225 model.CreateRecurringTask("Token Cleanup", func() { 226 doTokenCleanup(a) 227 }, time.Hour*1) 228 } 229 230 func runCommandWebhookCleanupJob(a *app.App) { 231 doCommandWebhookCleanup(a) 232 model.CreateRecurringTask("Command Hook Cleanup", func() { 233 doCommandWebhookCleanup(a) 234 }, time.Hour*1) 235 } 236 237 func runSessionCleanupJob(a *app.App) { 238 doSessionCleanup(a) 239 model.CreateRecurringTask("Session Cleanup", func() { 240 doSessionCleanup(a) 241 }, time.Hour*24) 242 } 243 244 func resetStatuses(a *app.App) { 245 if result := <-a.Srv.Store.Status().ResetAll(); result.Err != nil { 246 mlog.Error(fmt.Sprint("mattermost.reset_status.error FIXME: NOT FOUND IN TRANSLATIONS FILE", result.Err.Error())) 247 } 248 } 249 250 func doSecurity(a *app.App) { 251 a.DoSecurityUpdateCheck() 252 } 253 254 func doDiagnostics(a *app.App) { 255 if *a.Config().LogSettings.EnableDiagnostics { 256 a.SendDailyDiagnostics() 257 } 258 } 259 260 func notifyReady() { 261 // If the environment vars provide a systemd notification socket, 262 // notify systemd that the server is ready. 263 systemdSocket := os.Getenv("NOTIFY_SOCKET") 264 if systemdSocket != "" { 265 mlog.Info("Sending systemd READY notification.") 266 267 err := sendSystemdReadyNotification(systemdSocket) 268 if err != nil { 269 mlog.Error(err.Error()) 270 } 271 } 272 } 273 274 func sendSystemdReadyNotification(socketPath string) error { 275 msg := "READY=1" 276 addr := &net.UnixAddr{ 277 Name: socketPath, 278 Net: "unixgram", 279 } 280 conn, err := net.DialUnix(addr.Net, nil, addr) 281 if err != nil { 282 return err 283 } 284 defer conn.Close() 285 _, err = conn.Write([]byte(msg)) 286 return err 287 } 288 289 func doTokenCleanup(a *app.App) { 290 a.Srv.Store.Token().Cleanup() 291 } 292 293 func doCommandWebhookCleanup(a *app.App) { 294 a.Srv.Store.CommandWebhook().Cleanup() 295 } 296 297 func doSessionCleanup(a *app.App) { 298 a.Srv.Store.Session().Cleanup(model.GetMillis(), SESSIONS_CLEANUP_BATCH_SIZE) 299 }