|
7 | 7 | "fmt"
|
8 | 8 | "log"
|
9 | 9 | "os"
|
| 10 | + "time" |
10 | 11 |
|
11 | 12 | "github.com/godror/godror"
|
12 | 13 | )
|
@@ -48,85 +49,90 @@ func main() {
|
48 | 49 | }
|
49 | 50 | fmt.Printf("Listening for messages... start time: %s\n", thedate)
|
50 | 51 | ctx := context.Background()
|
51 |
| - for { |
52 |
| - listenForMessages(ctx, db) |
53 |
| - } |
| 52 | + listenForMessages(ctx, db) |
54 | 53 | }
|
55 | 54 |
|
56 | 55 | func listenForMessages(ctx context.Context, db *sql.DB) {
|
57 | 56 |
|
58 |
| - tx, err := db.BeginTx(ctx, nil) |
59 |
| - fmt.Println("__________________________________________") |
60 |
| - //receive order... |
61 |
| - var orderJSON string |
62 |
| - dequeueOrderMessageSproc := `BEGIN dequeueOrderMessage(:1); END;` |
63 |
| - if _, err := db.ExecContext(ctx, dequeueOrderMessageSproc, sql.Out{Dest: &orderJSON}); err != nil { |
64 |
| - log.Printf("Error running %q: %+v", dequeueOrderMessageSproc, err) |
65 |
| - return |
66 |
| - } |
67 |
| - fmt.Println("orderJSON:" + orderJSON) |
68 |
| - type Order struct { |
69 |
| - Orderid string |
70 |
| - Itemid string |
71 |
| - Deliverylocation string |
72 |
| - Status string |
73 |
| - Inventorylocation string |
74 |
| - SuggestiveSale string |
75 |
| - } |
76 |
| - var order Order |
77 |
| - jsonerr := json.Unmarshal([]byte(orderJSON), &order) |
78 |
| - if jsonerr != nil { |
79 |
| - fmt.Printf("Order Unmarshal fmt.Sprint(data) err = %s", jsonerr) |
80 |
| - } |
81 |
| - fmt.Printf("order.orderid: %s", order.Orderid) |
82 |
| - fmt.Println("__________________________________________") |
83 |
| - //check inventory... |
84 |
| - var inventorylocation string |
85 |
| - sqlString := "update INVENTORY set INVENTORYCOUNT = INVENTORYCOUNT - 1 where INVENTORYID = :inventoryid and INVENTORYCOUNT > 0 returning inventorylocation into :inventorylocation" |
86 |
| - _, errFromInventoryCheck := db.Exec(sqlString, sql.Named("inventoryid", order.Itemid), sql.Named("inventorylocation", sql.Out{Dest: &inventorylocation})) |
87 |
| - if err != nil { |
88 |
| - fmt.Println("errFromInventoryCheck: %s", errFromInventoryCheck) |
89 |
| - } |
90 |
| - // numRows, err := res.RowsAffected() |
91 |
| - // if err != nil { |
92 |
| - // fmt.Println(errFromInventoryCheck) |
93 |
| - // } |
94 |
| - // fmt.Println("numRows:" + string(numRows)) |
95 |
| - if inventorylocation == "" { |
96 |
| - inventorylocation = "inventorydoesnotexist" |
97 |
| - } |
98 |
| - fmt.Println("inventorylocation:" + inventorylocation) |
99 |
| - fmt.Println("__________________________________________") |
100 |
| - //create inventory reply message... |
101 |
| - type Inventory struct { |
102 |
| - Orderid string `json:"orderid"` |
103 |
| - Itemid string `json:"itemid"` |
104 |
| - Inventorylocation string `json:"inventorylocation"` |
105 |
| - SuggestiveSale string `json:"suggestiveSale"` |
106 |
| - } |
107 |
| - inventory := &Inventory{ |
108 |
| - Orderid: order.Orderid, |
109 |
| - Itemid: order.Itemid, |
110 |
| - Inventorylocation: inventorylocation, |
111 |
| - SuggestiveSale: "beer", |
112 |
| - } |
113 |
| - inventoryJsonData, err := json.Marshal(inventory) |
114 |
| - if err != nil { |
115 |
| - fmt.Println(err) |
116 |
| - } |
117 |
| - inventoryJsonString := string(inventoryJsonData) |
118 |
| - fmt.Println("inventoryJsonData:" + inventoryJsonString) // :inventoryid |
119 |
| - messageSendSproc := `BEGIN enqueueInventoryMessage(:1); END;` |
120 |
| - if _, err := db.ExecContext(ctx, messageSendSproc, inventoryJsonString); err != nil { |
121 |
| - log.Printf("Error running %q: %+v", messageSendSproc, err) |
122 |
| - return |
123 |
| - } |
124 |
| - fmt.Println("inventory status message sent:" + inventoryJsonString) |
125 |
| - commiterr := tx.Commit() |
126 |
| - if commiterr != nil { |
127 |
| - fmt.Println("commiterr:", commiterr) |
| 57 | + for { |
| 58 | + tx, err := db.BeginTx(ctx, nil) |
| 59 | + // fmt.Println("__________________________________________") |
| 60 | + //receive order... |
| 61 | + var orderJSON string |
| 62 | + dequeueOrderMessageSproc := `BEGIN dequeueOrderMessage(:1); END;` |
| 63 | + if _, err := db.ExecContext(ctx, dequeueOrderMessageSproc, sql.Out{Dest: &orderJSON}); err != nil { |
| 64 | + log.Printf("Error running %q: %+v", dequeueOrderMessageSproc, err) |
| 65 | + return |
| 66 | + } |
| 67 | + // fmt.Println("orderJSON:" + orderJSON) |
| 68 | + type Order struct { |
| 69 | + Orderid string |
| 70 | + Itemid string |
| 71 | + Deliverylocation string |
| 72 | + Status string |
| 73 | + Inventorylocation string |
| 74 | + SuggestiveSale string |
| 75 | + } |
| 76 | + var order Order |
| 77 | + jsonerr := json.Unmarshal([]byte(orderJSON), &order) |
| 78 | + if jsonerr != nil { |
| 79 | + tx.Commit() |
| 80 | + continue |
| 81 | + // fmt.Printf("Order Unmarshal fmt.Sprint(data) err = %s", jsonerr) |
| 82 | + } |
| 83 | + if jsonerr == nil { |
| 84 | + fmt.Printf("order.orderid: %s", order.Orderid) |
| 85 | + } |
| 86 | + // fmt.Println("__________________________________________") |
| 87 | + //check inventory... |
| 88 | + var inventorylocation string |
| 89 | + sqlString := "update INVENTORY set INVENTORYCOUNT = INVENTORYCOUNT - 1 where INVENTORYID = :inventoryid and INVENTORYCOUNT > 0 returning inventorylocation into :inventorylocation" |
| 90 | + _, errFromInventoryCheck := db.Exec(sqlString, sql.Named("inventoryid", order.Itemid), sql.Named("inventorylocation", sql.Out{Dest: &inventorylocation})) |
| 91 | + if err != nil { |
| 92 | + fmt.Println("errFromInventoryCheck: %s", errFromInventoryCheck) |
| 93 | + } |
| 94 | + // numRows, err := res.RowsAffected() |
| 95 | + // if err != nil { |
| 96 | + // fmt.Println(errFromInventoryCheck) |
| 97 | + // } |
| 98 | + // fmt.Println("numRows:" + string(numRows)) |
| 99 | + if inventorylocation == "" { |
| 100 | + inventorylocation = "inventorydoesnotexist" |
| 101 | + } |
| 102 | + fmt.Println("inventorylocation:" + inventorylocation) |
| 103 | + // fmt.Println("__________________________________________") |
| 104 | + //create inventory reply message... |
| 105 | + type Inventory struct { |
| 106 | + Orderid string `json:"orderid"` |
| 107 | + Itemid string `json:"itemid"` |
| 108 | + Inventorylocation string `json:"inventorylocation"` |
| 109 | + SuggestiveSale string `json:"suggestiveSale"` |
| 110 | + } |
| 111 | + inventory := &Inventory{ |
| 112 | + Orderid: order.Orderid, |
| 113 | + Itemid: order.Itemid, |
| 114 | + Inventorylocation: inventorylocation, |
| 115 | + SuggestiveSale: "beer", |
| 116 | + } |
| 117 | + inventoryJsonData, err := json.Marshal(inventory) |
| 118 | + if err != nil { |
| 119 | + fmt.Println(err) |
| 120 | + } |
| 121 | + inventoryJsonString := string(inventoryJsonData) |
| 122 | + fmt.Println("inventoryJsonData:" + inventoryJsonString) // :inventoryid |
| 123 | + messageSendSproc := `BEGIN enqueueInventoryMessage(:1); END;` |
| 124 | + if _, err := db.ExecContext(ctx, messageSendSproc, inventoryJsonString); err != nil { |
| 125 | + log.Printf("Error running %q: %+v", messageSendSproc, err) |
| 126 | + return |
| 127 | + } |
| 128 | + fmt.Println("inventory status message sent:" + inventoryJsonString) |
| 129 | + commiterr := tx.Commit() |
| 130 | + if commiterr != nil { |
| 131 | + fmt.Println("commiterr:", commiterr) |
| 132 | + } |
| 133 | + fmt.Println("commit complete for message sent:" + inventoryJsonString) |
| 134 | + time.Sleep(1 * time.Second) |
128 | 135 | }
|
129 |
| - fmt.Println("commit complete for message sent:" + inventoryJsonString) |
130 | 136 | }
|
131 | 137 |
|
132 | 138 | func listenForMessagesAQAPI(ctx context.Context, db *sql.DB) { //todo incomplete - using PL/SQL above
|
|
0 commit comments