Skip to content

Commit

Permalink
added a function to wait until job is completed before returning
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunday Akin authored and Sunday Akin committed Feb 3, 2023
1 parent 97d94fc commit 0f13d80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
token.json
token.json
result.json
36 changes: 7 additions & 29 deletions db2Test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"

"github.com/asolpshinning/db2-warehouse/utils"

Expand All @@ -14,33 +15,10 @@ func main() {
userName := utils.GoDotEnv("username")
password := utils.GoDotEnv("password")
host := utils.GoDotEnv("host")
sqlCommand := `select APPLICATION_NAME, IMAP_NUMBER, APP_DOMAIN FROM COE.APPLICATION_DIM fetch first 5 rows only;select TECHNICAL_OWNER, PRODUCT_OWNER, LOAD_DATE from COE.APPLICATION_DIM fetch first 5 rows only;`
/* sqlCommand2 := `SELECT
cad.CIRRUS_APP_NAME ,
cad.ENV ,
cpd.NAMESPACE ,
cad.PROJECT_ID ,
cad.QUOTA ,
cad.HS_ACTIVE ,
cad.IMAP_NUMBER ,
ad.APP_DOMAIN ,
ad.APP_DOMAIN_1 ,
ad.APP_DOMAIN_2 ,
ad.PRODUCT_OWNER ,
ad.TECHNICAL_OWNER ,
cad.START_DATE ,
cad.END_DATE,
cad.ACTIVE
FROM
COE.CIRRUS_APPLICATION_DIM cad
JOIN COE.CIRRUS_PROJECT_DIM cpd
ON
cpd.PROJECT_ID = cad.PROJECT_ID
JOIN COE.APPLICATION_DIM ad
ON
ad.IMAP_NUMBER = cpd.IMAP_NUMBER` */

limit := 10
//sqlCommand := `select APPLICATION_NAME, IMAP_NUMBER, APP_DOMAIN FROM COE.APPLICATION_DIM fetch first 5 rows only;select TECHNICAL_OWNER, PRODUCT_OWNER, LOAD_DATE from COE.APPLICATION_DIM fetch first 5 rows only;`
//sqlCommand := `SELECT * FROM COE.CIRRUS_AUTO_SCALING_V`
sqlCommand := `SELECT * FROM COE.CIRRUS_AUTO_SCALING_V WHERE ACTIVE = 'Y' AND ENV = 'prod' OFFSET 1000 ROWS FETCH NEXT 100 ROWS ONLY`
limit := 100

result, err := jobs.GetResultFromJob(userName, password, host, sqlCommand, limit)
if err != nil {
Expand All @@ -51,6 +29,6 @@ func main() {
if err != nil {
panic(err)
}
fmt.Println(string(jsonResult))

ioutil.WriteFile("result.json", jsonResult, 0644)
fmt.Println("Done! Check the result.json file")
}
17 changes: 16 additions & 1 deletion jobs/getResultFromJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/asolpshinning/db2-warehouse/jobs/auth"
)

func GetResultFromJob(username, password, host, sqlCommand string, limit int) (map[string]interface{}, error) {
func getResultFromJob(username, password, host, sqlCommand string, limit int) (map[string]interface{}, error) {

token, err := auth.RequestToken(username, password, host)
if err != nil {
Expand Down Expand Up @@ -48,3 +48,18 @@ func GetResultFromJob(username, password, host, sqlCommand string, limit int) (m

return data, nil
}

func GetResultFromJob(username, password, host, sqlCommand string, limit int) (map[string]interface{}, error) {

result, err := getResultFromJob(username, password, host, sqlCommand, limit)
if err != nil {
return nil, err
}
for result["status"] != "completed" {
result, err = getResultFromJob(username, password, host, sqlCommand, limit)
if err != nil {
return nil, err
}
}
return result, nil
}

0 comments on commit 0f13d80

Please sign in to comment.