This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
jobs.go
173 lines (145 loc) · 4.53 KB
/
jobs.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
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package management
import (
"context"
jobsAPI "github.com/percona/pmm/api/managementpb/jobs"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/reform.v1"
"github.com/percona/pmm-managed/models"
)
// JobsAPIService provides methods for Jobs starting and management.
type JobsAPIService struct {
l *logrus.Entry
db *reform.DB
jobsService jobsService
}
// NewJobsAPIServer creates new jobs service.
func NewJobsAPIServer(db *reform.DB, service jobsService) *JobsAPIService {
return &JobsAPIService{
l: logrus.WithField("component", "management/jobs"),
db: db,
jobsService: service,
}
}
// GetJob returns job result.
func (s *JobsAPIService) GetJob(_ context.Context, req *jobsAPI.GetJobRequest) (*jobsAPI.GetJobResponse, error) {
result, err := models.FindJobResultByID(s.db.Querier, req.JobId)
if err != nil {
return nil, err
}
resp := &jobsAPI.GetJobResponse{
JobId: result.ID,
PmmAgentId: result.PMMAgentID,
Done: result.Done,
}
if !result.Done {
return resp, nil
}
if result.Error != "" {
resp.Result = &jobsAPI.GetJobResponse_Error_{
Error: &jobsAPI.GetJobResponse_Error{
Message: result.Error,
},
}
return resp, nil
}
switch result.Type {
case models.Echo:
resp.Result = &jobsAPI.GetJobResponse_Echo_{
Echo: &jobsAPI.GetJobResponse_Echo{
Message: result.Result.Echo.Message,
},
}
default:
return nil, errors.Errorf("Unexpected job type: %s", result.Type)
}
return resp, nil
}
// StartEchoJob starts echo job. Its purpose is testing.
func (s *JobsAPIService) StartEchoJob(_ context.Context, req *jobsAPI.StartEchoJobRequest) (*jobsAPI.StartEchoJobResponse, error) {
res, err := s.prepareAgentJob(req.PmmAgentId, models.Echo)
if err != nil {
return nil, err
}
err = s.jobsService.StartEchoJob(res.ID, res.PMMAgentID, req.Timeout.AsDuration(), req.Message, req.Delay.AsDuration())
if err != nil {
s.saveJobError(res.ID, err.Error())
return nil, err
}
return &jobsAPI.StartEchoJobResponse{
PmmAgentId: req.PmmAgentId,
JobId: res.ID,
}, nil
}
// CancelJob terminates job.
func (s *JobsAPIService) CancelJob(_ context.Context, req *jobsAPI.CancelJobRequest) (*jobsAPI.CancelJobResponse, error) {
if err := s.jobsService.StopJob(req.JobId); err != nil {
return nil, err
}
return &jobsAPI.CancelJobResponse{}, nil
}
func (s *JobsAPIService) saveJobError(resultID string, message string) {
if e := s.db.InTransaction(func(t *reform.TX) error {
res, err := models.FindJobResultByID(t.Querier, resultID)
if err != nil {
return err
}
res.Error = message
res.Done = true
return t.Update(res)
}); e != nil {
s.l.Errorf("Failed to save job result: %+v", e)
}
}
func (s *JobsAPIService) prepareAgentJob(pmmAgentID string, jobType models.JobType) (*models.JobResult, error) {
var res *models.JobResult
e := s.db.InTransaction(func(tx *reform.TX) error {
_, err := models.FindAgentByID(tx.Querier, pmmAgentID)
if err != nil {
return err
}
res, err = models.CreateJobResult(tx.Querier, pmmAgentID, jobType, nil)
return err
})
if e != nil {
return nil, e
}
return res, nil
}
func (s *JobsAPIService) prepareServiceJob(serviceID, pmmAgentID, database string, jobType models.JobType) (*models.JobResult, string, error) {
var res *models.JobResult
var dsn string
e := s.db.InTransaction(func(tx *reform.TX) error {
agents, err := models.FindPMMAgentsForService(tx.Querier, serviceID)
if err != nil {
return err
}
if pmmAgentID, err = models.FindPmmAgentIDToRunActionOrJob(pmmAgentID, agents); err != nil {
return err
}
if dsn, _, err = models.FindDSNByServiceIDandPMMAgentID(tx.Querier, serviceID, pmmAgentID, database); err != nil {
return err
}
res, err = models.CreateJobResult(tx.Querier, pmmAgentID, jobType, nil)
return err
})
if e != nil {
return nil, "", e
}
return res, dsn, nil
}