remove jobs.Srv and other jobs-related globals (#7535)

Этот коммит содержится в:
Chris
2017-09-29 04:29:29 -05:00
коммит произвёл George Goldberg
родитель cb33179998
Коммит 4e79d2d4d0
16 изменённых файлов: 140 добавлений и 132 удалений

Просмотреть файл

@@ -23,7 +23,6 @@ import (
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/wsapi" "github.com/mattermost/mattermost-server/wsapi"
"github.com/mattermost/mattermost-server/jobs"
s3 "github.com/minio/minio-go" s3 "github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials" "github.com/minio/minio-go/pkg/credentials"
) )
@@ -76,8 +75,8 @@ func setupTestHelper(enterprise bool) *TestHelper {
utils.License().Features.SetDefaults() utils.License().Features.SetDefaults()
} }
if jobs.Srv.Store == nil { if th.App.Jobs.Store == nil {
jobs.Srv.Store = th.App.Srv.Store th.App.Jobs.Store = th.App.Srv.Store
} }
th.Client = th.CreateClient() th.Client = th.CreateClient()

Просмотреть файл

@@ -52,7 +52,7 @@ func createJob(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if job, err := app.CreateJob(job); err != nil { if job, err := c.App.CreateJob(job); err != nil {
c.Err = err c.Err = err
return return
} else { } else {
@@ -109,7 +109,7 @@ func cancelJob(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if err := app.CancelJob(c.Params.JobId); err != nil { if err := c.App.CancelJob(c.Params.JobId); err != nil {
c.Err = err c.Err = err
return return
} }

Просмотреть файл

@@ -14,7 +14,6 @@ import (
"net/http" "net/http"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/store/sqlstore" "github.com/mattermost/mattermost-server/store/sqlstore"
@@ -190,7 +189,7 @@ func (a *App) RecycleDatabaseConnection() {
l4g.Warn(utils.T("api.admin.recycle_db_start.warn")) l4g.Warn(utils.T("api.admin.recycle_db_start.warn"))
a.Srv.Store = store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Metrics), a.Metrics, a.Cluster) a.Srv.Store = store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Metrics), a.Metrics, a.Cluster)
jobs.Srv.Store = a.Srv.Store a.Jobs.Store = a.Srv.Store
time.Sleep(20 * time.Second) time.Sleep(20 * time.Second)
oldStore.Close() oldStore.Close()

Просмотреть файл

@@ -9,6 +9,8 @@ import (
"sync" "sync"
"github.com/mattermost/mattermost-server/einterfaces" "github.com/mattermost/mattermost-server/einterfaces"
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
"github.com/mattermost/mattermost-server/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin/pluginenv" "github.com/mattermost/mattermost-server/plugin/pluginenv"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
@@ -25,6 +27,8 @@ type App struct {
Hubs []*Hub Hubs []*Hub
HubsStopCheckingForDeadlock chan bool HubsStopCheckingForDeadlock chan bool
Jobs *jobs.JobServer
AccountMigration einterfaces.AccountMigrationInterface AccountMigration einterfaces.AccountMigrationInterface
Brand einterfaces.BrandInterface Brand einterfaces.BrandInterface
Cluster einterfaces.ClusterInterface Cluster einterfaces.ClusterInterface
@@ -36,7 +40,9 @@ type App struct {
Saml einterfaces.SamlInterface Saml einterfaces.SamlInterface
} }
var globalApp App var globalApp App = App{
Jobs: &jobs.JobServer{},
}
var initEnterprise sync.Once var initEnterprise sync.Once
@@ -65,6 +71,30 @@ func RegisterComplianceInterface(f func(*App) einterfaces.ComplianceInterface) {
complianceInterface = f complianceInterface = f
} }
var jobsDataRetentionInterface func(*App) ejobs.DataRetentionInterface
func RegisterJobsDataRetentionInterface(f func(*App) ejobs.DataRetentionInterface) {
jobsDataRetentionInterface = f
}
var jobsElasticsearchAggregatorInterface func(*App) ejobs.ElasticsearchAggregatorInterface
func RegisterJobsElasticsearchAggregatorInterface(f func(*App) ejobs.ElasticsearchAggregatorInterface) {
jobsElasticsearchAggregatorInterface = f
}
var jobsElasticsearchIndexerInterface func(*App) ejobs.ElasticsearchIndexerInterface
func RegisterJobsElasticsearchIndexerInterface(f func(*App) ejobs.ElasticsearchIndexerInterface) {
jobsElasticsearchIndexerInterface = f
}
var jobsLdapSyncInterface func(*App) ejobs.LdapSyncInterface
func RegisterJobsLdapSyncInterface(f func(*App) ejobs.LdapSyncInterface) {
jobsLdapSyncInterface = f
}
var ldapInterface func(*App) einterfaces.LdapInterface var ldapInterface func(*App) einterfaces.LdapInterface
func RegisterLdapInterface(f func(*App) einterfaces.LdapInterface) { func RegisterLdapInterface(f func(*App) einterfaces.LdapInterface) {
@@ -121,6 +151,19 @@ func (a *App) initEnterprise() {
a.Saml.ConfigureSP() a.Saml.ConfigureSP()
}) })
} }
if jobsDataRetentionInterface != nil {
a.Jobs.DataRetention = jobsDataRetentionInterface(a)
}
if jobsElasticsearchAggregatorInterface != nil {
a.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(a)
}
if jobsElasticsearchIndexerInterface != nil {
a.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(a)
}
if jobsLdapSyncInterface != nil {
a.Jobs.LdapSync = jobsLdapSyncInterface(a)
}
} }
func (a *App) Config() *model.Config { func (a *App) Config() *model.Config {

Просмотреть файл

@@ -4,7 +4,6 @@
package app package app
import ( import (
"github.com/mattermost/mattermost-server/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
) )
@@ -40,10 +39,10 @@ func (a *App) GetJobsByType(jobType string, offset int, limit int) ([]*model.Job
} }
} }
func CreateJob(job *model.Job) (*model.Job, *model.AppError) { func (a *App) CreateJob(job *model.Job) (*model.Job, *model.AppError) {
return jobs.CreateJob(job.Type, job.Data) return a.Jobs.CreateJob(job.Type, job.Data)
} }
func CancelJob(jobId string) *model.AppError { func (a *App) CancelJob(jobId string) *model.AppError {
return jobs.RequestCancellation(jobId) return a.Jobs.RequestCancellation(jobId)
} }

