Skip to content

Commit 2845f3c

Browse files
authored
Merge pull request #1 from Azure/haitao/paramstep-start
fix a bug where step can start before job start
2 parents 43490ec + ef4f6f3 commit 2845f3c

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ AsyncJob aiming to help you organize code in dependencyGraph(DAG), instead of a
1111
- a step would be started once all it's dependency is finished.
1212
- output of a step can be feed into next step as input, type is checked by go generics.
1313
- step is wrapped in [AsyncTask](github.com/Azure/go-asynctask) with strongType info preserved
14+
- you can feed parameters as a step as well.
1415

1516
# Usage
1617

@@ -47,7 +48,11 @@ AsyncJob aiming to help you organize code in dependencyGraph(DAG), instead of a
4748
```
4849

4950
### visualize of a job
50-
this visualize depend on github.com/hashicorp/terraform/dag, with some limitation, may need some upstream contribution.
51+
this visualize depend on [terraform/dag](github.com/hashicorp/terraform/dag), with some limitation, may need some upstream tweaks:
52+
- able to customize node name
53+
- able to distinguash type of node (param, executionBlock)
54+
- able to show state of node (pending, running, completed, failed)
55+
5156
```
5257
digraph {
5358
compound = "true"

job.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ func NewJob(name string) *Job {
5959
return j
6060
}
6161

62-
func InputParam[T any](j *Job, stepName string, value *T) *StepInfo[T] {
62+
func InputParam[T any](bCtx context.Context, j *Job, stepName string, value *T) *StepInfo[T] {
6363
step := newStepInfo[T](stepName, []string{j.rootJob.Name()})
64-
step.task = asynctask.NewCompletedTask(value)
64+
65+
instrumentedFunc := func(ctx context.Context) (*T, error) {
66+
j.rootJob.Wait(ctx)
67+
return value, nil
68+
}
69+
step.task = asynctask.Start(bCtx, instrumentedFunc)
6570

6671
j.Steps[stepName] = step
6772
j.registerStepInGraph(stepName, j.rootJob.Name())

job_test.go

+11-18
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,33 @@ func TestSimpleJob(t *testing.T) {
2222
jb.Wait(context.Background())
2323
}
2424

25-
func TestJobWithLoop(t *testing.T) {
26-
job := NewJob("sqlSummaryJob")
27-
jobLib := &SqlSummaryJobLib{}
28-
29-
bCtx := context.Background()
30-
AddStep(bCtx, job, "Step1", jobLib.GetConnection, []string{})
31-
AddStep(bCtx, job, "Step2", jobLib.GetConnection, []string{"Step1"})
32-
}
33-
3425
var _ JobBuilder = &SqlJobBuilder{}
3526

3627
type SqlJobBuilder struct {
37-
Table1 string
38-
Query1 string
39-
Table2 string
40-
Query2 string
28+
ServerName string
29+
Table1 string
30+
Query1 string
31+
Table2 string
32+
Query2 string
4133
}
4234

4335
func (sjb *SqlJobBuilder) BuildJob(bCtx context.Context) *Job {
4436
job := NewJob("sqlSummaryJob")
4537
jobLib := &SqlSummaryJobLib{}
4638

47-
connTsk, _ := AddStep(bCtx, job, "getConnection", jobLib.GetConnection, []string{})
39+
serverNameParamTask := InputParam(bCtx, job, "param_serverName", &sjb.ServerName)
40+
connTsk, _ := StepAfter(bCtx, job, "getConnection", serverNameParamTask, jobLib.GetConnection)
4841

4942
// TODO: handle error during BuildJob
5043

51-
table1ParamTsk := InputParam(job, "param_table1", &sjb.Table1)
44+
table1ParamTsk := InputParam(bCtx, job, "param_table1", &sjb.Table1)
5245
table1ClientTsk, _ := StepAfterBoth(bCtx, job, "getTableClient1", connTsk, table1ParamTsk, jobLib.GetTableClient)
53-
query1ParamTsk := InputParam(job, "param_query1", &sjb.Query1)
46+
query1ParamTsk := InputParam(bCtx, job, "param_query1", &sjb.Query1)
5447
qery1ResultTsk, _ := StepAfterBoth(bCtx, job, "queryTable1", table1ClientTsk, query1ParamTsk, jobLib.ExecuteQuery)
5548

56-
table2ParamTsk := InputParam(job, "param_table2", &sjb.Table2)
49+
table2ParamTsk := InputParam(bCtx, job, "param_table2", &sjb.Table2)
5750
table2ClientTsk, _ := StepAfterBoth(bCtx, job, "getTableClient2", connTsk, table2ParamTsk, jobLib.GetTableClient)
58-
query2ParamTsk := InputParam(job, "param_query2", &sjb.Query2)
51+
query2ParamTsk := InputParam(bCtx, job, "param_query2", &sjb.Query2)
5952
qery2ResultTsk, _ := StepAfterBoth(bCtx, job, "queryTable2", table2ClientTsk, query2ParamTsk, jobLib.ExecuteQuery)
6053

6154
StepAfterBoth(bCtx, job, "summarize", qery1ResultTsk, qery2ResultTsk, jobLib.SummarizeQueryResult)

sqljob_lib_test.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,39 @@ type SqlSummaryJobLib struct {
99
}
1010

1111
type SqlConnection struct {
12+
ServerName string
1213
}
1314

1415
type SqlTableClient struct {
16+
ServerName string
17+
TableName string
1518
}
1619

1720
type SqlQueryResult struct {
21+
Data map[string]interface{}
1822
}
1923

20-
type SummarizedResult struct{}
24+
type SummarizedResult struct {
25+
Data1 map[string]interface{}
26+
Data2 map[string]interface{}
27+
}
2128

22-
func (sql *SqlSummaryJobLib) GetConnection(ctx context.Context) (*SqlConnection, error) {
29+
func (sql *SqlSummaryJobLib) GetConnection(ctx context.Context, serverName *string) (*SqlConnection, error) {
2330
fmt.Println("GetConnection")
24-
return &SqlConnection{}, nil
31+
return &SqlConnection{ServerName: *serverName}, nil
2532
}
2633

2734
func (sql *SqlSummaryJobLib) GetTableClient(ctx context.Context, conn *SqlConnection, tableName *string) (*SqlTableClient, error) {
2835
fmt.Println("GetTableClient with tableName:", *tableName)
29-
return &SqlTableClient{}, nil
36+
return &SqlTableClient{ServerName: conn.ServerName, TableName: *tableName}, nil
3037
}
3138

32-
func (sql *SqlSummaryJobLib) ExecuteQuery(ctx context.Context, tableClient *SqlTableClient, queryName *string) (*SqlQueryResult, error) {
33-
fmt.Println("ExecuteQuery:", *queryName)
34-
return &SqlQueryResult{}, nil
39+
func (sql *SqlSummaryJobLib) ExecuteQuery(ctx context.Context, tableClient *SqlTableClient, queryString *string) (*SqlQueryResult, error) {
40+
fmt.Println("ExecuteQuery:", *queryString)
41+
return &SqlQueryResult{Data: map[string]interface{}{"serverName": tableClient.ServerName, "tableName": tableClient.TableName, "queryName": *queryString}}, nil
3542
}
3643

3744
func (sql *SqlSummaryJobLib) SummarizeQueryResult(ctx context.Context, result1 *SqlQueryResult, result2 *SqlQueryResult) (*SummarizedResult, error) {
3845
fmt.Println("SummarizeQueryResult")
39-
return &SummarizedResult{}, nil
46+
return &SummarizedResult{Data1: result1.Data, Data2: result2.Data}, nil
4047
}

0 commit comments

Comments
 (0)