Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: standardize logging behavior for write operations #62

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .ci/mockery_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: Apache-2.0
*
* Author: Michael Adler <[email protected]>
*/
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,8 @@ jobs:
- run: apk add --no-cache py3-yaml git just bash go
- name: Disable git security features
run: git config --global safe.directory '*'
- uses: brokeyourbike/go-mockery-action@v0
with:
mockery-version: "2.36.0"
- run: just generate
- run: git diff --exit-code
19 changes: 19 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
with-expecter: True
inpackage: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "mock_{{.InterfaceName}}.go"
all: True

boilerplate-file: .ci/mockery_header.txt
# tags are currently broken, see vektra/mockery issue #691
tags: testing

packages:
github.com/siemens/wfx/persistence:
config:
tags: testing
recursive: True
interfaces:
Storage:
4 changes: 3 additions & 1 deletion api/northbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ func NewNorthboundAPI(storage persistence.Storage) *operations.WorkflowExecutorA
err2.Message = err.Error()
return northbound.NewPostWorkflowsBadRequest().WithPayload(&model.ErrorResponse{Errors: []*model.Error{&err2}})
case ftag.AlreadyExists:
return northbound.NewPostWorkflowsBadRequest().WithPayload(&model.ErrorResponse{Errors: []*model.Error{&WorkflowNotUnique}})
err2 := WorkflowNotUnique
err2.Message = fmt.Sprintf("Workflow with name '%s' already exists", params.Workflow.Name)
return northbound.NewPostWorkflowsBadRequest().WithPayload(&model.ErrorResponse{Errors: []*model.Error{&err2}})
default:
return northbound.NewPostWorkflowsDefault(http.StatusInternalServerError)
}
Expand Down
7 changes: 6 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
# SPDX-License-Identifier: Apache-2.0
#
# Author: Michael Adler <[email protected]>

#
# This file can be validated as follows:
# curl -X POST --data-binary @codecov.yml https://codecov.io/validate

ignore:
# exclude mocks generated by mockery
- "**/mock_*.go"