Просмотреть файл

@@ -8,7 +8,6 @@ import (
"syscall" "syscall"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/jobs"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/store/sqlstore" "github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -37,18 +36,18 @@ func jobserverCmdF(cmd *cobra.Command, args []string) {
} }
defer l4g.Close() defer l4g.Close()
jobs.Srv.Store = store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Metrics), a.Metrics, a.Cluster) a.Jobs.Store = store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Metrics), a.Metrics, a.Cluster)
defer jobs.Srv.Store.Close() defer a.Jobs.Store.Close()
jobs.Srv.LoadLicense() a.Jobs.LoadLicense()
// Run jobs // Run jobs
l4g.Info("Starting Mattermost job server") l4g.Info("Starting Mattermost job server")
if !noJobs { if !noJobs {
jobs.Srv.StartWorkers() a.Jobs.StartWorkers()
} }
if !noSchedule { if !noSchedule {
jobs.Srv.StartSchedulers() a.Jobs.StartSchedulers()
} }
var signalChan chan os.Signal = make(chan os.Signal) var signalChan chan os.Signal = make(chan os.Signal)
@@ -58,8 +57,8 @@ func jobserverCmdF(cmd *cobra.Command, args []string) {
// Cleanup anything that isn't handled by a defer statement // Cleanup anything that isn't handled by a defer statement
l4g.Info("Stopping Mattermost job server") l4g.Info("Stopping Mattermost job server")
jobs.Srv.StopSchedulers() a.Jobs.StopSchedulers()
jobs.Srv.StopWorkers() a.Jobs.StopWorkers()
l4g.Info("Stopped Mattermost job server") l4g.Info("Stopped Mattermost job server")
} }

Просмотреть файл

@@ -13,7 +13,6 @@ import (
"github.com/mattermost/mattermost-server/api" "github.com/mattermost/mattermost-server/api"
"github.com/mattermost/mattermost-server/api4" "github.com/mattermost/mattermost-server/api4"
"github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/jobs"
"github.com/mattermost/mattermost-server/manualtesting" "github.com/mattermost/mattermost-server/manualtesting"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
@@ -138,12 +137,12 @@ func runServer(configFileLocation string) {
} }
} }
jobs.Srv.Store = a.Srv.Store a.Jobs.Store = a.Srv.Store
if *utils.Cfg.JobSettings.RunJobs { if *utils.Cfg.JobSettings.RunJobs {
jobs.Srv.StartWorkers() a.Jobs.StartWorkers()
} }
if *utils.Cfg.JobSettings.RunScheduler { if *utils.Cfg.JobSettings.RunScheduler {
jobs.Srv.StartSchedulers() a.Jobs.StartSchedulers()
} }
// wait for kill signal before attempting to gracefully shutdown // wait for kill signal before attempting to gracefully shutdown
@@ -160,8 +159,8 @@ func runServer(configFileLocation string) {
a.Metrics.StopServer() a.Metrics.StopServer()
} }
jobs.Srv.StopSchedulers() a.Jobs.StopSchedulers()
jobs.Srv.StopWorkers() a.Jobs.StopWorkers()
a.StopServer() a.StopServer()
} }

