-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
319 lines (250 loc) · 7.94 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package tax_receipt
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"runtime"
"strconv"
"strings"
"time"
_ "net/http/pprof"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
"cloud.google.com/go/storage"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/googleapis/gax-go/v2"
"github.com/googleapis/google-cloudevents-go/cloud/storagedata"
"github.com/zenthangplus/goccm"
"google.golang.org/protobuf/encoding/protojson"
)
var (
RESULT_BUCKET = Getenv("RESULT_BUCKET", "<tax_receipt_repository>")
ASSET_FOLDER = Getenv("ASSET_FOLDER", ".")
FIELD_DEMILITER = Getenv("FIELD_DEMILITER", ",")
RETRY_TIMEOUT = 60
QUEUE_PATH string
MAX_CONCURRENT int
err error
client *storage.Client
bucket *storage.BucketHandle
)
func init() {
MAX_CONCURRENT, err = strconv.Atoi(Getenv("MAX_CONCURRENT", "100"))
RETRY_TIMEOUT, _ = strconv.Atoi(Getenv("RETRY_TIMEOUT", "60"))
if client, err = storage.NewClient(context.Background()); err != nil {
log.Printf("GCS CONNECTION FAILED storage.Client: %v\n", err)
}
bucket = client.Bucket(RESULT_BUCKET)
// log.Printf("GCS CONNECTED:%+v", bucket)
QUEUE_PATH = Getenv("QUEUE_PATH", "projects/<your-gcp-project>/locations/<your-gcp-region>/queues/failed-receipt-retrigger")
functions.HTTP("ReceiptRequest", ReceiptRequest)
// functions.CloudEvent("ReceiptEvent", ReceiptEvent)
}
func ReceiptRequest(res http.ResponseWriter, req *http.Request) {
contentType := req.Header.Get("Content-type")
if contentType == "" {
contentType = "application/json"
}
query := req.URL.Query()
companyId := query.Get("companyId")
if companyId == "" {
companyId = "ID01"
}
tipe := query.Get("Tipe")
// log.Printf("receiving %s request for %s:%s from %s", contentType, companyId, tipe, req.RemoteAddr)
var results []string
if strings.Contains(contentType, "application/json") {
results = processJSONRequest(&req.Body, companyId, tipe)
} else {
if strings.Contains(contentType, "text/tsv") {
FIELD_DEMILITER = string('\t')
}
results = processCSVRequest(&req.Body, companyId, tipe)
}
log.Printf("Total %d files ; \n", len(results))
res.WriteHeader(http.StatusOK)
res.Header().Set("Content-Type", "text/plain") // this
res.Write([]byte(strings.Join(results, "\n")))
runtime.GC()
}
func ReceiptEvent(ctx context.Context, event event.Event) error {
// meta, _ := metadata.FromContext(ctx)
// log.Printf("ctx meta:%v", meta)
// expiration := meta.Timestamp.Add(60 * time.Second)
// if time.Now().After(expiration) {
// log.Printf("event timeout: halting retries for expired event '%v'", meta.EventID)
// return nil
// }
sourceClient, _ := storage.NewClient(ctx)
var data storagedata.StorageObjectData
if err := protojson.Unmarshal(event.Data(), &data); err != nil {
log.Printf("protojson.Unmarshal: %v", err)
}
paths := strings.Split(data.GetName(), "/")
companyId := paths[1]
contentType := data.GetContentType()
tipe := ""
srcObj := sourceClient.Bucket(data.GetBucket()).Object(data.GetName())
objReader, _ := srcObj.NewReader(ctx)
srcReader := io.NopCloser(objReader)
log.Printf("receiving %s event for %s:%s from %s", contentType, companyId, tipe, data.GetName())
var results []string
if strings.Contains(contentType, "json") {
results = processJSONRequest(&srcReader, companyId, tipe)
} else {
if strings.Contains(contentType, "tsv") {
FIELD_DEMILITER = string('\t')
}
results = processCSVRequest(&srcReader, companyId, tipe)
}
log.Printf("Total %d files ; \n", len(results))
runtime.GC()
return nil
}
func processCSVRequest(source *io.ReadCloser, companyId string, tipe string) []string {
c := goccm.New(MAX_CONCURRENT)
reader := bufio.NewReader(*source)
var results []string
firstLine, _, _ := reader.ReadLine()
HEADER = strings.Split(string(firstLine), FIELD_DEMILITER)
tipes := []string{"customer", "driver"}
if tipe != "" {
tipes = []string{tipe}
}
company := getCompany(companyId)
for {
byteline, _, err := reader.ReadLine()
if err == io.EOF {
break
}
c.Wait()
go func(line string) {
defer func() {
if panicInfo := recover(); panicInfo != nil {
log.Printf("in loop csv:%v:%s\n", panicInfo, line)
}
}()
for _, tpe := range tipes {
order := newOrder(tpe + FIELD_DEMILITER + line)
if order.IsValid() {
receipt := newReceipt(company, order)
res := ReceiptRender(receipt)
if res != "" {
results = append(results, res)
}
}
}
c.Done()
}(string(byteline))
}
c.WaitAllDone()
log.Printf("Total %d files ; \n", len(results))
return results
}
func processJSONRequest(source *io.ReadCloser, companyId string, tipe string) []string {
c := goccm.New(MAX_CONCURRENT)
company := getCompany(companyId)
orders := []Order{}
json.NewDecoder(*source).Decode(&orders)
// log.Printf("Orders:%+v\n", orders)
var results []string
tipes := []string{"customer", "driver"}
if tipe != "" {
tipes = []string{tipe}
}
for _, order := range orders {
for _, tipe := range tipes {
order.Tipe = tipe
if !order.IsValid() {
continue
}
c.Wait()
go func(order Order) {
// log.Printf("Order:%+v\n", order)
defer func() {
if panicInfo := recover(); panicInfo != nil {
log.Printf("in loop json :%v:%+v\n", panicInfo, order)
}
}()
receipt := newReceipt(company, order)
res := ReceiptRender(receipt)
if res != "" {
results = append(results, res)
}
c.Done()
}(order)
}
}
c.WaitAllDone()
// source.Close()
return results
}
func ReceiptRender(receipt *Receipt) string {
receiptPath := receipt.order.getReceiptPath(receipt.company.Id)
lineCtx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(RETRY_TIMEOUT*3))
defer cancel()
receiptFile := bucket.Object(receiptPath).Retryer(
// Use WithBackoff to control the timing of the exponential backoff.
storage.WithBackoff(gax.Backoff{
// Set the initial retry delay to a maximum of 2 seconds. The length of
// pauses between retries is subject to random jitter.
Initial: 5 * time.Second,
// Set the maximum retry delay to 60 seconds.
Max: time.Duration(RETRY_TIMEOUT) * time.Second,
// Set the backoff multiplier to 3.0.
Multiplier: 3,
}),
// Use WithPolicy to customize retry so that all requests are retried even
// if they are non-idempotent.
storage.WithPolicy(storage.RetryAlways),
)
receiptWriter := receiptFile.NewWriter(lineCtx)
receipt.render(receiptWriter)
if err := receiptWriter.Close(); err != nil {
defer cancel()
log.Printf("Error on writing %s:%s", receiptPath, err)
QueueFailedUpload(receipt)
return ""
}
return receiptPath
}
func QueueFailedUpload(receipt *Receipt) *taskspb.Task {
gTaskctx := context.Background()
client, err := cloudtasks.NewClient(gTaskctx)
if err != nil {
fmt.Printf("Tasks NewClient Failed: %v", err)
return nil
}
defer client.Close()
receiptPath := receipt.order.getReceiptPath(receipt.company.Id)
gcs_api_url := fmt.Sprintf("https://storage.googleapis.com/upload/storage/v1/b/%s/o?uploadType=media&name=%s", RESULT_BUCKET, receiptPath)
pdfByte := receipt.render(nil)
req := &taskspb.CreateTaskRequest{
Parent: QUEUE_PATH,
Task: &taskspb.Task{
MessageType: &taskspb.Task_HttpRequest{
HttpRequest: &taskspb.HttpRequest{
HttpMethod: taskspb.HttpMethod_POST,
Url: gcs_api_url,
AuthorizationHeader: &taskspb.HttpRequest_OauthToken{
OauthToken: &taskspb.OAuthToken{
ServiceAccountEmail: Getenv("SERVICE_ACCOUNT", "<your-gcp-project>@appspot.gserviceaccount.com"),
},
},
Headers: map[string]string{"Content-Type": "application/pdf", "Content-Size": fmt.Sprint(len(pdfByte))},
Body: pdfByte,
},
},
},
}
task, err := client.CreateTask(gTaskctx, req)
if err != nil {
log.Printf("Failed to requeue GCS Upload cloudtasks.CreateTask: %v ", err)
}
return task
}