Skip to content

Commit

Permalink
Ensure that queue is deleted even if subscribe fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Sep 11, 2018
1 parent c29255d commit f04767a
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f04767a

Please sign in to comment.