Просмотреть файл

@@ -11,13 +11,3 @@ type DataRetentionInterface interface {
MakeWorker() model.Worker MakeWorker() model.Worker
MakeScheduler() model.Scheduler MakeScheduler() model.Scheduler
} }
var theDataRetentionInterface DataRetentionInterface
func RegisterDataRetentionInterface(newInterface DataRetentionInterface) {
theDataRetentionInterface = newInterface
}
func GetDataRetentionInterface() DataRetentionInterface {
return theDataRetentionInterface
}

Просмотреть файл

@@ -11,27 +11,7 @@ type ElasticsearchIndexerInterface interface {
MakeWorker() model.Worker MakeWorker() model.Worker
} }
var theElasticsearchIndexerInterface ElasticsearchIndexerInterface
func RegisterElasticsearchIndexerInterface(newInterface ElasticsearchIndexerInterface) {
theElasticsearchIndexerInterface = newInterface
}
func GetElasticsearchIndexerInterface() ElasticsearchIndexerInterface {
return theElasticsearchIndexerInterface
}
type ElasticsearchAggregatorInterface interface { type ElasticsearchAggregatorInterface interface {
MakeWorker() model.Worker MakeWorker() model.Worker
MakeScheduler() model.Scheduler MakeScheduler() model.Scheduler
} }
var theElasticsearchAggregatorInterface ElasticsearchAggregatorInterface
func RegisterElasticsearchAggregatorInterface(newInterface ElasticsearchAggregatorInterface) {
theElasticsearchAggregatorInterface = newInterface
}
func GetElasticsearchAggregatorInterface() ElasticsearchAggregatorInterface {
return theElasticsearchAggregatorInterface
}

Просмотреть файл

@@ -11,13 +11,3 @@ type LdapSyncInterface interface {
MakeWorker() model.Worker MakeWorker() model.Worker
MakeScheduler() model.Scheduler MakeScheduler() model.Scheduler
} }
var theLdapSyncInterface LdapSyncInterface
func RegisterLdapSyncInterface(newInterface LdapSyncInterface) {
theLdapSyncInterface = newInterface
}
func GetLdapSyncInterface() LdapSyncInterface {
return theLdapSyncInterface
}

Просмотреть файл

