/
runs.go
77 lines (67 loc) · 2.14 KB
/
runs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package repos
import (
"context"
"time"
"github.com/do87/poly/src/mesh/common"
"github.com/do87/poly/src/mesh/models"
)
// runsRepo is the repo for runs
type runsRepo repo
// Get returns run by uuid
func (r *runsRepo) Get(ctx context.Context, uuid string) (run models.Run, err error) {
result := r.db.First(&run, "uuid = ?", uuid)
if result.Error != nil {
return models.Run{}, result.Error
}
return
}
// List returns all runs
func (r *runsRepo) List(ctx context.Context) (keys []models.Run, err error) {
result := r.db.Order("created_at DESC").Find(&keys)
if result.Error != nil {
return keys, result.Error
}
return keys, nil
}
// ListPendingByAgentID pending runs for agent
func (r *runsRepo) ListPendingByAgentID(ctx context.Context, agentID string) (keys []models.Run, err error) {
result := r.db.Where("status = ?", common.RUN_STATUS_PENDING).Where("agent = ?", agentID).Order("created_at DESC").Find(&keys)
if result.Error != nil {
return keys, result.Error
}
return keys, nil
}
// ListCreated returns all new runs with unassigned agent
func (r *runsRepo) ListCreated(ctx context.Context) (keys []models.Run, err error) {
result := r.db.Where("status = ?", common.RUN_STATUS_CREATED).Order("created_at DESC").Find(&keys)
if result.Error != nil {
return keys, result.Error
}
return keys, nil
}
// ListPendingSince returns all runs in pending state since given time
func (r *runsRepo) ListPendingSince(ctx context.Context, t time.Time) (keys []models.Run, err error) {
result := r.db.
Where("status = ?", common.RUN_STATUS_PENDING).
Where("assigned_at > ?", t).
Order("created_at DESC").Find(&keys)
if result.Error != nil {
return keys, result.Error
}
return keys, nil
}
// Create a new run
func (r *runsRepo) Create(ctx context.Context, run models.Run) (models.Run, error) {
if result := r.db.Create(&run); result.Error != nil {
return run, result.Error
}
return run, nil
}
// Update updated a run by UUID
func (r *runsRepo) Update(ctx context.Context, run models.Run) (models.Run, error) {
m := &models.Run{}
if err := r.db.Model(m).Where("uuid = ?", run.UUID).Updates(run).Error; err != nil {
return run, err
}
return run, nil
}