Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CloudTasks.go
package main
import (
"context"
"log"
"math/rand"
"strconv"
"sync"
"time"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2beta3"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2beta3"
)
const (
projectID string = "mineral-minutia-820"
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
var (
wg sync.WaitGroup
)
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func transmitCustomerID(ctx context.Context, itemCount int, customerName string) {
defer wg.Done()
c, err := cloudtasks.NewClient(ctx)
if err != nil {
log.Fatalf("ERROR: ", err.Error())
}
queueName := "q1"
randTaskID := randStringBytes(40)
location := "us-central1"
parent := "projects/" + projectID + "/locations/" + location + "/queues/" + queueName
taskName := parent + "/tasks/" + randTaskID
req := &taskspb.CreateTaskRequest{
Parent: parent,
Task: &taskspb.Task{
Name: taskName,
PayloadType: &taskspb.Task_HttpRequest{
HttpRequest: &taskspb.HttpRequest{
Url: "https://www.43eskaton.com/",
HttpMethod: taskspb.HttpMethod_GET,
AuthorizationHeader: &taskspb.HttpRequest_OidcToken{
OidcToken: &taskspb.OidcToken{
ServiceAccountEmail: "cloudfilerscheduledbackups@mineral-minutia-820.iam.gserviceaccount.com",
Audience: "https://www.43eskaton.com/",
},
},
//Body: []byte("{ 'customerName1' : '" + customerName + "'}"),
},
},
},
}
resp, err := c.CreateTask(ctx, req)
if err != nil {
log.Fatalf("ERROR: ", err.Error())
}
_ = resp
log.Printf("%i >>>>>>>>>>> ,%s Enqueue Resposnse %s", strconv.Itoa(itemCount), customerName, resp.GetName())
}
func main() {
log.Printf("/Notify Called!: ")
rand.Seed(time.Now().UTC().UnixNano())
ctx := context.Background()
itemCount := 0
for {
itemCount++
if itemCount > 1 {
break
}
ran := rand.Intn(50)
time.Sleep(time.Duration(ran) * time.Millisecond)
wg.Add(1)
go transmitCustomerID(ctx, itemCount, strconv.Itoa(itemCount))
break
}
wg.Wait()
log.Printf("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment