PLT-6916: Redesign the jobs package and Jobserver. (#6733)
This commit redesigns the jobserver to be based around an architecture of "workers", which carry out jobs of a particular type, and "jobs" which are a unit of work carried by a particular worker. It also introduces "schedulers" which are responsible for scheduling jobs of a particular type automatically (jobs can also be scheduled manually when apropriate). Workers may be run many times, either in instances of the platform binary, or the standalone jobserver binary. In any mattermost cluster, only one instance of platform OR jobserver must run the schedulers. At the moment this is controlled by a config variable, but in future will be controlled through the cluster leader election process.
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6e0f5f0969
Коммит
0495a51949
79
jobs/workers.go
Обычный файл
79
jobs/workers.go
Обычный файл
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
ejobs "github.com/mattermost/platform/einterfaces/jobs"
|
||||
"github.com/mattermost/platform/model"
|
||||
"github.com/mattermost/platform/utils"
|
||||
)
|
||||
|
||||
type Workers struct {
|
||||
startOnce sync.Once
|
||||
watcher *Watcher
|
||||
|
||||
DataRetention model.Worker
|
||||
// SearchIndexing model.Job
|
||||
|
||||
listenerId string
|
||||
}
|
||||
|
||||
func InitWorkers() *Workers {
|
||||
workers := &Workers{
|
||||
// SearchIndexing: MakeTestJob(s, "SearchIndexing"),
|
||||
}
|
||||
workers.watcher = MakeWatcher(workers)
|
||||
|
||||
if dataRetentionInterface := ejobs.GetDataRetentionInterface(); dataRetentionInterface != nil {
|
||||
workers.DataRetention = dataRetentionInterface.MakeWorker()
|
||||
}
|
||||
|
||||
return workers
|
||||
}
|
||||
|
||||
func (workers *Workers) Start() *Workers {
|
||||
l4g.Info("Starting workers")
|
||||
|
||||
workers.startOnce.Do(func() {
|
||||
if workers.DataRetention != nil && *utils.Cfg.DataRetentionSettings.Enable {
|
||||
go workers.DataRetention.Run()
|
||||
}
|
||||
|
||||
// go workers.SearchIndexing.Run()
|
||||
|
||||
go workers.watcher.Start()
|
||||
})
|
||||
|
||||
workers.listenerId = utils.AddConfigListener(workers.handleConfigChange)
|
||||
|
||||
return workers
|
||||
}
|
||||
|
||||
func (workers *Workers) handleConfigChange(oldConfig *model.Config, newConfig *model.Config) {
|
||||
if workers.DataRetention != nil {
|
||||
if !*oldConfig.DataRetentionSettings.Enable && *newConfig.DataRetentionSettings.Enable {
|
||||
go workers.DataRetention.Run()
|
||||
} else if *oldConfig.DataRetentionSettings.Enable && !*newConfig.DataRetentionSettings.Enable {
|
||||
workers.DataRetention.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (workers *Workers) Stop() *Workers {
|
||||
utils.RemoveConfigListener(workers.listenerId)
|
||||
|
||||
workers.watcher.Stop()
|
||||
|
||||
if workers.DataRetention != nil && *utils.Cfg.DataRetentionSettings.Enable {
|
||||
workers.DataRetention.Stop()
|
||||
}
|
||||
// workers.SearchIndexing.Stop()
|
||||
|
||||
l4g.Info("Stopped workers")
|
||||
|
||||
return workers
|
||||
}
|
||||
Ссылка в новой задаче
Block a user