This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
schedulable_entity_repo.go
150 lines (130 loc) · 4.39 KB
/
schedulable_entity_repo.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gormimpl
import (
"context"
"fmt"
"errors"
adminErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/scheduler/repositories/interfaces"
"github.com/flyteorg/flyteadmin/scheduler/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/promutils"
"gorm.io/gorm"
)
// SchedulableEntityRepo Implementation of SchedulableEntityRepoInterface.
type SchedulableEntityRepo struct {
db *gorm.DB
errorTransformer adminErrors.ErrorTransformer
metrics gormMetrics
}
func (r *SchedulableEntityRepo) Create(ctx context.Context, input models.SchedulableEntity) error {
timer := r.metrics.GetDuration.Start()
var record models.SchedulableEntity
tx := r.db.Omit("id").FirstOrCreate(&record, input)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *SchedulableEntityRepo) Activate(ctx context.Context, input models.SchedulableEntity) error {
var schedulableEntity models.SchedulableEntity
timer := r.metrics.GetDuration.Start()
// Find the existence of a scheduled entity
tx := r.db.Where(&models.SchedulableEntity{
SchedulableEntityKey: models.SchedulableEntityKey{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
},
}).Take(&schedulableEntity)
timer.Stop()
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
// Not found and hence create one
return r.Create(ctx, input)
}
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
// Activate the already existing schedule
return activateOrDeactivate(r, input.SchedulableEntityKey, true)
}
func (r *SchedulableEntityRepo) Deactivate(ctx context.Context, ID models.SchedulableEntityKey) error {
// Activate the schedule
return activateOrDeactivate(r, ID, false)
}
func (r *SchedulableEntityRepo) GetAll(ctx context.Context) ([]models.SchedulableEntity, error) {
var schedulableEntities []models.SchedulableEntity
timer := r.metrics.GetDuration.Start()
tx := r.db.Find(&schedulableEntities)
timer.Stop()
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil,
fmt.Errorf("no active schedulable entities found")
}
return nil, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return schedulableEntities, nil
}
func (r *SchedulableEntityRepo) Get(ctx context.Context, ID models.SchedulableEntityKey) (models.SchedulableEntity, error) {
var schedulableEntity models.SchedulableEntity
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.SchedulableEntity{
SchedulableEntityKey: models.SchedulableEntityKey{
Project: ID.Project,
Domain: ID.Domain,
Name: ID.Name,
Version: ID.Version,
},
}).Take(&schedulableEntity)
timer.Stop()
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.SchedulableEntity{},
adminErrors.GetMissingEntityError("schedulable entity", &core.Identifier{
Project: ID.Project,
Domain: ID.Domain,
Name: ID.Name,
Version: ID.Version,
})
}
return models.SchedulableEntity{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return schedulableEntity, nil
}
// Helper function to activate and deactivate a schedule
func activateOrDeactivate(r *SchedulableEntityRepo, ID models.SchedulableEntityKey, activate bool) error {
timer := r.metrics.GetDuration.Start()
tx := r.db.Model(&models.SchedulableEntity{}).Where(&models.SchedulableEntity{
SchedulableEntityKey: models.SchedulableEntityKey{
Project: ID.Project,
Domain: ID.Domain,
Name: ID.Name,
Version: ID.Version,
},
}).Update("active", activate)
timer.Stop()
if tx.Error != nil {
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return adminErrors.GetMissingEntityError("schedulable entity", &core.Identifier{
Project: ID.Project,
Domain: ID.Domain,
Name: ID.Name,
Version: ID.Version,
})
}
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
// NewSchedulableEntityRepo Returns an instance of SchedulableEntityRepoInterface
func NewSchedulableEntityRepo(
db *gorm.DB, errorTransformer adminErrors.ErrorTransformer, scope promutils.Scope) interfaces.SchedulableEntityRepoInterface {
metrics := newMetrics(scope)
return &SchedulableEntityRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}