func run(messages []sqs.Message) { numMessages := len(messages) log.Debugln("worker: Received", numMessages, " tasks") var wg sync.WaitGroup wg.Add(numMessages) for i := range messages { go func(m *sqs.Message) { // launch goroutine defer func() { if err := recover(); err != nil { trace := make([]byte, 2048) runtime.Stack(trace, true) log.Criticalln("worker:", err, string(trace)) } wg.Done() }() if err := handleMessage(m); err != nil { log.Errorln("worker:", err) } }(&messages[i]) } wg.Wait() }
// Task runner daemon loop func taskRunner() { time.Sleep(time.Second * 10) log.Debugln("worker: Start polling") for { resp, err := queue.ReceiveMessage(batchSize) if err != nil { log.Errorln(err) continue } messages := resp.Messages if len(messages) > 0 { run(messages) } else { time.Sleep(time.Millisecond * 500) } } log.Debugln("worker: Leaving polling") }