2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2021-09-23 14:58:12 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
|
|
|
//
|
|
|
|
// 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 agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-23 14:35:24 +00:00
|
|
|
"errors"
|
2023-07-20 18:39:20 +00:00
|
|
|
"fmt"
|
2021-09-23 14:58:12 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-09-23 14:58:12 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2024-12-22 09:44:34 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/pipeline"
|
|
|
|
backend "go.woodpecker-ci.org/woodpecker/v3/pipeline/backend/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/pipeline/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/shared/constant"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/shared/utils"
|
2021-09-23 14:58:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Runner struct {
|
|
|
|
client rpc.Peer
|
|
|
|
filter rpc.Filter
|
|
|
|
hostname string
|
|
|
|
counter *State
|
2023-12-14 18:20:47 +00:00
|
|
|
backend *backend.Backend
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
func NewRunner(workEngine rpc.Peer, f rpc.Filter, h string, state *State, backend *backend.Backend) Runner {
|
2021-09-23 14:58:12 +00:00
|
|
|
return Runner{
|
|
|
|
client: workEngine,
|
|
|
|
filter: f,
|
|
|
|
hostname: h,
|
|
|
|
counter: state,
|
2023-12-14 18:20:47 +00:00
|
|
|
backend: backend,
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 23:06:20 +00:00
|
|
|
func (r *Runner) Run(runnerCtx, shutdownCtx context.Context) error { //nolint:contextcheck
|
2021-11-13 19:18:06 +00:00
|
|
|
log.Debug().Msg("request next execution")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
meta, _ := metadata.FromOutgoingContext(runnerCtx)
|
2024-05-24 20:35:04 +00:00
|
|
|
ctxMeta := metadata.NewOutgoingContext(context.Background(), meta)
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
// get the next workflow from the queue
|
2024-07-01 09:20:55 +00:00
|
|
|
workflow, err := r.client.Next(runnerCtx, r.filter)
|
2021-09-23 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-01 09:20:55 +00:00
|
|
|
if workflow == nil {
|
2021-09-23 14:58:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Hour
|
2024-07-01 09:20:55 +00:00
|
|
|
if minutes := workflow.Timeout; minutes != 0 {
|
2021-09-23 14:58:12 +00:00
|
|
|
timeout = time.Duration(minutes) * time.Minute
|
|
|
|
}
|
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
repoName := extractRepositoryName(workflow.Config) // hack
|
|
|
|
pipelineNumber := extractPipelineNumber(workflow.Config) // hack
|
2022-06-15 16:11:20 +00:00
|
|
|
|
2021-09-23 14:58:12 +00:00
|
|
|
r.counter.Add(
|
2024-07-01 09:20:55 +00:00
|
|
|
workflow.ID,
|
2021-09-23 14:58:12 +00:00
|
|
|
timeout,
|
2022-06-15 16:11:20 +00:00
|
|
|
repoName,
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineNumber,
|
2021-09-23 14:58:12 +00:00
|
|
|
)
|
2024-07-01 09:20:55 +00:00
|
|
|
defer r.counter.Done(workflow.ID)
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
logger := log.With().
|
2022-06-15 16:11:20 +00:00
|
|
|
Str("repo", repoName).
|
2022-10-18 01:24:12 +00:00
|
|
|
Str("pipeline", pipelineNumber).
|
2024-07-01 09:20:55 +00:00
|
|
|
Str("workflow_id", workflow.ID).
|
2021-09-23 14:58:12 +00:00
|
|
|
Logger()
|
|
|
|
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("received execution")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2024-05-24 20:35:04 +00:00
|
|
|
workflowCtx, cancel := context.WithTimeout(ctxMeta, timeout)
|
2021-09-23 14:58:12 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
// Add sigterm support for internal context.
|
|
|
|
// Required when the pipeline is terminated by external signals
|
|
|
|
// like kubernetes.
|
2022-11-23 14:35:24 +00:00
|
|
|
workflowCtx = utils.WithContextSigtermCallback(workflowCtx, func() {
|
2022-05-11 11:40:44 +00:00
|
|
|
logger.Error().Msg("Received sigterm termination signal")
|
|
|
|
})
|
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
canceled := false
|
2021-09-23 14:58:12 +00:00
|
|
|
go func() {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("listen for cancel signal")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
if err := r.client.Wait(workflowCtx, workflow.ID); err != nil {
|
|
|
|
canceled = true
|
2024-05-24 20:35:04 +00:00
|
|
|
logger.Warn().Err(err).Msg("cancel signal received")
|
2021-09-23 14:58:12 +00:00
|
|
|
cancel()
|
|
|
|
} else {
|
2024-07-01 09:20:55 +00:00
|
|
|
logger.Debug().Msg("done listening for cancel signal")
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2022-11-23 14:35:24 +00:00
|
|
|
case <-workflowCtx.Done():
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("pipeline done")
|
2021-09-23 14:58:12 +00:00
|
|
|
return
|
2024-08-30 09:44:56 +00:00
|
|
|
|
2024-09-20 07:40:48 +00:00
|
|
|
case <-time.After(constant.TaskTimeout / 3):
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("pipeline lease renewed")
|
2024-07-01 09:20:55 +00:00
|
|
|
if err := r.client.Extend(workflowCtx, workflow.ID); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
log.Error().Err(err).Msg("extending pipeline deadline failed")
|
|
|
|
}
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
state := rpc.WorkflowState{}
|
2021-09-23 14:58:12 +00:00
|
|
|
state.Started = time.Now().Unix()
|
|
|
|
|
2024-07-01 09:20:55 +00:00
|
|
|
err = r.client.Init(runnerCtx, workflow.ID, state)
|
2021-09-23 14:58:12 +00:00
|
|
|
if err != nil {
|
2024-07-01 09:20:55 +00:00
|
|
|
logger.Error().Err(err).Msg("workflow initialization failed")
|
|
|
|
// TODO: should we return here?
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var uploads sync.WaitGroup
|
2024-01-11 21:15:15 +00:00
|
|
|
//nolint:contextcheck
|
2024-07-01 09:20:55 +00:00
|
|
|
err = pipeline.New(workflow.Config,
|
2022-11-23 14:35:24 +00:00
|
|
|
pipeline.WithContext(workflowCtx),
|
2024-07-01 09:20:55 +00:00
|
|
|
pipeline.WithTaskUUID(fmt.Sprint(workflow.ID)),
|
|
|
|
pipeline.WithLogger(r.createLogger(logger, &uploads, workflow)),
|
2024-08-30 09:44:56 +00:00
|
|
|
pipeline.WithTracer(r.createTracer(ctxMeta, &uploads, logger, workflow)),
|
2023-12-14 18:20:47 +00:00
|
|
|
pipeline.WithBackend(*r.backend),
|
2022-06-15 16:11:20 +00:00
|
|
|
pipeline.WithDescription(map[string]string{
|
2024-07-01 09:20:55 +00:00
|
|
|
"workflow_id": workflow.ID,
|
|
|
|
"repo": repoName,
|
|
|
|
"pipeline_number": pipelineNumber,
|
2022-06-15 16:11:20 +00:00
|
|
|
}),
|
2022-11-23 14:35:24 +00:00
|
|
|
).Run(runnerCtx)
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
state.Finished = time.Now().Unix()
|
2024-07-01 09:20:55 +00:00
|
|
|
|
|
|
|
if errors.Is(err, pipeline.ErrCancel) {
|
|
|
|
canceled = true
|
|
|
|
} else if canceled {
|
|
|
|
err = errors.Join(err, pipeline.ErrCancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
state.Error = err.Error()
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug().
|
|
|
|
Str("error", state.Error).
|
2024-07-01 09:20:55 +00:00
|
|
|
Bool("canceled", canceled).
|
|
|
|
Msg("workflow finished")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2024-08-30 09:44:56 +00:00
|
|
|
logger.Debug().Msg("uploading logs and traces / states ...")
|
2021-09-23 14:58:12 +00:00
|
|
|
uploads.Wait()
|
2024-08-30 09:44:56 +00:00
|
|
|
logger.Debug().Msg("uploaded logs and traces / states")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
logger.Debug().
|
|
|
|
Str("error", state.Error).
|
2024-07-01 09:20:55 +00:00
|
|
|
Msg("updating workflow status")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2024-07-13 23:06:20 +00:00
|
|
|
doneCtx := runnerCtx
|
|
|
|
if doneCtx.Err() != nil {
|
|
|
|
doneCtx = shutdownCtx
|
|
|
|
}
|
|
|
|
if err := r.client.Done(doneCtx, workflow.ID, state); err != nil {
|
2024-07-01 09:20:55 +00:00
|
|
|
logger.Error().Err(err).Msg("updating workflow status failed")
|
2021-09-23 14:58:12 +00:00
|
|
|
} else {
|
2024-07-01 09:20:55 +00:00
|
|
|
logger.Debug().Msg("updating workflow status complete")
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractRepositoryName(config *backend.Config) string {
|
2021-11-25 19:43:31 +00:00
|
|
|
return config.Stages[0].Steps[0].Environment["CI_REPO"]
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func extractPipelineNumber(config *backend.Config) string {
|
|
|
|
return config.Stages[0].Steps[0].Environment["CI_PIPELINE_NUMBER"]
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|