github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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  	"sync"
     8  
     9  	"github.com/vnforks/kid/v5/mlog"
    10  	"github.com/vnforks/kid/v5/model"
    11  	"github.com/vnforks/kid/v5/services/configservice"
    12  )
    13  
    14  type Workers struct {
    15  	startOnce     sync.Once
    16  	ConfigService configservice.ConfigService
    17  	Watcher       *Watcher
    18  
    19  	DataRetention            model.Worker
    20  	MessageExport            model.Worker
    21  	ElasticsearchIndexing    model.Worker
    22  	ElasticsearchAggregation model.Worker
    23  	LdapSync                 model.Worker
    24  	Migrations               model.Worker
    25  	Plugins                  model.Worker
    26  
    27  	listenerId string
    28  }
    29  
    30  func (srv *JobServer) InitWorkers() *Workers {
    31  	workers := &Workers{
    32  		ConfigService: srv.ConfigService,
    33  	}
    34  	workers.Watcher = srv.MakeWatcher(workers, DEFAULT_WATCHER_POLLING_INTERVAL)
    35  
    36  	if srv.DataRetentionJob != nil {
    37  		workers.DataRetention = srv.DataRetentionJob.MakeWorker()
    38  	}
    39  
    40  	if srv.MessageExportJob != nil {
    41  		workers.MessageExport = srv.MessageExportJob.MakeWorker()
    42  	}
    43  
    44  	if elasticsearchIndexerInterface := srv.ElasticsearchIndexer; elasticsearchIndexerInterface != nil {
    45  		workers.ElasticsearchIndexing = elasticsearchIndexerInterface.MakeWorker()
    46  	}
    47  
    48  	if elasticsearchAggregatorInterface := srv.ElasticsearchAggregator; elasticsearchAggregatorInterface != nil {
    49  		workers.ElasticsearchAggregation = elasticsearchAggregatorInterface.MakeWorker()
    50  	}
    51  
    52  	if ldapSyncInterface := srv.LdapSync; ldapSyncInterface != nil {
    53  		workers.LdapSync = ldapSyncInterface.MakeWorker()
    54  	}
    55  
    56  	if migrationsInterface := srv.Migrations; migrationsInterface != nil {
    57  		workers.Migrations = migrationsInterface.MakeWorker()
    58  	}
    59  
    60  	if pluginsInterface := srv.Plugins; pluginsInterface != nil {
    61  		workers.Plugins = pluginsInterface.MakeWorker()
    62  	}
    63  
    64  	return workers
    65  }
    66  
    67  func (workers *Workers) Start() *Workers {
    68  	mlog.Info("Starting workers")
    69  
    70  	workers.startOnce.Do(func() {
    71  		if workers.DataRetention != nil && (*workers.ConfigService.Config().DataRetentionSettings.EnableMessageDeletion || *workers.ConfigService.Config().DataRetentionSettings.EnableFileDeletion) {
    72  			go workers.DataRetention.Run()
    73  		}
    74  
    75  		if workers.MessageExport != nil && *workers.ConfigService.Config().MessageExportSettings.EnableExport {
    76  			go workers.MessageExport.Run()
    77  		}
    78  
    79  		if workers.ElasticsearchIndexing != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
    80  			go workers.ElasticsearchIndexing.Run()
    81  		}
    82  
    83  		if workers.ElasticsearchAggregation != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
    84  			go workers.ElasticsearchAggregation.Run()
    85  		}
    86  
    87  		if workers.LdapSync != nil && *workers.ConfigService.Config().LdapSettings.EnableSync {
    88  			go workers.LdapSync.Run()
    89  		}
    90  
    91  		if workers.Migrations != nil {
    92  			go workers.Migrations.Run()
    93  		}
    94  
    95  		if workers.Plugins != nil {
    96  			go workers.Plugins.Run()
    97  		}
    98  
    99  		go workers.Watcher.Start()
   100  	})
   101  
   102  	workers.listenerId = workers.ConfigService.AddConfigListener(workers.handleConfigChange)
   103  
   104  	return workers
   105  }
   106  
   107  func (workers *Workers) handleConfigChange(oldConfig *model.Config, newConfig *model.Config) {
   108  	mlog.Debug("Workers received config change.")
   109  
   110  	if workers.DataRetention != nil {
   111  		if (!*oldConfig.DataRetentionSettings.EnableMessageDeletion && !*oldConfig.DataRetentionSettings.EnableFileDeletion) && (*newConfig.DataRetentionSettings.EnableMessageDeletion || *newConfig.DataRetentionSettings.EnableFileDeletion) {
   112  			go workers.DataRetention.Run()
   113  		} else if (*oldConfig.DataRetentionSettings.EnableMessageDeletion || *oldConfig.DataRetentionSettings.EnableFileDeletion) && (!*newConfig.DataRetentionSettings.EnableMessageDeletion && !*newConfig.DataRetentionSettings.EnableFileDeletion) {
   114  			workers.DataRetention.Stop()
   115  		}
   116  	}
   117  
   118  	if workers.MessageExport != nil {
   119  		if !*oldConfig.MessageExportSettings.EnableExport && *newConfig.MessageExportSettings.EnableExport {
   120  			go workers.MessageExport.Run()
   121  		} else if *oldConfig.MessageExportSettings.EnableExport && !*newConfig.MessageExportSettings.EnableExport {
   122  			workers.MessageExport.Stop()
   123  		}
   124  	}
   125  
   126  	if workers.ElasticsearchIndexing != nil {
   127  		if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
   128  			go workers.ElasticsearchIndexing.Run()
   129  		} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
   130  			workers.ElasticsearchIndexing.Stop()
   131  		}
   132  	}
   133  
   134  	if workers.ElasticsearchAggregation != nil {
   135  		if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
   136  			go workers.ElasticsearchAggregation.Run()
   137  		} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
   138  			workers.ElasticsearchAggregation.Stop()
   139  		}
   140  	}
   141  
   142  	if workers.LdapSync != nil {
   143  		if !*oldConfig.LdapSettings.EnableSync && *newConfig.LdapSettings.EnableSync {
   144  			go workers.LdapSync.Run()
   145  		} else if *oldConfig.LdapSettings.EnableSync && !*newConfig.LdapSettings.EnableSync {
   146  			workers.LdapSync.Stop()
   147  		}
   148  	}
   149  }
   150  
   151  func (workers *Workers) Stop() *Workers {
   152  	workers.ConfigService.RemoveConfigListener(workers.listenerId)
   153  
   154  	workers.Watcher.Stop()
   155  
   156  	if workers.DataRetention != nil && (*workers.ConfigService.Config().DataRetentionSettings.EnableMessageDeletion || *workers.ConfigService.Config().DataRetentionSettings.EnableFileDeletion) {
   157  		workers.DataRetention.Stop()
   158  	}
   159  
   160  	if workers.MessageExport != nil && *workers.ConfigService.Config().MessageExportSettings.EnableExport {
   161  		workers.MessageExport.Stop()
   162  	}
   163  
   164  	if workers.ElasticsearchIndexing != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   165  		workers.ElasticsearchIndexing.Stop()
   166  	}
   167  
   168  	if workers.ElasticsearchAggregation != nil && *workers.ConfigService.Config().ElasticsearchSettings.EnableIndexing {
   169  		workers.ElasticsearchAggregation.Stop()
   170  	}
   171  
   172  	if workers.LdapSync != nil && *workers.ConfigService.Config().LdapSettings.EnableSync {
   173  		workers.LdapSync.Stop()
   174  	}
   175  
   176  	if workers.Migrations != nil {
   177  		workers.Migrations.Stop()
   178  	}
   179  
   180  	if workers.Plugins != nil {
   181  		workers.Plugins.Stop()
   182  	}
   183  
   184  	mlog.Info("Stopped workers")
   185  
   186  	return workers
   187  }