-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (66 loc) · 1.66 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"time"
e "putrafirman/google-cloud-task-helper/entity"
s "putrafirman/google-cloud-task-helper/service"
"github.com/google/uuid"
)
type IniModel struct {
Nama string `json:"name"`
Score int `json:"score"`
}
func main() {
/*
example default queue config:
name:"**2"
rate_limits:{max_dispatches_per_second:500 max_burst_size:100 max_concurrent_dispatches:1000}
retry_config:{max_attempts:10 min_backoff:{nanos:100000000} max_backoff:{seconds:3600} max_doublings:16}
state:RUNNING
*/
/*
Init Service
*/
taskService := s.NewTaskService("project-name", "asia-southeast1", "rg-live-task-dev-alpha2")
ctx := context.Background()
/*
Example Creating New Queue and Handling
*/
respQueue, errQueue := taskService.CreateQueue(ctx, 10)
if errQueue != nil {
if strings.Contains(errQueue.Error(), "AlreadyExists") {
fmt.Println("queue already created")
}
fmt.Println(errQueue)
}
fmt.Println(respQueue)
fmt.Println("======")
/*
Example Create New Task inside Queue
*/
testStruct := IniModel{
Nama: "Putra",
Score: 1,
}
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(testStruct)
id := uuid.New()
resp, err := taskService.CreateHTTPTask(ctx, "your-http-target", "coba-dari-local-schedule-"+id.String(), reqBodyBytes.Bytes(), e.HttpMethod_POST, time.Now())
if err != nil {
fmt.Println(err)
}
fmt.Println(resp)
fmt.Println("======")
/*
Example List Task inside Queue
*/
respList, _ := taskService.ListTask(ctx)
for _, task := range respList {
fmt.Println(task)
fmt.Println(task.ScheduleTime.AsTime().Format(time.RFC3339))
}
}