@@ -18,7 +18,7 @@ const (
CANCEL_WATCHER_POLLING_INTERVAL = 5000 CANCEL_WATCHER_POLLING_INTERVAL = 5000
) )
func CreateJob(jobType string, jobData map[string]string) (*model.Job, *model.AppError) { func (srv *JobServer) CreateJob(jobType string, jobData map[string]string) (*model.Job, *model.AppError) {
job := model.Job{ job := model.Job{
Id: model.NewId(), Id: model.NewId(),
Type: jobType, Type: jobType,
@@ -31,23 +31,23 @@ func CreateJob(jobType string, jobData map[string]string) (*model.Job, *model.Ap
return nil, err return nil, err
} }
if result := <-Srv.Store.Job().Save(&job); result.Err != nil { if result := <-srv.Store.Job().Save(&job); result.Err != nil {
return nil, result.Err return nil, result.Err
} }
return &job, nil return &job, nil
} }
func GetJob(id string) (*model.Job, *model.AppError) { func (srv *JobServer) GetJob(id string) (*model.Job, *model.AppError) {
if result := <-Srv.Store.Job().Get(id); result.Err != nil { if result := <-srv.Store.Job().Get(id); result.Err != nil {
return nil, result.Err return nil, result.Err
} else { } else {
return result.Data.(*model.Job), nil return result.Data.(*model.Job), nil
} }
} }
func ClaimJob(job *model.Job) (bool, *model.AppError) { func (srv *JobServer) ClaimJob(job *model.Job) (bool, *model.AppError) {
if result := <-Srv.Store.Job().UpdateStatusOptimistically(job.Id, model.JOB_STATUS_PENDING, model.JOB_STATUS_IN_PROGRESS); result.Err != nil { if result := <-srv.Store.Job().UpdateStatusOptimistically(job.Id, model.JOB_STATUS_PENDING, model.JOB_STATUS_IN_PROGRESS); result.Err != nil {
return false, result.Err return false, result.Err
} else { } else {
success := result.Data.(bool) success := result.Data.(bool)
@@ -55,25 +55,25 @@ func ClaimJob(job *model.Job) (bool, *model.AppError) {
} }
} }
func SetJobProgress(job *model.Job, progress int64) *model.AppError { func (srv *JobServer) SetJobProgress(job *model.Job, progress int64) *model.AppError {
job.Status = model.JOB_STATUS_IN_PROGRESS job.Status = model.JOB_STATUS_IN_PROGRESS
job.Progress = progress job.Progress = progress
if result := <-Srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_IN_PROGRESS); result.Err != nil { if result := <-srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_IN_PROGRESS); result.Err != nil {
return result.Err return result.Err
} else { } else {
return nil return nil
} }
} }
func SetJobSuccess(job *model.Job) *model.AppError { func (srv *JobServer) SetJobSuccess(job *model.Job) *model.AppError {
result := <-Srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_SUCCESS) result := <-srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_SUCCESS)
return result.Err return result.Err
} }
func SetJobError(job *model.Job, jobError *model.AppError) *model.AppError { func (srv *JobServer) SetJobError(job *model.Job, jobError *model.AppError) *model.AppError {
if jobError == nil { if jobError == nil {
result := <-Srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_ERROR) result := <-srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_ERROR)
return result.Err return result.Err
} }
@@ -85,11 +85,11 @@ func SetJobError(job *model.Job, jobError *model.AppError) *model.AppError {
jobError.Translate(utils.T) jobError.Translate(utils.T)
job.Data["error"] = jobError.Message + " (" + jobError.DetailedError + ")" job.Data["error"] = jobError.Message + " (" + jobError.DetailedError + ")"
if result := <-Srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_IN_PROGRESS); result.Err != nil { if result := <-srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_IN_PROGRESS); result.Err != nil {
return result.Err return result.Err
} else { } else {
if !result.Data.(bool) { if !result.Data.(bool) {
if result := <-Srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_CANCEL_REQUESTED); result.Err != nil { if result := <-srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_CANCEL_REQUESTED); result.Err != nil {
return result.Err return result.Err
} else { } else {
if !result.Data.(bool) { if !result.Data.(bool) {
@@ -102,19 +102,19 @@ func SetJobError(job *model.Job, jobError *model.AppError) *model.AppError {
return nil return nil
} }
func SetJobCanceled(job *model.Job) *model.AppError { func (srv *JobServer) SetJobCanceled(job *model.Job) *model.AppError {
result := <-Srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_CANCELED) result := <-srv.Store.Job().UpdateStatus(job.Id, model.JOB_STATUS_CANCELED)
return result.Err return result.Err
} }
func RequestCancellation(jobId string) *model.AppError { func (srv *JobServer) RequestCancellation(jobId string) *model.AppError {
if result := <-Srv.Store.Job().UpdateStatusOptimistically(jobId, model.JOB_STATUS_PENDING, model.JOB_STATUS_CANCELED); result.Err != nil { if result := <-srv.Store.Job().UpdateStatusOptimistically(jobId, model.JOB_STATUS_PENDING, model.JOB_STATUS_CANCELED); result.Err != nil {
return result.Err return result.Err
} else if result.Data.(bool) { } else if result.Data.(bool) {
return nil return nil
} }
if result := <-Srv.Store.Job().UpdateStatusOptimistically(jobId, model.JOB_STATUS_IN_PROGRESS, model.JOB_STATUS_CANCEL_REQUESTED); result.Err != nil { if result := <-srv.Store.Job().UpdateStatusOptimistically(jobId, model.JOB_STATUS_IN_PROGRESS, model.JOB_STATUS_CANCEL_REQUESTED); result.Err != nil {
return result.Err return result.Err
} else if result.Data.(bool) { } else if result.Data.(bool) {
return nil return nil
@@ -123,7 +123,7 @@ func RequestCancellation(jobId string) *model.AppError {
return model.NewAppError("Jobs.RequestCancellation", "jobs.request_cancellation.status.error", nil, "id="+jobId, http.StatusInternalServerError) return model.NewAppError("Jobs.RequestCancellation", "jobs.request_cancellation.status.error", nil, "id="+jobId, http.StatusInternalServerError)
} }
func CancellationWatcher(ctx context.Context, jobId string, cancelChan chan interface{}) { func (srv *JobServer) CancellationWatcher(ctx context.Context, jobId string, cancelChan chan interface{}) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@@ -131,7 +131,7 @@ func CancellationWatcher(ctx context.Context, jobId string, cancelChan chan inte
return return
case <-time.After(CANCEL_WATCHER_POLLING_INTERVAL * time.Millisecond): case <-time.After(CANCEL_WATCHER_POLLING_INTERVAL * time.Millisecond):
l4g.Debug("CancellationWatcher for Job: %v polling.", jobId) l4g.Debug("CancellationWatcher for Job: %v polling.", jobId)
if result := <-Srv.Store.Job().Get(jobId); result.Err == nil { if result := <-srv.Store.Job().Get(jobId); result.Err == nil {
jobStatus := result.Data.(*model.Job) jobStatus := result.Data.(*model.Job)
if jobStatus.Status == model.JOB_STATUS_CANCEL_REQUESTED { if jobStatus.Status == model.JOB_STATUS_CANCEL_REQUESTED {
close(cancelChan) close(cancelChan)
@@ -152,16 +152,16 @@ func GenerateNextStartDateTime(now time.Time, nextStartTime time.Time) *time.Tim
return &nextTime return &nextTime
} }
func CheckForPendingJobsByType(jobType string) (bool, *model.AppError) { func (srv *JobServer) CheckForPendingJobsByType(jobType string) (bool, *model.AppError) {
if result := <-Srv.Store.Job().GetCountByStatusAndType(model.JOB_STATUS_PENDING, jobType); result.Err != nil { if result := <-srv.Store.Job().GetCountByStatusAndType(model.JOB_STATUS_PENDING, jobType); result.Err != nil {
return false, result.Err return false, result.Err
} else { } else {
return result.Data.(int64) > 0, nil return result.Data.(int64) > 0, nil
} }
} }
func GetLastSuccessfulJobByType(jobType string) (*model.Job, *model.AppError) { func (srv *JobServer) GetLastSuccessfulJobByType(jobType string) (*model.Job, *model.AppError) {
if result := <-Srv.Store.Job().GetNewestJobByStatusAndType(model.JOB_STATUS_SUCCESS, jobType); result.Err != nil { if result := <-srv.Store.Job().GetNewestJobByStatusAndType(model.JOB_STATUS_SUCCESS, jobType); result.Err != nil {
return nil, result.Err return nil, result.Err
} else { } else {
return result.Data.(*model.Job), nil return result.Data.(*model.Job), nil

Просмотреть файл

@@ -16,6 +16,7 @@ const (
) )
type Watcher struct { type Watcher struct {
srv *JobServer
workers *Workers workers *Workers
stop chan bool stop chan bool
@@ -23,12 +24,13 @@ type Watcher struct {
pollingInterval int pollingInterval int
} }
func MakeWatcher(workers *Workers, pollingInterval int) *Watcher { func (srv *JobServer) MakeWatcher(workers *Workers, pollingInterval int) *Watcher {
return &Watcher{ return &Watcher{
stop: make(chan bool, 1), stop: make(chan bool, 1),
stopped: make(chan bool, 1), stopped: make(chan bool, 1),
pollingInterval: pollingInterval, pollingInterval: pollingInterval,
workers: workers, workers: workers,
srv: srv,
} }
} }
@@ -63,7 +65,7 @@ func (watcher *Watcher) Stop() {
} }
func (watcher *Watcher) PollAndNotify() { func (watcher *Watcher) PollAndNotify() {
if result := <-Srv.Store.Job().GetAllByStatus(model.JOB_STATUS_PENDING); result.Err != nil { if result := <-watcher.srv.Store.Job().GetAllByStatus(model.JOB_STATUS_PENDING); result.Err != nil {
l4g.Error("Error occured getting all pending statuses: %v", result.Err.Error()) l4g.Error("Error occured getting all pending statuses: %v", result.Err.Error())
} else { } else {
jobs := result.Data.([]*model.Job) jobs := result.Data.([]*model.Job)

Просмотреть файл

@@ -9,7 +9,6 @@ import (
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
) )
@@ -20,28 +19,31 @@ type Schedulers struct {
configChanged chan *model.Config configChanged chan *model.Config
listenerId string listenerId string
startOnce sync.Once startOnce sync.Once
jobs *JobServer
schedulers []model.Scheduler schedulers []model.Scheduler
nextRunTimes []*time.Time nextRunTimes []*time.Time
} }
func InitSchedulers() *Schedulers { func (srv *JobServer) InitSchedulers() *Schedulers {
l4g.Debug("Initialising schedulers.") l4g.Debug("Initialising schedulers.")
schedulers := &Schedulers{ schedulers := &Schedulers{
stop: make(chan bool), stop: make(chan bool),
stopped: make(chan bool), stopped: make(chan bool),
configChanged: make(chan *model.Config), configChanged: make(chan *model.Config),
jobs: srv,
} }
if dataRetentionInterface := ejobs.GetDataRetentionInterface(); dataRetentionInterface != nil { if dataRetentionInterface := srv.DataRetention; dataRetentionInterface != nil {
schedulers.schedulers = append(schedulers.schedulers, dataRetentionInterface.MakeScheduler()) schedulers.schedulers = append(schedulers.schedulers, dataRetentionInterface.MakeScheduler())
} }
if elasticsearchAggregatorInterface := ejobs.GetElasticsearchAggregatorInterface(); elasticsearchAggregatorInterface != nil { if elasticsearchAggregatorInterface := srv.ElasticsearchAggregator; elasticsearchAggregatorInterface != nil {
schedulers.schedulers = append(schedulers.schedulers, elasticsearchAggregatorInterface.MakeScheduler()) schedulers.schedulers = append(schedulers.schedulers, elasticsearchAggregatorInterface.MakeScheduler())
} }
if ldapSyncInterface := ejobs.GetLdapSyncInterface(); ldapSyncInterface != nil { if ldapSyncInterface := srv.LdapSync; ldapSyncInterface != nil {
schedulers.schedulers = append(schedulers.schedulers, ldapSyncInterface.MakeScheduler()) schedulers.schedulers = append(schedulers.schedulers, ldapSyncInterface.MakeScheduler())
} }
@@ -124,7 +126,7 @@ func (schedulers *Schedulers) setNextRunTime(cfg *model.Config, idx int, now tim
scheduler := schedulers.schedulers[idx] scheduler := schedulers.schedulers[idx]
if !pendingJobs { if !pendingJobs {
if pj, err := CheckForPendingJobsByType(scheduler.JobType()); err != nil { if pj, err := schedulers.jobs.CheckForPendingJobsByType(scheduler.JobType()); err != nil {
l4g.Error("Failed to set next job run time: " + err.Error()) l4g.Error("Failed to set next job run time: " + err.Error())
schedulers.nextRunTimes[idx] = nil schedulers.nextRunTimes[idx] = nil
return return
@@ -133,7 +135,7 @@ func (schedulers *Schedulers) setNextRunTime(cfg *model.Config, idx int, now tim
} }
} }
lastSuccessfulJob, err := GetLastSuccessfulJobByType(scheduler.JobType()) lastSuccessfulJob, err := schedulers.jobs.GetLastSuccessfulJobByType(scheduler.JobType())
if err != nil { if err != nil {
l4g.Error("Failed to set next job run time: " + err.Error()) l4g.Error("Failed to set next job run time: " + err.Error())
schedulers.nextRunTimes[idx] = nil schedulers.nextRunTimes[idx] = nil
@@ -145,12 +147,12 @@ func (schedulers *Schedulers) setNextRunTime(cfg *model.Config, idx int, now tim
} }
func (schedulers *Schedulers) scheduleJob(cfg *model.Config, scheduler model.Scheduler) (*model.Job, *model.AppError) { func (schedulers *Schedulers) scheduleJob(cfg *model.Config, scheduler model.Scheduler) (*model.Job, *model.AppError) {
pendingJobs, err := CheckForPendingJobsByType(scheduler.JobType()) pendingJobs, err := schedulers.jobs.CheckForPendingJobsByType(scheduler.JobType())
if err != nil { if err != nil {
return nil, err return nil, err
} }
lastSuccessfulJob, err2 := GetLastSuccessfulJobByType(scheduler.JobType()) lastSuccessfulJob, err2 := schedulers.jobs.GetLastSuccessfulJobByType(scheduler.JobType())
if err2 != nil { if err2 != nil {
return nil, err return nil, err
} }

Просмотреть файл

@@ -5,6 +5,8 @@ package jobs
import ( import (
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
@@ -14,13 +16,16 @@ type JobServer struct {
Store store.Store Store store.Store
Workers *Workers Workers *Workers
Schedulers *Schedulers Schedulers *Schedulers
DataRetention ejobs.DataRetentionInterface
ElasticsearchAggregator ejobs.ElasticsearchAggregatorInterface
ElasticsearchIndexer ejobs.ElasticsearchIndexerInterface
LdapSync ejobs.LdapSyncInterface
} }
var Srv JobServer func (srv *JobServer) LoadLicense() {
func (server *JobServer) LoadLicense() {
licenseId := "" licenseId := ""
if result := <-server.Store.System().Get(); result.Err == nil { if result := <-srv.Store.System().Get(); result.Err == nil {
props := result.Data.(model.StringMap) props := result.Data.(model.StringMap)
licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID] licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID]
} }
@@ -31,7 +36,7 @@ func (server *JobServer) LoadLicense() {
// Lets attempt to load the file from disk since it was missing from the DB // Lets attempt to load the file from disk since it was missing from the DB
_, licenseBytes = utils.GetAndValidateLicenseFileFromDisk() _, licenseBytes = utils.GetAndValidateLicenseFileFromDisk()
} else { } else {
if result := <-server.Store.License().Get(licenseId); result.Err == nil { if result := <-srv.Store.License().Get(licenseId); result.Err == nil {
record := result.Data.(*model.LicenseRecord) record := result.Data.(*model.LicenseRecord)
licenseBytes = []byte(record.Bytes) licenseBytes = []byte(record.Bytes)
l4g.Info("License key valid unlocking enterprise features.") l4g.Info("License key valid unlocking enterprise features.")
@@ -48,22 +53,22 @@ func (server *JobServer) LoadLicense() {
} }
} }
func (server *JobServer) StartWorkers() { func (srv *JobServer) StartWorkers() {
Srv.Workers = InitWorkers().Start() srv.Workers = srv.InitWorkers().Start()
} }
func (server *JobServer) StartSchedulers() { func (srv *JobServer) StartSchedulers() {
Srv.Schedulers = InitSchedulers().Start() srv.Schedulers = srv.InitSchedulers().Start()
} }
func (server *JobServer) StopWorkers() { func (srv *JobServer) StopWorkers() {
if Srv.Workers != nil { if srv.Workers != nil {
Srv.Workers.Stop() srv.Workers.Stop()
} }
} }
func (server *JobServer) StopSchedulers() { func (srv *JobServer) StopSchedulers() {
if Srv.Schedulers != nil { if srv.Schedulers != nil {
Srv.Schedulers.Stop() srv.Schedulers.Stop()
} }
} }

Просмотреть файл

@@ -12,14 +12,16 @@ import (
) )
type TestWorker struct { type TestWorker struct {
srv *JobServer
name string name string
stop chan bool stop chan bool
stopped chan bool stopped chan bool
jobs chan model.Job jobs chan model.Job
} }
func MakeTestWorker(name string) *TestWorker { func (srv *JobServer) MakeTestWorker(name string) *TestWorker {
return &TestWorker{ return &TestWorker{
srv: srv,
name: name, name: name,
stop: make(chan bool, 1), stop: make(chan bool, 1),
stopped: make(chan bool, 1), stopped: make(chan bool, 1),
@@ -48,7 +50,7 @@ func (worker *TestWorker) Run() {
} }
func (worker *TestWorker) DoJob(job *model.Job) { func (worker *TestWorker) DoJob(job *model.Job) {
if claimed, err := ClaimJob(job); err != nil { if claimed, err := worker.srv.ClaimJob(job); err != nil {
l4g.Error("Job: %v: Error occurred while trying to claim job: %v", job.Id, err.Error()) l4g.Error("Job: %v: Error occurred while trying to claim job: %v", job.Id, err.Error())
return return
} else if !claimed { } else if !claimed {
@@ -57,7 +59,7 @@ func (worker *TestWorker) DoJob(job *model.Job) {
cancelCtx, cancelCancelWatcher := context.WithCancel(context.Background()) cancelCtx, cancelCancelWatcher := context.WithCancel(context.Background())
cancelWatcherChan := make(chan interface{}, 1) cancelWatcherChan := make(chan interface{}, 1)
go CancellationWatcher(cancelCtx, job.Id, cancelWatcherChan) go worker.srv.CancellationWatcher(cancelCtx, job.Id, cancelWatcherChan)
defer cancelCancelWatcher() defer cancelCancelWatcher()
@@ -66,13 +68,13 @@ func (worker *TestWorker) DoJob(job *model.Job) {
select { select {
case <-cancelWatcherChan: case <-cancelWatcherChan:
l4g.Debug("Job %v: Job has been canceled via CancellationWatcher.", job.Id) l4g.Debug("Job %v: Job has been canceled via CancellationWatcher.", job.Id)
if err := SetJobCanceled(job); err != nil { if err := worker.srv.SetJobCanceled(job); err != nil {
l4g.Error("Failed to mark job: %v as canceled. Error: %v", job.Id, err.Error()) l4g.Error("Failed to mark job: %v as canceled. Error: %v", job.Id, err.Error())
} }
return return
case <-worker.stop: case <-worker.stop:
l4g.Debug("Job %v: Job has been canceled via Worker Stop.", job.Id) l4g.Debug("Job %v: Job has been canceled via Worker Stop.", job.Id)
if err := SetJobCanceled(job); err != nil { if err := worker.srv.SetJobCanceled(job); err != nil {
l4g.Error("Failed to mark job: %v as canceled. Error: %v", job.Id, err.Error()) l4g.Error("Failed to mark job: %v as canceled. Error: %v", job.Id, err.Error())
} }
return return
@@ -80,12 +82,12 @@ func (worker *TestWorker) DoJob(job *model.Job) {
counter++ counter++
if counter > 10 { if counter > 10 {
l4g.Debug("Job %v: Job completed.", job.Id) l4g.Debug("Job %v: Job completed.", job.Id)
if err := SetJobSuccess(job); err != nil { if err := worker.srv.SetJobSuccess(job); err != nil {
l4g.Error("Failed to mark job: %v as succeeded. Error: %v", job.Id, err.Error()) l4g.Error("Failed to mark job: %v as succeeded. Error: %v", job.Id, err.Error())
} }
return return
} else { } else {
if err := SetJobProgress(job, int64(counter*10)); err != nil { if err := worker.srv.SetJobProgress(job, int64(counter*10)); err != nil {
l4g.Error("Job: %v: an error occured while trying to set job progress: %v", job.Id, err.Error()) l4g.Error("Job: %v: an error occured while trying to set job progress: %v", job.Id, err.Error())
} }
} }

Просмотреть файл

@@ -7,7 +7,6 @@ import (
"sync" "sync"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
) )
@@ -24,23 +23,23 @@ type Workers struct {
listenerId string listenerId string
} }
func InitWorkers() *Workers { func (srv *JobServer) InitWorkers() *Workers {
workers := &Workers{} workers := &Workers{}
workers.Watcher = MakeWatcher(workers, DEFAULT_WATCHER_POLLING_INTERVAL) workers.Watcher = srv.MakeWatcher(workers, DEFAULT_WATCHER_POLLING_INTERVAL)
if dataRetentionInterface := ejobs.GetDataRetentionInterface(); dataRetentionInterface != nil { if dataRetentionInterface := srv.DataRetention; dataRetentionInterface != nil {
workers.DataRetention = dataRetentionInterface.MakeWorker() workers.DataRetention = dataRetentionInterface.MakeWorker()
} }
if elasticsearchIndexerInterface := ejobs.GetElasticsearchIndexerInterface(); elasticsearchIndexerInterface != nil { if elasticsearchIndexerInterface := srv.ElasticsearchIndexer; elasticsearchIndexerInterface != nil {
workers.ElasticsearchIndexing = elasticsearchIndexerInterface.MakeWorker() workers.ElasticsearchIndexing = elasticsearchIndexerInterface.MakeWorker()
} }
if elasticsearchAggregatorInterface := ejobs.GetElasticsearchAggregatorInterface(); elasticsearchAggregatorInterface != nil { if elasticsearchAggregatorInterface := srv.ElasticsearchAggregator; elasticsearchAggregatorInterface != nil {
workers.ElasticsearchAggregation = elasticsearchAggregatorInterface.MakeWorker() workers.ElasticsearchAggregation = elasticsearchAggregatorInterface.MakeWorker()
} }
if ldapSyncInterface := ejobs.GetLdapSyncInterface(); ldapSyncInterface != nil { if ldapSyncInterface := srv.LdapSync; ldapSyncInterface != nil {
workers.LdapSync = ldapSyncInterface.MakeWorker() workers.LdapSync = ldapSyncInterface.MakeWorker()
} }