2019-02-21 15:04:33 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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 scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-06 13:19:29 +00:00
|
|
|
"encoding/json"
|
2019-02-21 15:04:33 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
slog "agola.io/agola/internal/log"
|
|
|
|
"agola.io/agola/internal/services/common"
|
|
|
|
"agola.io/agola/internal/services/config"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
rsapitypes "agola.io/agola/services/runservice/api/types"
|
|
|
|
rsclient "agola.io/agola/services/runservice/client"
|
2019-02-21 15:04:33 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 15:04:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
|
|
|
|
var logger = slog.New(level)
|
|
|
|
var log = logger.Sugar()
|
|
|
|
|
|
|
|
func (s *Scheduler) scheduleLoop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
if err := s.schedule(ctx); err != nil {
|
|
|
|
log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-07-25 13:53:26 +00:00
|
|
|
|
|
|
|
sleepCh := time.NewTimer(1 * time.Second).C
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sleepCh:
|
|
|
|
}
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scheduler) schedule(ctx context.Context) error {
|
|
|
|
// create a list of project and users with queued runs
|
|
|
|
groups := map[string]struct{}{}
|
|
|
|
|
|
|
|
var lastRunID string
|
|
|
|
for {
|
2019-05-06 13:19:29 +00:00
|
|
|
queuedRunsResponse, _, err := s.runserviceClient.GetQueuedRuns(ctx, lastRunID, 0, nil)
|
2019-02-21 15:04:33 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to get queued runs: %w", err)
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, run := range queuedRunsResponse.Runs {
|
|
|
|
groups[run.Group] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(queuedRunsResponse.Runs) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:19:29 +00:00
|
|
|
lastRunID = queuedRunsResponse.Runs[len(queuedRunsResponse.Runs)-1].ID
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:15:48 +00:00
|
|
|
for groupID := range groups {
|
2019-02-21 15:04:33 +00:00
|
|
|
if err := s.scheduleRun(ctx, groupID); err != nil {
|
|
|
|
log.Errorf("scheduler err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scheduler) scheduleRun(ctx context.Context, groupID string) error {
|
|
|
|
// get first queued run
|
|
|
|
queuedRunsResponse, _, err := s.runserviceClient.GetGroupFirstQueuedRuns(ctx, groupID, nil)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to get the first project queued run: %w", err)
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
if len(queuedRunsResponse.Runs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
run := queuedRunsResponse.Runs[0]
|
|
|
|
|
2019-04-29 08:14:10 +00:00
|
|
|
changegroup := util.EncodeSha256Hex(fmt.Sprintf("changegroup-%s", groupID))
|
|
|
|
runningRunsResponse, _, err := s.runserviceClient.GetGroupRunningRuns(ctx, groupID, 1, []string{changegroup})
|
2019-02-21 15:04:33 +00:00
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to get running runs: %w", err)
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
if len(runningRunsResponse.Runs) == 0 {
|
|
|
|
log.Infof("starting run %s", run.ID)
|
2019-06-14 09:28:00 +00:00
|
|
|
log.Debugf("changegroups: %s", runningRunsResponse.ChangeGroupsUpdateToken)
|
2019-02-21 15:04:33 +00:00
|
|
|
if _, err := s.runserviceClient.StartRun(ctx, run.ID, runningRunsResponse.ChangeGroupsUpdateToken); err != nil {
|
|
|
|
log.Errorf("failed to start run %s: %v", run.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:19:29 +00:00
|
|
|
func (s *Scheduler) approveLoop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
if err := s.approve(ctx); err != nil {
|
|
|
|
log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-07-25 13:53:26 +00:00
|
|
|
|
|
|
|
sleepCh := time.NewTimer(1 * time.Second).C
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sleepCh:
|
|
|
|
}
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scheduler) approve(ctx context.Context) error {
|
|
|
|
var lastRunID string
|
|
|
|
for {
|
|
|
|
runningRunsResponse, _, err := s.runserviceClient.GetRunningRuns(ctx, lastRunID, 0, nil)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to get running runs: %w", err)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(runningRunsResponse.Runs) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, run := range runningRunsResponse.Runs {
|
|
|
|
if err := s.approveRunTasks(ctx, run.ID); err != nil {
|
|
|
|
// just log error and continue with the other runs
|
|
|
|
log.Errorf("failed to approve run tasks for run %q: %+v", run.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRunID = runningRunsResponse.Runs[len(runningRunsResponse.Runs)-1].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scheduler) approveRunTasks(ctx context.Context, runID string) error {
|
|
|
|
// refetch run with a dedicated changegroup
|
|
|
|
changegroup := util.EncodeSha256Hex(fmt.Sprintf("approval-%s", runID))
|
|
|
|
runResp, _, err := s.runserviceClient.GetRun(ctx, runID, []string{changegroup})
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to get run %q: %w", runID, err)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
run := runResp.Run
|
|
|
|
|
|
|
|
tasksWaitingApproval := run.TasksWaitingApproval()
|
|
|
|
for _, rtID := range tasksWaitingApproval {
|
|
|
|
rt, ok := run.Tasks[rtID]
|
|
|
|
if !ok {
|
2022-02-21 11:19:55 +00:00
|
|
|
return errors.Errorf("run %q doesn't have task %q", run.ID, rtID)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
annotations := rt.Annotations
|
|
|
|
if annotations == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
approversAnnotation, ok := annotations[common.ApproversAnnotation]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var approvers []string
|
|
|
|
if err := json.Unmarshal([]byte(approversAnnotation), &approvers); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to unmarshal run task approvers annotation: %w", err)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
// TODO(sgotti) change when we introduce a config the set the minimum number of required approvers
|
|
|
|
if len(approvers) > 0 {
|
2019-07-31 13:39:07 +00:00
|
|
|
rsreq := &rsapitypes.RunTaskActionsRequest{
|
|
|
|
ActionType: rsapitypes.RunTaskActionTypeApprove,
|
2019-05-06 13:19:29 +00:00
|
|
|
ChangeGroupsUpdateToken: runResp.ChangeGroupsUpdateToken,
|
|
|
|
}
|
|
|
|
if _, err := s.runserviceClient.RunTaskActions(ctx, run.ID, rt.ID, rsreq); err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return errors.Errorf("failed to approve run: %w", err)
|
2019-05-06 13:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:04:33 +00:00
|
|
|
type Scheduler struct {
|
|
|
|
c *config.Scheduler
|
2019-07-31 13:39:07 +00:00
|
|
|
runserviceClient *rsclient.Client
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:01:46 +00:00
|
|
|
func NewScheduler(ctx context.Context, l *zap.Logger, c *config.Scheduler) (*Scheduler, error) {
|
|
|
|
if l != nil {
|
|
|
|
logger = l
|
|
|
|
}
|
2019-02-21 15:04:33 +00:00
|
|
|
if c.Debug {
|
|
|
|
level.SetLevel(zapcore.DebugLevel)
|
|
|
|
}
|
2020-01-15 11:01:46 +00:00
|
|
|
log = logger.Sugar()
|
2019-02-21 15:04:33 +00:00
|
|
|
|
|
|
|
return &Scheduler{
|
2019-07-02 13:56:14 +00:00
|
|
|
c: c,
|
2019-07-31 13:39:07 +00:00
|
|
|
runserviceClient: rsclient.NewClient(c.RunserviceURL),
|
2019-02-21 15:04:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scheduler) Run(ctx context.Context) error {
|
|
|
|
go s.scheduleLoop(ctx)
|
2019-05-06 13:19:29 +00:00
|
|
|
go s.approveLoop(ctx)
|
2019-02-21 15:04:33 +00:00
|
|
|
|
2019-07-02 13:56:14 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
log.Infof("scheduler exiting")
|
|
|
|
|
|
|
|
return nil
|
2019-02-21 15:04:33 +00:00
|
|
|
}
|