github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/jobs/workers.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package jobs
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/masterhung0112/hk_server/v5/model"
    10  	"github.com/masterhung0112/hk_server/v5/services/configservice"
    11  	"github.com/masterhung0112/hk_server/v5/shared/mlog"
    12  )
    13  
    14  type Workers struct {
    15  	ConfigService configservice.ConfigService
    16  	Watcher       *Watcher
    17  
    18  	DataRetention            model.Worker
    19  	MessageExport            model.Worker
    20  	ElasticsearchIndexing    model.Worker
    21  	ElasticsearchAggregation model.Worker
    22  	LdapSync                 model.Worker
    23  	Migrations               model.Worker
    24  	Plugins                  model.Worker
    25  	BleveIndexing            model.Worker
    26  	ExpiryNotify             model.Worker
    27  	ProductNotices           model.Worker
    28  	ActiveUsers              model.Worker
    29  	ImportProcess            model.Worker
    30  	ImportDelete             model.Worker
    31  	ExportProcess            model.Worker
    32  	ExportDelete             model.Worker
    33  	Cloud                    model.Worker
    34  	ResendInvitationEmail    model.Worker
    35  
    36  	listenerId string
    37  	running    bool
    38  }
    39  
    40  var (
    41  	ErrWorkersNotRunning    = errors.New("job workers are not running")
    42  	ErrWorkersRunning       = errors.New("job workers are running")
    43  	ErrWorkersUninitialized = errors.New("job workers are not initialized")
    44  )
    45  
    46  func (srv *JobServer) InitWorkers() error {
    47  	srv.mut.Lock()
    48  	defer srv.mut.Unlock()
    49  
    50  	if srv.workers != nil && srv.workers.running {
    51  		return ErrWorkersRunning
    52  	}
    53  
    54  	workers := &Workers{
    55  		ConfigService: srv.ConfigService,
    56  	}
    57  	workers.Watcher = srv.MakeWatcher(workers, DefaultWatcherPollingInterval)
    58  
    59  	if srv.DataRetentionJob != nil {
    60  		workers.DataRetention = srv.DataRetentionJob.MakeWorker()
    61  	}
    62  
    63  	if srv.MessageExportJob != nil {
    64  		workers.MessageExport = srv.MessageExportJob.MakeWorker()
    65  	}
    66  
    67  	if elasticsearchIndexerInterface := srv.ElasticsearchIndexer; elasticsearchIndexerInterface != nil {
    68  		workers.ElasticsearchIndexing = elasticsearchIndexerInterface.MakeWorker()
    69  	}
    70  
    71  	if elasticsearchAggregatorInterface := srv.ElasticsearchAggregator; elasticsearchAggregatorInterface != nil {
    72  		workers.ElasticsearchAggregation = elasticsearchAggregatorInterface.MakeWorker()
    73  	}
    74  
    75  	if ldapSyncInterface := srv.LdapSync; ldapSyncInterface != nil {
    76  		workers.LdapSync = ldapSyncInterface.MakeWorker()
    77  	}
    78  
    79  	if migrationsInterface := srv.Migrations; migrationsInterface != nil {
    80  		workers.Migrations = migrationsInterface.MakeWorker()
    81  	}
    82  
    83  	if pluginsInterface := srv.Plugins; pluginsInterface != nil {
    84  		workers.Plugins = pluginsInterface.MakeWorker()
    85  	}
    86  
    87  	if bleveIndexerInterface := srv.BleveIndexer; bleveIndexerInterface != nil {
    88  		workers.BleveIndexing = bleveIndexerInterface.MakeWorker()
    89  	}
    90  
    91  	if expiryNotifyInterface := srv.ExpiryNotify; expiryNotifyInterface != nil {
    92  		workers.ExpiryNotify = expiryNotifyInterface.MakeWorker()
    93  	}
    94  
    95  	if activeUsersInterface := srv.ActiveUsers; activeUsersInterface != nil {
    96  		workers.ActiveUsers = activeUsersInterface.MakeWorker()
    97  	}
    98  
    99  	if productNoticesInterface := srv.ProductNotices; productNoticesInterface != nil {
   100  		workers.ProductNotices = productNoticesInterface.MakeWorker()
   101  	}
   102  
   103  	if importProcessInterface := srv.ImportProcess; importProcessInterface != nil {
   104  		workers.ImportProcess = importProcessInterface.MakeWorker()
   105  	}
   106  
   107  	if importDeleteInterface := srv.ImportDelete; importDeleteInterface != nil {
   108  		workers.ImportDelete = importDeleteInterface.MakeWorker()
   109  	}
   110  
   111  	if exportProcessInterface := srv.ExportProcess; exportProcessInterface != nil {
   112  		workers.ExportProcess = exportProcessInterface.MakeWorker()
   113  	}
   114  
   115  	if exportDeleteInterface := srv.ExportDelete; exportDeleteInterface != nil {
   116  		workers.ExportDelete = exportDeleteInterface.MakeWorker()
   117  	}
   118  
   119  	if cloudInterface := srv.Cloud; cloudInterface != nil {
   120  		workers.Cloud = cloudInterface.MakeWorker()
   121  	}
   122  
   123  	if resendInvitationEmailInterface := srv.ResendInvitationEmails; resendInvitationEmailInterface != nil {
   124  		workers.ResendInvitationEmail = resendInvitationEmailInterface.MakeWorker()
   125  	}
   126  
   127  	srv.workers = workers
   128  
   129  	return nil
   130  }
   131  
   132  // Start starts the workers. This call is not safe for concurrent use.
   133  // Synchronization should be implemented by the caller.
   134  func (workers *Workers) Start() {
   135  	mlog.Info("Starting workers")
   136  	if workers.DataRetention != nil {
   137  		go workers.DataRetention.Run()
   138  	}
   139  
   140  	if workers.MessageExport != nil && *workers.ConfigService.Config().MessageExportSettings.EnableExport {
   141  		go workers.MessageExport.Run()
   142  	}
   143  
   144  	if workers.ElasticsearchIndexing != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   145  		go workers.ElasticsearchIndexing.Run()
   146  	}
   147  
   148  	if workers.ElasticsearchAggregation != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   149  		go workers.ElasticsearchAggregation.Run()
   150  	}
   151  
   152  	if workers.LdapSync != nil && *workers.ConfigService.Config().LdapSettings.EnableSync {
   153  		go workers.LdapSync.Run()
   154  	}
   155  
   156  	if workers.Migrations != nil {
   157  		go workers.Migrations.Run()
   158  	}
   159  
   160  	if workers.Plugins != nil {
   161  		go workers.Plugins.Run()
   162  	}
   163  
   164  	if workers.BleveIndexing != nil && *workers.ConfigService.Config().BleveSettings.EnableIndexing && *workers.ConfigService.Config().BleveSettings.IndexDir != "" {
   165  		go workers.BleveIndexing.Run()
   166  	}
   167  
   168  	if workers.ExpiryNotify != nil {
   169  		go workers.ExpiryNotify.Run()
   170  	}
   171  
   172  	if workers.ActiveUsers != nil {
   173  		go workers.ActiveUsers.Run()
   174  	}
   175  
   176  	if workers.ProductNotices != nil {
   177  		go workers.ProductNotices.Run()
   178  	}
   179  
   180  	if workers.ImportProcess != nil {
   181  		go workers.ImportProcess.Run()
   182  	}
   183  
   184  	if workers.ImportDelete != nil {
   185  		go workers.ImportDelete.Run()
   186  	}
   187  
   188  	if workers.ExportProcess != nil {
   189  		go workers.ExportProcess.Run()
   190  	}
   191  
   192  	if workers.ExportDelete != nil {
   193  		go workers.ExportDelete.Run()
   194  	}
   195  
   196  	if workers.Cloud != nil {
   197  		go workers.Cloud.Run()
   198  	}
   199  
   200  	if workers.ResendInvitationEmail != nil {
   201  		go workers.ResendInvitationEmail.Run()
   202  	}
   203  
   204  	go workers.Watcher.Start()
   205  
   206  	workers.listenerId = workers.ConfigService.AddConfigListener(workers.handleConfigChange)
   207  	workers.running = true
   208  }
   209  
   210  func (workers *Workers) handleConfigChange(oldConfig *model.Config, newConfig *model.Config) {
   211  	mlog.Debug("Workers received config change.")
   212  
   213  	if workers.DataRetention != nil {
   214  		if (!*oldConfig.DataRetentionSettings.EnableMessageDeletion && !*oldConfig.DataRetentionSettings.EnableFileDeletion) && (*newConfig.DataRetentionSettings.EnableMessageDeletion || *newConfig.DataRetentionSettings.EnableFileDeletion) {
   215  			go workers.DataRetention.Run()
   216  		} else if (*oldConfig.DataRetentionSettings.EnableMessageDeletion || *oldConfig.DataRetentionSettings.EnableFileDeletion) && (!*newConfig.DataRetentionSettings.EnableMessageDeletion && !*newConfig.DataRetentionSettings.EnableFileDeletion) {
   217  			workers.DataRetention.Stop()
   218  		}
   219  	}
   220  
   221  	if workers.MessageExport != nil {
   222  		if !*oldConfig.MessageExportSettings.EnableExport && *newConfig.MessageExportSettings.EnableExport {
   223  			go workers.MessageExport.Run()
   224  		} else if *oldConfig.MessageExportSettings.EnableExport && !*newConfig.MessageExportSettings.EnableExport {
   225  			workers.MessageExport.Stop()
   226  		}
   227  	}
   228  
   229  	if workers.ElasticsearchIndexing != nil {
   230  		if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
   231  			go workers.ElasticsearchIndexing.Run()
   232  		} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
   233  			workers.ElasticsearchIndexing.Stop()
   234  		}
   235  	}
   236  
   237  	if workers.ElasticsearchAggregation != nil {
   238  		if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
   239  			go workers.ElasticsearchAggregation.Run()
   240  		} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
   241  			workers.ElasticsearchAggregation.Stop()
   242  		}
   243  	}
   244  
   245  	if workers.LdapSync != nil {
   246  		if !*oldConfig.LdapSettings.EnableSync && *newConfig.LdapSettings.EnableSync {
   247  			go workers.LdapSync.Run()
   248  		} else if *oldConfig.LdapSettings.EnableSync && !*newConfig.LdapSettings.EnableSync {
   249  			workers.LdapSync.Stop()
   250  		}
   251  	}
   252  
   253  	if workers.BleveIndexing != nil {
   254  		if !*oldConfig.BleveSettings.EnableIndexing && *newConfig.BleveSettings.EnableIndexing {
   255  			go workers.BleveIndexing.Run()
   256  		} else if *oldConfig.BleveSettings.EnableIndexing && !*newConfig.BleveSettings.EnableIndexing {
   257  			workers.BleveIndexing.Stop()
   258  		}
   259  	}
   260  }
   261  
   262  // Stop stops the workers. This call is not safe for concurrent use.
   263  // Synchronization should be implemented by the caller.
   264  func (workers *Workers) Stop() {
   265  	workers.ConfigService.RemoveConfigListener(workers.listenerId)
   266  
   267  	workers.Watcher.Stop()
   268  
   269  	if workers.DataRetention != nil && (*workers.ConfigService.Config().DataRetentionSettings.EnableMessageDeletion || *workers.ConfigService.Config().DataRetentionSettings.EnableFileDeletion) {
   270  		workers.DataRetention.Stop()
   271  	}
   272  
   273  	if workers.MessageExport != nil && *workers.ConfigService.Config().MessageExportSettings.EnableExport {
   274  		workers.MessageExport.Stop()
   275  	}
   276  
   277  	if workers.ElasticsearchIndexing != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   278  		workers.ElasticsearchIndexing.Stop()
   279  	}
   280  
   281  	if workers.ElasticsearchAggregation != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   282  		workers.ElasticsearchAggregation.Stop()
   283  	}
   284  
   285  	if workers.LdapSync != nil && *workers.ConfigService.Config().LdapSettings.EnableSync {
   286  		workers.LdapSync.Stop()
   287  	}
   288  
   289  	if workers.Migrations != nil {
   290  		workers.Migrations.Stop()
   291  	}
   292  
   293  	if workers.Plugins != nil {
   294  		workers.Plugins.Stop()
   295  	}
   296  
   297  	if workers.BleveIndexing != nil && *workers.ConfigService.Config().BleveSettings.EnableIndexing {
   298  		workers.BleveIndexing.Stop()
   299  	}
   300  
   301  	if workers.ExpiryNotify != nil {
   302  		workers.ExpiryNotify.Stop()
   303  	}
   304  
   305  	if workers.ActiveUsers != nil {
   306  		workers.ActiveUsers.Stop()
   307  	}
   308  
   309  	if workers.ProductNotices != nil {
   310  		workers.ProductNotices.Stop()
   311  	}
   312  
   313  	if workers.ImportProcess != nil {
   314  		workers.ImportProcess.Stop()
   315  	}
   316  
   317  	if workers.ImportDelete != nil {
   318  		workers.ImportDelete.Stop()
   319  	}
   320  
   321  	if workers.ExportProcess != nil {
   322  		workers.ExportProcess.Stop()
   323  	}
   324  
   325  	if workers.ExportDelete != nil {
   326  		workers.ExportDelete.Stop()
   327  	}
   328  
   329  	if workers.Cloud != nil {
   330  		workers.Cloud.Stop()
   331  	}
   332  
   333  	if workers.ResendInvitationEmail != nil {
   334  		workers.ResendInvitationEmail.Stop()
   335  	}
   336  
   337  	workers.running = false
   338  
   339  	mlog.Info("Stopped workers")
   340  }