Skip to content

Commit e4a12eb

Browse files
authored
Merge pull request #147 from nyaruka/cwatch_batcher_optional
Make batch queue processing optional on cloudwatch service
2 parents 4b55d8c + df2f8d4 commit e4a12eb

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

aws/cwatch/service.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,31 @@ type Service struct {
2121
}
2222

2323
// NewService creates a new Cloudwatch service with the given credentials and configuration
24-
func NewService(accessKey, secretKey, region, namespace, deployment string, wg *sync.WaitGroup) (*Service, error) {
24+
func NewService(accessKey, secretKey, region, namespace, deployment string) (*Service, error) {
2525
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
2626
if err != nil {
2727
return nil, err
2828
}
2929

30-
s := &Service{
30+
return &Service{
3131
Client: cloudwatch.NewFromConfig(cfg),
3232
namespace: namespace,
3333
deployment: types.Dimension{Name: aws.String("Deployment"), Value: aws.String(deployment)},
34-
}
35-
s.batcher = syncx.NewBatcher(s.processBatch, 100, time.Second*3, 1000, wg)
36-
37-
return s, nil
34+
}, nil
3835
}
3936

40-
func (s *Service) Start() {
37+
func (s *Service) StartQueue(wg *sync.WaitGroup) {
38+
if s.batcher != nil {
39+
panic("queue already started")
40+
}
41+
s.batcher = syncx.NewBatcher(s.processBatch, 100, time.Second*3, 1000, wg)
4142
s.batcher.Start()
4243
}
4344

44-
func (s *Service) Stop() {
45+
func (s *Service) StopQueue() {
46+
if s.batcher == nil {
47+
panic("queue wasn't started")
48+
}
4549
s.batcher.Stop()
4650
}
4751

aws/cwatch/service_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
)
1313

1414
func TestService(t *testing.T) {
15-
wg := &sync.WaitGroup{}
16-
17-
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "testing", wg)
15+
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "testing")
1816
assert.NoError(t, err)
1917

2018
assert.Equal(t, &cloudwatch.PutMetricDataInput{
@@ -41,9 +39,8 @@ func TestService(t *testing.T) {
4139
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20)},
4240
}))
4341

44-
svc.Start()
45-
46-
svc.Stop()
47-
42+
wg := &sync.WaitGroup{}
43+
svc.StartQueue(wg)
44+
svc.StopQueue()
4845
wg.Wait()
4946
}

0 commit comments

Comments
 (0)