-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
default_context.go
113 lines (95 loc) · 2.91 KB
/
default_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
// 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"
"errors"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/logger"
)
// DefaultContext provides a basic job context
type DefaultContext struct {
// System context
sysContext context.Context
// Logger for job
logger logger.Interface
// Other required information
properties map[string]interface{}
// Track the job attached with the context
tracker job.Tracker
}
// NewDefaultContext is constructor of building DefaultContext
func NewDefaultContext(sysCtx context.Context) job.Context {
return &DefaultContext{
sysContext: sysCtx,
properties: make(map[string]interface{}),
}
}
// Build implements the same method in env.Context interface
// This func will build the job execution context before running
func (dc *DefaultContext) Build(t job.Tracker) (job.Context, error) {
if t == nil {
return nil, errors.New("nil job tracker")
}
jContext := &DefaultContext{
sysContext: dc.sysContext,
tracker: t,
properties: make(map[string]interface{}),
}
// Copy properties
if len(dc.properties) > 0 {
for k, v := range dc.properties {
jContext.properties[k] = v
}
}
// Set loggers for job
lg, err := createLoggers(t.Job().Info.JobID)
if err != nil {
return nil, err
}
jContext.logger = lg
return jContext, nil
}
// Get implements the same method in env.Context interface
func (dc *DefaultContext) Get(prop string) (interface{}, bool) {
v, ok := dc.properties[prop]
return v, ok
}
// SystemContext implements the same method in env.Context interface
func (dc *DefaultContext) SystemContext() context.Context {
return dc.sysContext
}
// Checkin is bridge func for reporting detailed status
func (dc *DefaultContext) Checkin(status string) error {
return dc.tracker.CheckIn(status)
}
// OPCommand return the control operational command like stop if have
func (dc *DefaultContext) OPCommand() (job.OPCommand, bool) {
latest, err := dc.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 (dc *DefaultContext) GetLogger() logger.Interface {
return dc.logger
}
// Tracker returns the tracker tracking the job attached with the context
func (dc *DefaultContext) Tracker() job.Tracker {
return dc.tracker
}