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

Fix bug with error state writing #6

Merged
merged 1 commit into from
Jul 31, 2024
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
14 changes: 10 additions & 4 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ type Return[JC any] struct {
PriorState string
Job Job[JC]
KickRequests []KickRequest[JC]
Error error
}

func NewProcessor[AC any, OC any, JC any](ac AC, states []State[AC, OC, JC], serializer Serializer[OC, JC], statusListener StatusListener) (*Processor[AC, OC, JC], error) {
Expand Down Expand Up @@ -382,13 +381,20 @@ func (s *StateExec[AC, OC, JC]) Run() {
s.state.RateLimit.Wait(s.ctx)
slog.Info("LimiterAllowed", "worker", s.i, "state", s.state.TriggerState, "job", j.Id)
}
priorState := j.State
// Execute the job
rtn := Return[JC]{
PriorState: j.State,
PriorState: priorState,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test that catches this bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated test case does

}
slog.Info("Executing job", "job", j.Id, "state", s.state.TriggerState)
j.C, j.State, rtn.KickRequests, rtn.Error = s.state.Exec(s.ctx, s.ac, s.oc, j.C)
slog.Info("Execution complete", "job", j.Id, "state", s.state.TriggerState, "newState", j.State, "error", rtn.Error, "kickRequests", len(rtn.KickRequests))
var err error
j.C, j.State, rtn.KickRequests, err = s.state.Exec(s.ctx, s.ac, s.oc, j.C)
if err != nil {
j.StateErrors[priorState] = append(j.StateErrors[priorState], err.Error())
slog.Info("Execution complete", "job", j.Id, "state", s.state.TriggerState, "newState", j.State, "error", err, "kickRequests", len(rtn.KickRequests))
} else {
slog.Info("Execution complete", "job", j.Id, "state", s.state.TriggerState, "newState", j.State, "kickRequests", len(rtn.KickRequests))
}

rtn.Job = j
slog.Info("Returning job", "job", j.Id, "newState", j.State)
Expand Down
10 changes: 8 additions & 2 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jorb

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -716,10 +717,14 @@ func TestProcessor_Serialization(t *testing.T) {
State[MyAppContext, MyOverallContext, MyJobContext]{
TriggerState: TRIGGER_STATE_NEW,
Exec: func(ctx context.Context, ac MyAppContext, oc MyOverallContext, jc MyJobContext) (MyJobContext, string, []KickRequest[MyJobContext], error) {
if jc.Count == 1 {
return jc, STATE_DONE, nil, errors.New("errored again")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why errors again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to confirm that if multiple errors are detected, they are recorded separately and don't clobber each other. You can see this assertion lower in the test case

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it

}

//log.Println("Processing New")
jc.Count += 1
time.Sleep(time.Second)
return jc, STATE_DONE, nil, nil
return jc, TRIGGER_STATE_NEW, nil, errors.New("errored")
},
Terminal: false,
Concurrency: 10,
Expand All @@ -738,10 +743,11 @@ func TestProcessor_Serialization(t *testing.T) {
err = p.Exec(context.Background(), r)
delta := time.Since(start)
require.NoError(t, err)
assert.Less(t, delta, time.Second*2, "Should take less than 2 seconds when run in parallel")
assert.Less(t, delta, time.Second*4, "Should take less than 4 seconds when run in parallel")

for _, j := range r.Jobs {
assert.Equal(t, 1, j.C.Count, "Job Count should be 1")
assert.Equal(t, map[string][]string{TRIGGER_STATE_NEW: {"errored", "errored again"}}, j.StateErrors)
}

// Now reload the job
Expand Down
Loading