forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
214 lines (186 loc) · 5.32 KB
/
context.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package impl
import (
"context"
"fmt"
"math"
"time"
"errors"
comcfg "github.com/goharbor/harbor/src/common/config"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/jobservice/config"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/goharbor/harbor/src/jobservice/logger/sweeper"
)
const (
maxRetryTimes = 5
)
// Context ...
type Context struct {
// System context
sysContext context.Context
// Logger for job
logger logger.Interface
// other required information
properties map[string]interface{}
// admin server client
cfgMgr comcfg.CfgManager
// job life cycle tracker
tracker job.Tracker
}
// NewContext ...
func NewContext(sysCtx context.Context, cfgMgr *comcfg.CfgManager) *Context {
return &Context{
sysContext: sysCtx,
cfgMgr: *cfgMgr,
properties: make(map[string]interface{}),
}
}
// Init ...
func (c *Context) Init() error {
var (
counter = 0
err error
)
for counter == 0 || err != nil {
counter++
err = c.cfgMgr.Load()
if err != nil {
logger.Errorf("Job context initialization error: %s\n", err.Error())
if counter < maxRetryTimes {
backoff := (int)(math.Pow(2, (float64)(counter))) + 2*counter + 5
logger.Infof("Retry in %d seconds", backoff)
time.Sleep(time.Duration(backoff) * time.Second)
} else {
return fmt.Errorf("job context initialization error: %s (%d times tried)", err.Error(), counter)
}
}
}
db := c.cfgMgr.GetDatabaseCfg()
err = dao.InitDatabase(db)
if err != nil {
return err
}
// Initialize DB finished
initDBCompleted()
return nil
}
// Build implements the same method in env.JobContext interface
// This func will build the job execution context before running
func (c *Context) Build(tracker job.Tracker) (job.Context, error) {
if tracker == nil || tracker.Job() == nil {
return nil, errors.New("nil job tracker")
}
jContext := &Context{
sysContext: c.sysContext,
cfgMgr: c.cfgMgr,
properties: make(map[string]interface{}),
tracker: tracker,
}
// Copy properties
if len(c.properties) > 0 {
for k, v := range c.properties {
jContext.properties[k] = v
}
}
// Refresh config properties
err := c.cfgMgr.Load()
if err != nil {
return nil, err
}
props := c.cfgMgr.GetAll()
for k, v := range props {
jContext.properties[k] = v
}
// Set loggers for job
lg, err := createLoggers(tracker.Job().Info.JobID)
if err != nil {
return nil, err
}
jContext.logger = lg
return jContext, nil
}
// Get implements the same method in env.JobContext interface
func (c *Context) Get(prop string) (interface{}, bool) {
v, ok := c.properties[prop]
return v, ok
}
// SystemContext implements the same method in env.JobContext interface
func (c *Context) SystemContext() context.Context {
return c.sysContext
}
// Checkin is bridge func for reporting detailed status
func (c *Context) Checkin(status string) error {
return c.tracker.CheckIn(status)
}
// OPCommand return the control operational command like stop/cancel if have
func (c *Context) OPCommand() (job.OPCommand, bool) {
latest, err := c.tracker.Status()
if err != nil {
return job.NilCommand, false
}
if job.StoppedStatus == latest {
return job.StopCommand, true
}
return job.NilCommand, false
}
// GetLogger returns the logger
func (c *Context) GetLogger() logger.Interface {
return c.logger
}
// Tracker returns the job tracker attached with the context
func (c *Context) Tracker() job.Tracker {
return c.tracker
}
// create loggers based on the configurations.
func createLoggers(jobID string) (logger.Interface, error) {
// Init job loggers here
lOptions := make([]logger.Option, 0)
for _, lc := range config.DefaultConfig.JobLoggerConfigs {
// For running job, the depth should be 5
if lc.Name == logger.NameFile || lc.Name == logger.NameStdOutput || lc.Name == logger.NameDB {
if lc.Settings == nil {
lc.Settings = map[string]interface{}{}
}
lc.Settings["depth"] = 5
}
if lc.Name == logger.NameFile || lc.Name == logger.NameDB {
// Need extra param
fSettings := map[string]interface{}{}
for k, v := range lc.Settings {
// Copy settings
fSettings[k] = v
}
if lc.Name == logger.NameFile {
// Append file name param
fSettings["filename"] = fmt.Sprintf("%s.log", jobID)
lOptions = append(lOptions, logger.BackendOption(lc.Name, lc.Level, fSettings))
} else { // DB Logger
// Append DB key
fSettings["key"] = jobID
lOptions = append(lOptions, logger.BackendOption(lc.Name, lc.Level, fSettings))
}
} else {
lOptions = append(lOptions, logger.BackendOption(lc.Name, lc.Level, lc.Settings))
}
}
// Get logger for the job
return logger.GetLogger(lOptions...)
}
func initDBCompleted() error {
sweeper.PrepareDBSweep()
return nil
}