From 577ed27f1bb060080d311342047e31943a02ccbb Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Thu, 18 May 2017 15:05:57 -0400 Subject: PLT-6408 Framework for job server (#6404) * Added initial job server * Added job server to be ran as part of platform * Added test job to the enterprise repo * Fixed job server not loading license * Renamed job package to jobs * Fixed TE not being buildable * Added JobStatus table to database * Changed fields used by JobStatus * Added APIs to query job status * Added config change listener to server * Added option to run job server from Makefile * Added ability to enable/disable jobs from config * Commented out placeholder for search indexing job * Fixed govet * Removed debug messages and fixed job api init message --- app/job.go | 28 +++++++++++++++++++++ app/job_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/license.go | 27 +++++--------------- 3 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 app/job.go create mode 100644 app/job_test.go (limited to 'app') diff --git a/app/job.go b/app/job.go new file mode 100644 index 000000000..00439e4d2 --- /dev/null +++ b/app/job.go @@ -0,0 +1,28 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "github.com/mattermost/platform/model" +) + +func GetJobStatus(id string) (*model.JobStatus, *model.AppError) { + if result := <-Srv.Store.JobStatus().Get(id); result.Err != nil { + return nil, result.Err + } else { + return result.Data.(*model.JobStatus), nil + } +} + +func GetJobStatusesByTypePage(jobType string, page int, perPage int) ([]*model.JobStatus, *model.AppError) { + return GetJobStatusesByType(jobType, page*perPage, perPage) +} + +func GetJobStatusesByType(jobType string, offset int, limit int) ([]*model.JobStatus, *model.AppError) { + if result := <-Srv.Store.JobStatus().GetAllByTypePage(jobType, offset, limit); result.Err != nil { + return nil, result.Err + } else { + return result.Data.([]*model.JobStatus), nil + } +} diff --git a/app/job_test.go b/app/job_test.go new file mode 100644 index 000000000..20e9dee8a --- /dev/null +++ b/app/job_test.go @@ -0,0 +1,78 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "testing" + + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/store" +) + +func TestGetJobStatus(t *testing.T) { + Setup() + + status := &model.JobStatus{ + Id: model.NewId(), + Status: model.NewId(), + } + if result := <-Srv.Store.JobStatus().SaveOrUpdate(status); result.Err != nil { + t.Fatal(result.Err) + } + + defer Srv.Store.JobStatus().Delete(status.Id) + + if received, err := GetJobStatus(status.Id); err != nil { + t.Fatal(err) + } else if received.Id != status.Id || received.Status != status.Status { + t.Fatal("inccorrect job status received") + } +} + +func TestGetJobStatusesByType(t *testing.T) { + Setup() + + jobType := model.NewId() + + statuses := []*model.JobStatus{ + { + Id: model.NewId(), + Type: jobType, + StartAt: 1000, + }, + { + Id: model.NewId(), + Type: jobType, + StartAt: 999, + }, + { + Id: model.NewId(), + Type: jobType, + StartAt: 1001, + }, + } + + for _, status := range statuses { + store.Must(Srv.Store.JobStatus().SaveOrUpdate(status)) + defer Srv.Store.JobStatus().Delete(status.Id) + } + + if received, err := GetJobStatusesByType(jobType, 0, 2); err != nil { + t.Fatal(err) + } else if len(received) != 2 { + t.Fatal("received wrong number of statuses") + } else if received[0].Id != statuses[1].Id { + t.Fatal("should've received newest job first") + } else if received[1].Id != statuses[0].Id { + t.Fatal("should've received second newest job second") + } + + if received, err := GetJobStatusesByType(jobType, 2, 2); err != nil { + t.Fatal(err) + } else if len(received) != 1 { + t.Fatal("received wrong number of statuses") + } else if received[0].Id != statuses[2].Id { + t.Fatal("should've received oldest job last") + } +} diff --git a/app/license.go b/app/license.go index 7a00d7fb4..44b700d5b 100644 --- a/app/license.go +++ b/app/license.go @@ -4,7 +4,6 @@ package app import ( - "os" "strings" l4g "github.com/alecthomas/log4go" @@ -23,28 +22,14 @@ func LoadLicense() { if len(licenseId) != 26 { // Lets attempt to load the file from disk since it was missing from the DB - fileName := utils.GetLicenseFileLocation(*utils.Cfg.ServiceSettings.LicenseFileLocation) - - if _, err := os.Stat(fileName); err == nil { - l4g.Info("License key has not been uploaded. Loading license key from disk at %v", fileName) - licenseBytes := utils.GetLicenseFileFromDisk(fileName) - - if success, licenseStr := utils.ValidateLicense(licenseBytes); success { - licenseFileFromDisk := model.LicenseFromJson(strings.NewReader(licenseStr)) - licenseId = licenseFileFromDisk.Id - if _, err := SaveLicense(licenseBytes); err != nil { - l4g.Info("Failed to save license key loaded from disk err=%v", err.Error()) - return - } + license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk() + + if license != nil { + if _, err := SaveLicense(licenseBytes); err != nil { + l4g.Info("Failed to save license key loaded from disk err=%v", err.Error()) } else { - l4g.Error("Found license key at %v but it appears to be invalid.", fileName) - return + licenseId = license.Id } - - } else { - l4g.Info(utils.T("mattermost.load_license.find.warn")) - l4g.Debug("We could not find the license key in the database or on disk at %v", fileName) - return } } -- cgit v1.2.3-1-g7c22