@@ -63,58 +63,52 @@ func main() {
63
63
How do I know if a job timed out? How do I handle an error in my job?
64
64
65
65
``` golang
66
- package main
67
-
68
66
import (
69
- " fmt"
70
- " time"
71
-
72
67
wpxt " github.com/oze4/workerpoolxt"
68
+ // ...
73
69
)
74
70
75
- func main () {
76
- wp := wpxt.New (3 , time.Duration (time.Second *10 ))
71
+ wp := wpxt.New (3 , time.Duration (time.Second *10 ))
77
72
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
+ })
85
80
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
+ })
97
92
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
+ })
109
104
110
- results := wp.StopWaitXT ()
105
+ results := wp.StopWaitXT ()
111
106
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" )
118
112
}
119
113
}
120
114
@@ -130,7 +124,7 @@ func main() {
130
124
- Options are nothing more than ` map[string]interface{} ` so that you may supply anything you wish. This also simplifies accessing options within a job.
131
125
- You can supply options along with the workerpool, or on a per job basis.
132
126
- ** If a job has options set, it overrides the defaults**
133
- - ** We do not merge options** .
127
+ - ** We do not merge options**
134
128
135
129
#### Supply default options
136
130
@@ -164,10 +158,13 @@ import (
164
158
)
165
159
166
160
wp := wpxt.New (10 , time.Duration (time.Second *10 ))
167
- myclient := &http.Client {}
168
161
162
+ httpclient := &http.Client {}
163
+ k8s := kubernetes.Clientset {}
164
+
165
+ // This Job Only Needs an HTTP Client
169
166
wp.SubmitXT (wpxt.Job {
170
- Name : " myjob " ,
167
+ Name : " This Job Only Needs an HTTP Client " ,
171
168
Options : map [string ]interface {}{
172
169
" http" : myclient
173
170
},
@@ -176,4 +173,16 @@ wp.SubmitXT(wpxt.Job{
176
173
httpclient := o[" http" ]
177
174
},
178
175
})
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
+ })
179
188
```
0 commit comments