coverage:
status:
patch:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ require (
github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tsenart/go-tsz v0.0.0-20180814235614-0bd30b3df1c3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.mongodb.org/mongo-driver v1.11.6 // indirect
Expand Down
5 changes: 2 additions & 3 deletions internal/handler/job/definition/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import (
func Update(ctx context.Context, storage persistence.Storage, jobID string, definition map[string]any) (map[string]any, error) {
log := logging.LoggerFromCtx(ctx)
contextLogger := log.With().Str("id", jobID).Logger()
contextLogger.Debug().Msg("Updating job definition")

job, err := storage.GetJob(ctx, jobID, persistence.FetchParams{History: false})
if err != nil {
contextLogger.Error().Err(err).Msg("Failed to fetch job from database")
contextLogger.Err(err).Msg("Failed to get job from storage")
return nil, fault.Wrap(err)
}

Expand All @@ -36,7 +35,7 @@ func Update(ctx context.Context, storage persistence.Storage, jobID string, defi

result, err := storage.UpdateJob(ctx, job, persistence.JobUpdate{Status: job.Status, Definition: &job.Definition})
if err != nil {
contextLogger.Error().Err(err).Msg("Failed to update job")
contextLogger.Err(err).Msg("Failed to update job")
return nil, fault.Wrap(err)
}

Expand Down
8 changes: 6 additions & 2 deletions internal/handler/job/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (

func DeleteJob(ctx context.Context, storage persistence.Storage, jobID string) error {
log := logging.LoggerFromCtx(ctx)
log.Info().Str("id", jobID).Msg("Deleting job")
return fault.Wrap(storage.DeleteJob(ctx, jobID))
if err := storage.DeleteJob(ctx, jobID); err != nil {
log.Err(err).Str("id", jobID).Msg("Failed to delete job")
return fault.Wrap(err)
}
log.Info().Str("id", jobID).Msg("Deleted job")
return nil
}
29 changes: 9 additions & 20 deletions internal/handler/job/status/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ import (
)

func Update(ctx context.Context, storage persistence.Storage, jobID string, newStatus *model.JobStatus, actor model.EligibleEnum) (*model.JobStatus, error) {
log := logging.LoggerFromCtx(ctx)
contextLogger := log.With().
Str("id", jobID).
Str("actor", string(actor)).
Logger()
contextLogger := logging.LoggerFromCtx(ctx).With().Str("id", jobID).Str("actor", string(actor)).Logger()

job, err := storage.GetJob(ctx, jobID, persistence.FetchParams{History: false})
if err != nil {
return nil, fault.Wrap(err)
}

from := job.Status.State
contextLogger.Debug().Str("from", from).Msg("Updating job")

// update status
to := newStatus.State
Expand All @@ -57,38 +52,32 @@ func Update(ctx context.Context, storage persistence.Storage, jobID string, newS
}
if !isAllowed {
if !foundTransition {
contextLogger.Info().Msg("Transition does not exist")
contextLogger.Warn().Msg("Transition does not exist")
return nil, fault.Wrap(fmt.Errorf("transition from '%s' to '%s' does not exist", from, to), ftag.With(ftag.InvalidArgument))
}
contextLogger.Info().Msg("Transition exists but actor is not allowed to trigger it")
contextLogger.Warn().Msg("Transition exists but actor is not allowed to trigger it")
return nil, fault.Wrap(fmt.Errorf("transition from '%s' to '%s' is not allowed for actor '%s'", from, to, actor), ftag.With(ftag.InvalidArgument))
}

// transition is allowed, now apply wfx transitions
newTo := workflow.FollowImmediateTransitions(job.Workflow, to)
if newTo != to {
log.Debug().Str("to", to).Str("newTo", newTo).Msg("Resetting state since we moved the transition forward")
contextLogger.Debug().Str("to", to).Str("newTo", newTo).Msg("Resetting state since we moved the transition forward")
newStatus = &model.JobStatus{}
}
newStatus.State = newTo
// override any definitionHash provided by client
newStatus.DefinitionHash = job.Status.DefinitionHash

log.Debug().
Str("message", job.Status.Message).
Str("state", job.Status.State).
Msg("Updating job")

result, err := storage.UpdateJob(ctx, job, persistence.JobUpdate{Status: newStatus})
if err != nil {
log.Error().Err(err).Msg("Failed to persist job update")
contextLogger.Err(err).Msg("Failed to persist job update")
return nil, fault.Wrap(err)
}

if from != to {
contextLogger.Info().Msg("Updated job status")
} else {
contextLogger.Debug().Msg("Updated job status")
}
contextLogger.Info().
Str("from", from).
Str("to", newStatus.State).
Msg("Updated job status")
return result.Status, nil
}
7 changes: 4 additions & 3 deletions internal/handler/job/tags/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import (

func Add(ctx context.Context, storage persistence.Storage, jobID string, tags []string) ([]string, error) {
log := logging.LoggerFromCtx(ctx)
contextLogger := log.With().Str("id", jobID).Logger()
contextLogger.Debug().Strs("tags", tags).Msg("Adding tags")
contextLogger := log.With().Str("id", jobID).Strs("tags", tags).Logger()

job, err := storage.GetJob(ctx, jobID, persistence.FetchParams{History: false})
if err != nil {
contextLogger.Err(err).Msg("Failed to get job from storage")
return nil, fault.Wrap(err)
}

updatedJob, err := storage.UpdateJob(ctx, job, persistence.JobUpdate{AddTags: &tags})
if err != nil {
contextLogger.Err(err).Msg("Failed to add tags to job")
return nil, fault.Wrap(err)
}

contextLogger.Info().Msg("Updated job tags")
contextLogger.Info().Msg("Added job tags")
return updatedJob.Tags, nil
}
28 changes: 28 additions & 0 deletions internal/handler/job/tags/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package tags

import (
"context"
"errors"
"testing"

"github.com/siemens/wfx/generated/model"
Expand Down Expand Up @@ -38,6 +39,33 @@ func TestAdd(t *testing.T) {
assert.Equal(t, []string{"bar", "foo"}, actual)
}

func TestAdd_FaultyStorageGet(t *testing.T) {
db := persistence.NewMockStorage(t)
ctx := context.Background()
expectedErr := errors.New("mock error")
db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr)

tags, err := Add(ctx, db, "1", []string{"foo", "bar"})
assert.Nil(t, tags)
assert.NotNil(t, err)
}

func TestAdd_FaultyStorageUpdate(t *testing.T) {
db := persistence.NewMockStorage(t)
ctx := context.Background()

expectedErr := errors.New("mock error")
dummyJob := model.Job{ID: "1"}
tags := []string{"foo", "bar"}

db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil)
db.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{AddTags: &tags}).Return(nil, expectedErr)

tags, err := Add(ctx, db, "1", tags)
assert.Nil(t, tags)
assert.NotNil(t, err)
}

func newInMemoryDB(t *testing.T) persistence.Storage {
db := &entgo.SQLite{}
err := db.Initialize(context.Background(), "file:wfx?mode=memory&cache=shared&_fk=1")
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/job/tags/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ import (

func Delete(ctx context.Context, storage persistence.Storage, jobID string, tags []string) ([]string, error) {
log := logging.LoggerFromCtx(ctx)
contextLogger := log.With().Str("id", jobID).Logger()
contextLogger.Debug().Strs("tags", tags).Msg("Deleting tags")
contextLogger := log.With().Str("id", jobID).Strs("tags", tags).Logger()

job, err := storage.GetJob(ctx, jobID, persistence.FetchParams{History: false})
if err != nil {
contextLogger.Err(err).Msg("Failed to get job from storage")
return nil, fault.Wrap(err)
}

updatedJob, err := storage.UpdateJob(ctx, job, persistence.JobUpdate{DelTags: &tags})
if err != nil {
contextLogger.Err(err).Msg("Failed to delete tags to job")
return nil, fault.Wrap(err)
}
contextLogger.Debug().Msg("Deleted tags")
contextLogger.Info().Msg("Deleted job tags")
return updatedJob.Tags, nil
}
29 changes: 29 additions & 0 deletions internal/handler/job/tags/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ package tags

import (
"context"
"errors"
"testing"

"github.com/siemens/wfx/generated/model"
"github.com/siemens/wfx/persistence"
"github.com/siemens/wfx/workflow/dau"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -35,3 +37,30 @@ func TestDelete(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []string{"bar"}, tags)
}

func TestDelete_FaultyStorageGet(t *testing.T) {
db := persistence.NewMockStorage(t)
ctx := context.Background()
expectedErr := errors.New("mock error")
db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(nil, expectedErr)

tags, err := Delete(ctx, db, "1", []string{"foo", "bar"})
assert.Nil(t, tags)
assert.NotNil(t, err)
}

func TestDelete_FaultyStorageUpdate(t *testing.T) {
db := persistence.NewMockStorage(t)
ctx := context.Background()

expectedErr := errors.New("mock error")
dummyJob := model.Job{ID: "1"}
tags := []string{"foo", "bar"}

db.On("GetJob", ctx, "1", persistence.FetchParams{History: false}).Return(&dummyJob, nil)
db.On("UpdateJob", ctx, &dummyJob, persistence.JobUpdate{DelTags: &tags}).Return(nil, expectedErr)

tags, err := Delete(ctx, db, "1", tags)
assert.Nil(t, tags)
assert.NotNil(t, err)
}
1 change: 1 addition & 0 deletions internal/handler/workflow/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ func CreateWorkflow(ctx context.Context, storage persistence.Storage, wf *model.
log.Error().Err(err).Msg("Failed to create workflow")
return nil, fault.Wrap(err)
}
log.Info().Str("name", wf.Name).Msg("Created new workflow")
return wf, nil
}
8 changes: 6 additions & 2 deletions internal/handler/workflow/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (

func DeleteWorkflow(ctx context.Context, storage persistence.Storage, name string) error {
log := logging.LoggerFromCtx(ctx)
log.Debug().Str("name", name).Msg("Deleting workflow")
return fault.Wrap(storage.DeleteWorkflow(ctx, name))
if err := storage.DeleteWorkflow(ctx, name); err != nil {
log.Err(err).Str("name", name).Msg("Failed to delete workflow")
return fault.Wrap(err)
}
log.Info().Str("name", name).Msg("Deleted workflow")
return nil
}
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ _generate-ent:

// Code generated by ent, DO NOT EDIT." --feature sql/execquery,sql/versioned-migration ./schema

_generate-mockery:
mockery --all

# Generate code
generate: _generate-swagger _generate-ent
generate: _generate-swagger _generate-ent _generate-mockery

# Start PostgreSQL container
postgres-start VERSION="15":
Expand Down
19 changes: 19 additions & 0 deletions persistence/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
with-expecter: True
inpackage: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "mock_{{.InterfaceName}}.go"
all: True

boilerplate-file: .ci/mockery_header.txt
# tags are currently broken, see vektra/mockery issue #691
tags: testing

packages:
github.com/siemens/wfx/persistence:
config:
tags: testing
recursive: True
interfaces:
Storage:
Loading
Loading