Skip to content

Commit b774118

Browse files
v1.0.1; fix bug; update readme
fix issue where per job options were not overriding default options
1 parent 273e403 commit b774118

File tree

3 files changed

+101
-49
lines changed

3 files changed

+101
-49
lines changed

README.md

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,58 +63,52 @@ func main() {
6363
How do I know if a job timed out? How do I handle an error in my job?
6464

6565
```golang
66-
package main
67-
6866
import (
69-
"fmt"
70-
"time"
71-
7267
wpxt "github.com/oze4/workerpoolxt"
68+
// ...
7369
)
7470

75-
func main() {
76-
wp := wpxt.New(3, time.Duration(time.Second*10))
71+
wp := wpxt.New(3, time.Duration(time.Second*10))
7772

78-
// Uses default timeout
79-
wp.SubmitXT(wpxt.Job{
80-
Name: "Job 1 will pass",
81-
Task: func(o wpxt.Options) wpxt.Response {
82-
return wpxt.Response{Data: "yay"}
83-
},
84-
})
73+
// Uses default timeout
74+
wp.SubmitXT(wpxt.Job{
75+
Name: "Job 1 will pass",
76+
Task: func(o wpxt.Options) wpxt.Response {
77+
return wpxt.Response{Data: "yay"}
78+
},
79+
})
8580

86-
// Uses custom timeout
87-
// This job is configured to timeout on purpose
88-
wp.SubmitXT(wpxt.Job{
89-
Name: "Job 2 will timeout",
90-
Timeout: time.Duration(time.Millisecond * 1),
91-
Task: func(o wpxt.Options) wpxt.Response {
92-
// Simulate long running task
93-
time.Sleep(time.Second * 20)
94-
return wpxt.Response{Data: "timedout"}
95-
},
96-
})
81+
// Uses custom timeout
82+
// This job is configured to timeout on purpose
83+
wp.SubmitXT(wpxt.Job{
84+
Name: "Job 2 will timeout",
85+
Timeout: time.Duration(time.Millisecond * 1),
86+
Task: func(o wpxt.Options) wpxt.Response {
87+
// Simulate long running task
88+
time.Sleep(time.Second * 20)
89+
return wpxt.Response{Data: "timedout"}
90+
},
91+
})
9792

98-
// Or if you encounter an error within the code in your job
99-
wp.SubmitXT(wpxt.Job{
100-
Name: "Job 3 will encounter an error",
101-
Task: func(o wpxt.Options) wpxt.Response {
102-
err := fmt.Errorf("ErrorPretendException : something failed")
103-
if err != nil {
104-
return wpxt.Response{Error: err}
105-
}
106-
return wpxt.Response{Data: "error"}
107-
},
108-
})
93+
// Or if you encounter an error within the code in your job
94+
wp.SubmitXT(wpxt.Job{
95+
Name: "Job 3 will encounter an error",
96+
Task: func(o wpxt.Options) wpxt.Response {
97+
err := fmt.Errorf("ErrorPretendException : something failed")
98+
if err != nil {
99+
return wpxt.Response{Error: err}
100+
}
101+
return wpxt.Response{Data: "error"}
102+
},
103+
})
109104

110-
results := wp.StopWaitXT()
105+
results := wp.StopWaitXT()
111106

112-
for _, r := range results {
113-
if r.Error != nil {
114-
fmt.Println(r.Name(), "has failed with error :", r.Error.Error())
115-
} else {
116-
fmt.Println(r.Name(), "has passed successfully")
117-
}
107+
for _, r := range results {
108+
if r.Error != nil {
109+
fmt.Println(r.Name(), "has failed with error :", r.Error.Error())
110+
} else {
111+
fmt.Println(r.Name(), "has passed successfully")
118112
}
119113
}
120114

@@ -130,7 +124,7 @@ func main() {
130124
- Options are nothing more than `map[string]interface{}` so that you may supply anything you wish. This also simplifies accessing options within a job.
131125
- You can supply options along with the workerpool, or on a per job basis.
132126
- **If a job has options set, it overrides the defaults**
133-
- **We do not merge options**.
127+
- **We do not merge options**
134128

135129
#### Supply default options
136130

@@ -164,10 +158,13 @@ import (
164158
)
165159

166160
wp := wpxt.New(10, time.Duration(time.Second*10))
167-
myclient := &http.Client{}
168161

162+
httpclient := &http.Client{}
163+
k8s := kubernetes.Clientset{}
164+
165+
// This Job Only Needs an HTTP Client
169166
wp.SubmitXT(wpxt.Job{
170-
Name: "myjob",
167+
Name: "This Job Only Needs an HTTP Client",
171168
Options: map[string]interface{}{
172169
"http": myclient
173170
},
@@ -176,4 +173,16 @@ wp.SubmitXT(wpxt.Job{
176173
httpclient := o["http"]
177174
},
178175
})
176+
177+
// This Job Only Needs Kubernetes Clientset
178+
wp.SubmitXT(wpxt.Job{
179+
Name: "This Job Only Needs Kubernetes Clientset",
180+
Options: map[string]interface{}{
181+
"kube": k8s
182+
},
183+
Task: func(o wpxt.Options) wpxt.Response {
184+
// access options here
185+
kubernetesclient := o["kube"]
186+
},
187+
})
179188
```

workerpoolxt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ func (wp *WorkerPoolXT) getTimeout(job Job) time.Duration {
111111

112112
// getOptions decides which options to use : default or job
113113
func (wp *WorkerPoolXT) getOptions(job Job) Options {
114-
if wp.options == nil && job.Options == nil {
115-
return make(Options)
114+
if job.Options != nil {
115+
return job.Options
116116
}
117117
if wp.options != nil {
118118
return wp.options
119119
}
120-
return job.Options
120+
return make(Options)
121121
}
122122

123123
// processResponses listens for anything on the responses chan and appends the response to

workerpoolxt_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,46 @@ func TestPerJobOptions(t *testing.T) {
269269
}
270270
}
271271
}
272+
273+
func TestPerJobOptionsOverrideDefaultOptions(t *testing.T) {
274+
wp := New(3, time.Duration(time.Second*10))
275+
// set default options so we can verify they were overwritten by per job options
276+
opts := map[string]interface{}{"default": "value"}
277+
wp.WithOptions(opts)
278+
wp.SubmitXT(Job{
279+
Name: "job 1",
280+
Task: func(o Options) Response {
281+
// Set data to our opts myvar
282+
return Response{Data: o["var"]}
283+
},
284+
Options: map[string]interface{}{"var": "job1value"},
285+
})
286+
wp.SubmitXT(Job{
287+
Name: "job 2",
288+
Task: func(o Options) Response {
289+
// Set data to our opts myvar
290+
return Response{Data: o["var"]}
291+
},
292+
Options: map[string]interface{}{"var": "job2value"},
293+
})
294+
295+
res := wp.StopWaitXT()
296+
for _, result := range res {
297+
if result.Name() == "" {
298+
t.Fatalf("Expected option %s to be %s but got %s", "var", "not ''", "''")
299+
}
300+
if result.Data == "" {
301+
t.Fatalf("Expected data to not be null on : %s", result.Name())
302+
}
303+
if result.Name() == "job 1" {
304+
if result.Data != "job1value" {
305+
t.Fatalf("Expected %s option 'var' to be %s but got %s", result.Name(), "job1value", result.Data)
306+
}
307+
}
308+
if result.Name() == "job 2" {
309+
if result.Data != "job2value" {
310+
t.Fatalf("Expected %s option 'var' to be %s but got %s", result.Name(), "job2value", result.Data)
311+
}
312+
}
313+
}
314+
}

0 commit comments

Comments
 (0)