Skip to content

Commit 779ecb3

Browse files
author
Ryan Field
committed
Export the job execution method
1 parent 1d92df9 commit 779ecb3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

goflow.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goflow
33

44
import (
55
"errors"
6+
"fmt"
67
"net/http"
78

89
"github.com/google/uuid"
@@ -128,11 +129,15 @@ func (g *Goflow) toggle(jobName string) (bool, error) {
128129
return true, nil
129130
}
130131

131-
// execute tells the engine to run a given job in a new goroutine.
132-
func (g *Goflow) execute(job string) uuid.UUID {
132+
// Execute tells the engine to run a given job in a new goroutine.
133+
func (g *Goflow) Execute(job string) (*uuid.UUID, error) {
133134

134-
// create job
135-
j := g.Jobs[job]()
135+
jobFunc, ok := g.Jobs[job]
136+
if !ok {
137+
return nil, fmt.Errorf("job %s does not exist", job)
138+
}
139+
140+
j := jobFunc()
136141

137142
// create and persist a new execution
138143
e := j.newExecution()
@@ -142,7 +147,7 @@ func (g *Goflow) execute(job string) uuid.UUID {
142147
// start running the job
143148
go j.run(g.Store, e)
144149

145-
return e.ID
150+
return &e.ID, nil
146151
}
147152

148153
// Run runs the webserver.

goflow_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,12 @@ func TestInvalidJobName(t *testing.T) {
223223
t.Errorf("Expected error adding a job with an invalid name")
224224
}
225225
}
226+
227+
func TestExecutionOfNonexistentJob(t *testing.T) {
228+
g := New(Options{})
229+
_, err := g.Execute("job")
230+
231+
if err == nil {
232+
t.Errorf("Expected error executing a nonexistent job")
233+
}
234+
}

routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (g *Goflow) handleSubmittedJobs(w http.ResponseWriter, r *http.Request) {
8282
msg.Job = name
8383

8484
if ok {
85-
g.execute(name)
85+
g.Execute(name)
8686
msg.Success = true
8787
msg.Submitted = time.Now().UTC().Format(time.RFC3339Nano)
8888
out, _ := json.Marshal(msg)

0 commit comments

Comments
 (0)