diff options
author | George Goldberg <george@gberg.me> | 2017-07-07 15:21:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-07 15:21:02 +0100 |
commit | 0495a519499d6cefa289982a94d8f42de541c1f0 (patch) | |
tree | 94b6145daa41ca4d1d4a172f030071076852a09a /jobs/schedulers.go | |
parent | 6e0f5f096986dad11ef182ddb51d4bfb0e558860 (diff) | |
download | chat-0495a519499d6cefa289982a94d8f42de541c1f0.tar.gz chat-0495a519499d6cefa289982a94d8f42de541c1f0.tar.bz2 chat-0495a519499d6cefa289982a94d8f42de541c1f0.zip |
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.
Diffstat (limited to 'jobs/schedulers.go')
-rw-r--r-- | jobs/schedulers.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/jobs/schedulers.go b/jobs/schedulers.go new file mode 100644 index 000000000..73ec6661a --- /dev/null +++ b/jobs/schedulers.go @@ -0,0 +1,68 @@ +// 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 Schedulers struct { + startOnce sync.Once + + DataRetention model.Scheduler + + listenerId string +} + +func InitSchedulers() *Schedulers { + schedulers := &Schedulers{} + + if dataRetentionInterface := ejobs.GetDataRetentionInterface(); dataRetentionInterface != nil { + schedulers.DataRetention = dataRetentionInterface.MakeScheduler() + } + + return schedulers +} + +func (schedulers *Schedulers) Start() *Schedulers { + l4g.Info("Starting schedulers") + + schedulers.startOnce.Do(func() { + if schedulers.DataRetention != nil && *utils.Cfg.DataRetentionSettings.Enable { + go schedulers.DataRetention.Run() + } + }) + + schedulers.listenerId = utils.AddConfigListener(schedulers.handleConfigChange) + + return schedulers +} + +func (schedulers *Schedulers) handleConfigChange(oldConfig *model.Config, newConfig *model.Config) { + if schedulers.DataRetention != nil { + if !*oldConfig.DataRetentionSettings.Enable && *newConfig.DataRetentionSettings.Enable { + go schedulers.DataRetention.Run() + } else if *oldConfig.DataRetentionSettings.Enable && !*newConfig.DataRetentionSettings.Enable { + schedulers.DataRetention.Stop() + } + } +} + +func (schedulers *Schedulers) Stop() *Schedulers { + utils.RemoveConfigListener(schedulers.listenerId) + + if schedulers.DataRetention != nil && *utils.Cfg.DataRetentionSettings.Enable { + schedulers.DataRetention.Stop() + } + + l4g.Info("Stopped schedulers") + + return schedulers +} |