Skip to content

Commit

Permalink
Merge pull request #3 from gaffo/add_job_with_state
Browse files Browse the repository at this point in the history
Run::AddJobWithState
  • Loading branch information
gaffo authored Jul 18, 2024
2 parents 4344df3 + 141fb89 commit 1e4f04a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (r *Run[OC, JC]) Init() {
}
}

// Add a job to the pool, this shouldn't be called once it's running
func (r *Run[OC, JC]) AddJob(jc JC) {
func (r *Run[OC, JC]) AddJobWithState(jc JC, state string) {
r.m.Lock()
defer r.m.Unlock()

Expand All @@ -79,7 +78,7 @@ func (r *Run[OC, JC]) AddJob(jc JC) {
j := Job[JC]{
Id: id,
C: jc,
State: TRIGGER_STATE_NEW,
State: state,
StateErrors: map[string][]string{},
}
j.UpdateLastEvent()
Expand All @@ -90,6 +89,11 @@ func (r *Run[OC, JC]) AddJob(jc JC) {
r.Jobs[id] = j
}

// Add a job to the pool, this shouldn't be called once it's running
func (r *Run[OC, JC]) AddJob(jc JC) {
r.AddJobWithState(jc, TRIGGER_STATE_NEW)
}

func (r *Run[OC, JC]) NextJobForState(state string) (Job[JC], bool) {
r.m.Lock()
defer r.m.Unlock()
Expand Down
15 changes: 15 additions & 0 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,18 @@ func TestRun_JobsInFlight2(t *testing.T) {
r.Return(j)
require.False(t, r.JobsInFlight())
}

func Test_AddJobWithState(t *testing.T) {
t.Parallel()
r := NewRun[MyOverallContext, MyJobContext]("job", MyOverallContext{})
r.AddJobWithState(MyJobContext{Count: 0}, "other_state")
assert.Equal(t, map[string]StatusCount{
"other_state": {
State: "other_state",
Count: 1,
Executing: 0,
},
}, r.StatusCounts())
assert.Equal(t, 1, len(r.Jobs))
assert.Equal(t, "other_state", r.Jobs["0"].State)
}

0 comments on commit 1e4f04a

Please sign in to comment.