From 1a49b7e929b504c4bb6cc397adba2eb042afba5c Mon Sep 17 00:00:00 2001 From: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Date: Mon, 27 Nov 2023 08:49:10 +0530 Subject: [PATCH] Added license change listener (#24342) * Added license change listener * Fixed location of adding license listener * Made tests unaffected * Minor refactoring * Changed order of checks to avoid breaking all tests * Using CreateJobOnce to handle HA * Updated context --------- Co-authored-by: Mattermost Build --- server/channels/app/migrations.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/server/channels/app/migrations.go b/server/channels/app/migrations.go index 77646dc33c..edb19f2cbc 100644 --- a/server/channels/app/migrations.go +++ b/server/channels/app/migrations.go @@ -555,20 +555,27 @@ func (s *Server) doPostPriorityConfigDefaultTrueMigration() { } func (s *Server) doElasticsearchFixChannelIndex(c request.CTX) { + s.AddLicenseListener(func(oldLicense, newLicense *model.License) { + s.elasticsearchFixChannelIndex(c, newLicense) + }) + + s.elasticsearchFixChannelIndex(c, s.License()) +} + +func (s *Server) elasticsearchFixChannelIndex(c request.CTX, license *model.License) { + if model.BuildEnterpriseReady != "true" || license == nil || !*license.Features.Elasticsearch { + mlog.Debug("Skipping triggering Elasticsearch channel index fix job as build is not Enterprise ready") + return + } + // If the migration is already marked as completed, don't do it again. if _, err := s.Store().System().GetByName(model.MigrationKeyElasticsearchFixChannelIndex); err == nil { + mlog.Debug("Skipping triggering Elasticsearch channel index fix job as it is already marked completed in database") return } - license := s.License() - if model.BuildEnterpriseReady != "true" || license == nil || !*license.Features.Elasticsearch { - mlog.Info("Skipping triggering Elasticsearch channel index fix job as build is not Enterprise ready") - return - } - - if _, appErr := s.Jobs.CreateJob(c, model.JobTypeElasticsearchFixChannelIndex, nil); appErr != nil { + if _, appErr := s.Jobs.CreateJobOnce(c, model.JobTypeElasticsearchFixChannelIndex, nil); appErr != nil { mlog.Fatal("failed to start job for fixing Elasticsearch channels index", mlog.Err(appErr)) - return } }