-
Notifications
You must be signed in to change notification settings - Fork 6
/
job_result.go
43 lines (35 loc) · 1.21 KB
/
job_result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package asyncjob
import (
"context"
)
type JobDefinitionWithResult[Tin, Tout any] struct {
*JobDefinition[Tin]
resultStep *StepDefinition[Tout]
}
func JobWithResult[Tin, Tout any](jd *JobDefinition[Tin], resultStep *StepDefinition[Tout]) (*JobDefinitionWithResult[Tin, Tout], error) {
sdGet, ok := jd.GetStep(resultStep.GetName())
if !ok || sdGet != resultStep {
return nil, ErrRefStepNotInJob
}
return &JobDefinitionWithResult[Tin, Tout]{
JobDefinition: jd,
resultStep: resultStep,
}, nil
}
type JobInstanceWithResult[Tin, Tout any] struct {
*JobInstance[Tin]
resultStep *StepInstance[Tout]
}
func (jd *JobDefinitionWithResult[Tin, Tout]) Start(ctx context.Context, input Tin, jobOptions ...JobOptionPreparer) *JobInstanceWithResult[Tin, Tout] {
ji := jd.JobDefinition.Start(ctx, input, jobOptions...)
return &JobInstanceWithResult[Tin, Tout]{
JobInstance: ji,
resultStep: getStrongTypedStepInstance(jd.resultStep, ji),
}
}
// Result returns the result of the job from result step.
//
// it doesn't wait for all steps to finish, you can use Result() after Wait() if desired.
func (ji *JobInstanceWithResult[Tin, Tout]) Result(ctx context.Context) (Tout, error) {
return ji.resultStep.task.Result(ctx)
}