From f04767ac1cf463270b6e17e9533372559e3580a0 Mon Sep 17 00:00:00 2001 From: Lachlan Donald Date: Tue, 11 Sep 2018 13:24:15 +1000 Subject: [PATCH] Ensure that queue is deleted even if subscribe fails --- daemon.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/daemon.go b/daemon.go index 5d93aa8..ba4dda3 100644 --- a/daemon.go +++ b/daemon.go @@ -47,16 +47,21 @@ func (d *Daemon) Start(ctx context.Context) error { return err } - if err := d.Queue.Subscribe(); err != nil { - return err - } - // ensure the queue deletion happens only once var deleteOnce sync.Once defer func() { deleteOnce.Do(d.deleteQueue) }() + if err := d.Queue.Subscribe(); err != nil { + return err + } + defer func() { + if err := d.Queue.Unsubscribe(); err != nil { + log.WithError(err).Error("Failed to unsubscribe from sns topic") + } + }() + ch := make(chan *sqs.Message) go func() { @@ -173,11 +178,6 @@ func (d *Daemon) deleteQueue() { if err := d.Queue.Delete(); err != nil { log.WithError(err).Error("Failed to delete queue") } - - if err := d.Queue.Unsubscribe(); err != nil { - log.WithError(err).Error("Failed to unsubscribe from sns topic") - } - } func executeHandler(ctx context.Context, command *os.File, args []string) error {