Skip to content

Commit

Permalink
format do 'simplify' and check via CI (#509)
Browse files Browse the repository at this point in the history
* `make format` simplify code now

* code format

* check simplified via linter
  • Loading branch information
6543 authored Nov 14, 2021
1 parent 2524c69 commit 86bb8f1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters-settings:
gofmt:
simplify: false
simplify: true

linters:
disable-all: true
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ vendor:
go mod vendor

format:
@gofmt -w ${GOFILES_NOVENDOR}
@gofmt -s -w ${GOFILES_NOVENDOR}

.PHONY: clean
clean:
Expand All @@ -36,7 +36,7 @@ clean:
.PHONY: lint
lint:
@echo "Running golangci-lint"
go run vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/main.go run
go run vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/main.go run --timeout 5m
@echo "Running zerolog linter"
go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/agent
go run vendor/github.com/rs/zerolog/cmd/lint/lint.go github.com/woodpecker-ci/woodpecker/cmd/cli
Expand Down
97 changes: 43 additions & 54 deletions agent/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,41 +93,34 @@ func (r *Runner) Run(ctx context.Context) error {
Str("id", work.ID).
Logger()

logger.Debug().
Msg("received execution")
logger.Debug().Msg("received execution")

ctx, cancel := context.WithTimeout(ctxmeta, timeout)
defer cancel()

cancelled := abool.New()
go func() {
logger.Debug().
Msg("listen for cancel signal")
logger.Debug().Msg("listen for cancel signal")

if werr := r.client.Wait(ctx, work.ID); werr != nil {
cancelled.SetTo(true)
logger.Warn().
Err(werr).
Msg("cancel signal received")
logger.Warn().Err(werr).Msg("cancel signal received")

cancel()
} else {
logger.Debug().
Msg("stop listening for cancel signal")
logger.Debug().Msg("stop listening for cancel signal")
}
}()

go func() {
for {
select {
case <-ctx.Done():
logger.Debug().
Msg("pipeline done")
logger.Debug().Msg("pipeline done")

return
case <-time.After(time.Minute):
logger.Debug().
Msg("pipeline lease renewed")
logger.Debug().Msg("pipeline lease renewed")

r.client.Extend(ctx, work.ID)
}
Expand All @@ -139,9 +132,7 @@ func (r *Runner) Run(ctx context.Context) error {

err = r.client.Init(ctxmeta, work.ID, state)
if err != nil {
logger.Error().
Err(err).
Msg("pipeline initialization failed")
logger.Error().Err(err).Msg("pipeline initialization failed")
}

var uploads sync.WaitGroup
Expand Down Expand Up @@ -173,30 +164,29 @@ func (r *Runner) Run(ctx context.Context) error {

loglogger.Debug().Msg("log stream copied")

file := &rpc.File{}
file.Mime = "application/json+logs"
file.Proc = proc.Alias
file.Name = "logs.json"
file.Data, _ = json.Marshal(logstream.Lines())
file.Size = len(file.Data)
file.Time = time.Now().Unix()
data, err := json.Marshal(logstream.Lines())
if err != nil {
loglogger.Err(err).Msg("could not marshal logstream")
}

loglogger.Debug().
Msg("log stream uploading")
file := &rpc.File{
Mime: "application/json+logs",
Proc: proc.Alias,
Name: "logs.json",
Data: data,
Size: len(data),
Time: time.Now().Unix(),
}

loglogger.Debug().Msg("log stream uploading")
if serr := r.client.Upload(ctxmeta, work.ID, file); serr != nil {
loglogger.Error().
Err(serr).
Msg("log stream upload error")
loglogger.Error().Err(serr).Msg("log stream upload error")
} else {
loglogger.Debug().Msg("log stream upload complete")
}

loglogger.Debug().
Msg("log stream upload complete")

defer func() {
loglogger.Debug().
Msg("log stream closed")

loglogger.Debug().Msg("log stream closed")
uploads.Done()
}()

Expand All @@ -206,15 +196,20 @@ func (r *Runner) Run(ctx context.Context) error {
}
// TODO should be configurable
limitedPart = io.LimitReader(part, maxFileUpload)
file = &rpc.File{}
file.Mime = part.Header().Get("Content-Type")
file.Proc = proc.Alias
file.Name = part.FileName()
file.Data, _ = ioutil.ReadAll(limitedPart)
file.Size = len(file.Data)
file.Time = time.Now().Unix()
file.Meta = map[string]string{}
data, err = ioutil.ReadAll(limitedPart)
if err != nil {
loglogger.Err(err).Msg("could not read limited part")
}

file = &rpc.File{
Mime: part.Header().Get("Content-Type"),
Proc: proc.Alias,
Name: part.FileName(),
Data: data,
Size: len(data),
Time: time.Now().Unix(),
Meta: make(map[string]string),
}
for key, value := range part.Header() {
file.Meta[key] = value[0]
}
Expand Down Expand Up @@ -255,17 +250,15 @@ func (r *Runner) Run(ctx context.Context) error {
Finished: time.Now().Unix(),
}
defer func() {
proclogger.Debug().
Msg("update step status")
proclogger.Debug().Msg("update step status")

if uerr := r.client.Update(ctxmeta, work.ID, procState); uerr != nil {
proclogger.Debug().
Err(uerr).
Msg("update step status error")
}

proclogger.Debug().
Msg("update step status complete")
proclogger.Debug().Msg("update step status complete")
}()
if state.Process.Exited {
return nil
Expand Down Expand Up @@ -325,13 +318,11 @@ func (r *Runner) Run(ctx context.Context) error {
Int("exit_code", state.ExitCode).
Msg("pipeline complete")

logger.Debug().
Msg("uploading logs")
logger.Debug().Msg("uploading logs")

uploads.Wait()

logger.Debug().
Msg("uploading logs complete")
logger.Debug().Msg("uploading logs complete")

logger.Debug().
Str("error", state.Error).
Expand All @@ -340,11 +331,9 @@ func (r *Runner) Run(ctx context.Context) error {

err = r.client.Done(ctxmeta, work.ID, state)
if err != nil {
logger.Error().Err(err).
Msg("updating pipeline status failed")
logger.Error().Err(err).Msg("updating pipeline status failed")
} else {
logger.Debug().
Msg("updating pipeline status complete")
logger.Debug().Msg("updating pipeline status complete")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions server/shared/procBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ func TestPipelineName(t *testing.T) {
Regs: []*model.Registry{},
Link: "",
Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: ".woodpecker/lint.yml", Data: []byte(`
{Name: ".woodpecker/lint.yml", Data: []byte(`
pipeline:
build:
image: scratch
`)},
&remote.FileMeta{Name: ".woodpecker/.test.yml", Data: []byte(`
{Name: ".woodpecker/.test.yml", Data: []byte(`
pipeline:
build:
image: scratch
Expand Down

0 comments on commit 86bb8f1

Please sign in to comment.