-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (52 loc) · 1.16 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"github.com/twilio/twilio-go"
openapi "github.com/twilio/twilio-go/rest/api/v2010"
)
const (
workerCount = 10
pageSize = 100
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
client := twilio.NewRestClient()
params := &openapi.ListCallParams{}
params.SetPageSize(pageSize)
params.SetStatus("queued")
ch, _ := client.Api.StreamCall(params)
wg := &sync.WaitGroup{}
wg.Add(workerCount)
for i := 0; i < workerCount; i++ {
go work(ctx, wg, i, client, ch)
}
wg.Wait()
}
func work(ctx context.Context, wg *sync.WaitGroup, i int, client *twilio.RestClient, ch <-chan openapi.ApiV2010Call) {
defer wg.Done()
params := &openapi.UpdateCallParams{}
params.SetStatus("canceled")
for {
select {
case <-ctx.Done():
log.Printf("worker %d: %s", i, ctx.Err())
return
case record, ok := <-ch:
if !ok {
return
}
resp, err := client.Api.UpdateCall(*record.Sid, params)
if err != nil {
log.Printf("client.Api.UpdateCall(%s) failed", *record.Sid)
return
}
fmt.Printf("worker %d: %s\n", i, *resp.Sid)
}
}
}