summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorGeorge Goldberg <george@gberg.me>2017-07-20 16:25:35 +0100
committerChristopher Speller <crspeller@gmail.com>2017-07-20 08:25:35 -0700
commit6c6f2a1138447777bbf46cc2c40e1b3c47204466 (patch)
treec2630ad79a7bc12d6c0c0233098e29d6811cb99b /store
parent5ae701d133f713363e52b9cc6aa01579c81ebab4 (diff)
downloadchat-6c6f2a1138447777bbf46cc2c40e1b3c47204466.tar.gz
chat-6c6f2a1138447777bbf46cc2c40e1b3c47204466.tar.bz2
chat-6c6f2a1138447777bbf46cc2c40e1b3c47204466.zip
PLT-6595-Server: Job Management APIs. (#6931)
* PLT-6595-Server: Job Management APIs. * MANAGE_JOBS Permission * Fix test.
Diffstat (limited to 'store')
-rw-r--r--store/sql_job_store.go38
-rw-r--r--store/sql_job_store_test.go76
-rw-r--r--store/store.go1
3 files changed, 101 insertions, 14 deletions
diff --git a/store/sql_job_store.go b/store/sql_job_store.go
index c00e37d86..e287edad6 100644
--- a/store/sql_job_store.go
+++ b/store/sql_job_store.go
@@ -210,6 +210,38 @@ func (jss SqlJobStore) Get(id string) StoreChannel {
return storeChannel
}
+func (jss SqlJobStore) GetAllPage(offset int, limit int) StoreChannel {
+ storeChannel := make(StoreChannel, 1)
+
+ go func() {
+ result := StoreResult{}
+
+ var statuses []*model.Job
+
+ if _, err := jss.GetReplica().Select(&statuses,
+ `SELECT
+ *
+ FROM
+ Jobs
+ ORDER BY
+ CreateAt DESC
+ LIMIT
+ :Limit
+ OFFSET
+ :Offset`, map[string]interface{}{"Limit": limit, "Offset": offset}); err != nil {
+ result.Err = model.NewLocAppError("SqlJobStore.GetAllPage",
+ "store.sql_job.get_all.app_error", nil, err.Error())
+ } else {
+ result.Data = statuses
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (jss SqlJobStore) GetAllByType(jobType string) StoreChannel {
storeChannel := make(StoreChannel, 1)
@@ -224,7 +256,9 @@ func (jss SqlJobStore) GetAllByType(jobType string) StoreChannel {
FROM
Jobs
WHERE
- Type = :Type`, map[string]interface{}{"Type": jobType}); err != nil {
+ Type = :Type
+ ORDER BY
+ CreateAt DESC`, map[string]interface{}{"Type": jobType}); err != nil {
result.Err = model.NewLocAppError("SqlJobStore.GetAllByType",
"store.sql_job.get_all.app_error", nil, "Type="+jobType+", "+err.Error())
} else {
@@ -254,7 +288,7 @@ func (jss SqlJobStore) GetAllByTypePage(jobType string, offset int, limit int) S
WHERE
Type = :Type
ORDER BY
- StartAt ASC
+ CreateAt DESC
LIMIT
:Limit
OFFSET
diff --git a/store/sql_job_store_test.go b/store/sql_job_store_test.go
index edf09a4c0..97e95ab92 100644
--- a/store/sql_job_store_test.go
+++ b/store/sql_job_store_test.go
@@ -82,19 +82,24 @@ func TestJobGetAllByTypePage(t *testing.T) {
jobs := []*model.Job{
{
- Id: model.NewId(),
- Type: jobType,
- StartAt: 1000,
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: 1000,
+ },
+ {
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: 999,
},
{
- Id: model.NewId(),
- Type: jobType,
- StartAt: 999,
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: 1001,
},
{
- Id: model.NewId(),
- Type: jobType,
- StartAt: 1001,
+ Id: model.NewId(),
+ Type: model.NewId(),
+ CreateAt: 1002,
},
}
@@ -107,7 +112,7 @@ func TestJobGetAllByTypePage(t *testing.T) {
t.Fatal(result.Err)
} else if received := result.Data.([]*model.Job); len(received) != 2 {
t.Fatal("received wrong number of jobs")
- } else if received[0].Id != jobs[1].Id {
+ } else if received[0].Id != jobs[2].Id {
t.Fatal("should've received newest job first")
} else if received[1].Id != jobs[0].Id {
t.Fatal("should've received second newest job second")
@@ -117,7 +122,54 @@ func TestJobGetAllByTypePage(t *testing.T) {
t.Fatal(result.Err)
} else if received := result.Data.([]*model.Job); len(received) != 1 {
t.Fatal("received wrong number of jobs")
+ } else if received[0].Id != jobs[1].Id {
+ t.Fatal("should've received oldest job last")
+ }
+}
+
+func TestJobGetAllPage(t *testing.T) {
+ Setup()
+
+ jobType := model.NewId()
+
+ jobs := []*model.Job{
+ {
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: model.GetMillis() + 1,
+ },
+ {
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: model.GetMillis(),
+ },
+ {
+ Id: model.NewId(),
+ Type: jobType,
+ CreateAt: model.GetMillis() + 2,
+ },
+ }
+
+ for _, job := range jobs {
+ Must(store.Job().Save(job))
+ defer store.Job().Delete(job.Id)
+ }
+
+ if result := <-store.Job().GetAllPage(0, 2); result.Err != nil {
+ t.Fatal(result.Err)
+ } else if received := result.Data.([]*model.Job); len(received) != 2 {
+ t.Fatal("received wrong number of jobs")
} else if received[0].Id != jobs[2].Id {
+ t.Fatal("should've received newest job first")
+ } else if received[1].Id != jobs[0].Id {
+ t.Fatal("should've received second newest job second")
+ }
+
+ if result := <-store.Job().GetAllPage(2, 2); result.Err != nil {
+ t.Fatal(result.Err)
+ } else if received := result.Data.([]*model.Job); len(received) < 1 {
+ t.Fatal("received wrong number of jobs")
+ } else if received[0].Id != jobs[1].Id {
t.Fatal("should've received oldest job last")
}
}
@@ -331,11 +383,11 @@ func TestJobUpdateStatusUpdateStatusOptimistically(t *testing.T) {
func TestJobDelete(t *testing.T) {
Setup()
- status := Must(store.Job().Save(&model.Job{
+ job := Must(store.Job().Save(&model.Job{
Id: model.NewId(),
})).(*model.Job)
- if result := <-store.Job().Delete(status.Id); result.Err != nil {
+ if result := <-store.Job().Delete(job.Id); result.Err != nil {
t.Fatal(result.Err)
}
}
diff --git a/store/store.go b/store/store.go
index 062ed0fbd..ab3d97d9b 100644
--- a/store/store.go
+++ b/store/store.go
@@ -391,6 +391,7 @@ type JobStore interface {
UpdateStatus(id string, status string) StoreChannel
UpdateStatusOptimistically(id string, currentStatus string, newStatus string) StoreChannel
Get(id string) StoreChannel
+ GetAllPage(offset int, limit int) StoreChannel
GetAllByType(jobType string) StoreChannel
GetAllByTypePage(jobType string, offset int, limit int) StoreChannel
GetAllByStatus(status string) StoreChannel