/** * Run SQS * * @param array $tasks * @param null $priority * @return bool * @throws SqsException */ protected function run($tasks = [], $priority = null) { if (empty($tasks)) { throw new SqsException($this->no_tasks); } try { $data = $this->getMessages($priority); $skipped = 0; $processed = 0; foreach ($data as $message) { $body = (array) json_decode($message['Body']); $receipt = $message['ReceiptHandle']; $id = $body[$this->sqs_id_key]; $data = $body; /** * If priority only execute tasks for priority */ if ($priority != null) { $priority_value = $message['MessageAttributes']['Priority']['StringValue']; if (empty($priority_value) || $priority_value != $priority) { $skipped++; continue; } } $this->logMsg($this->sqs_id_key . ': ' . $this->Styles->blue($id), true); /** * Execute Tasks */ $statuses = $this->executeTasks($id, $data, $tasks); $processed++; /** * Handle Failures */ if (in_array(false, $statuses)) { $this->failure($data); } /** * Clear sqs message */ if (!$this->sqs->delete($receipt)) { $this->logMsg('Failed to delete sqs message: ' . $id . ' ' . $receipt, false, true, true); } } $this->logMsg('Processed: ' . $this->Styles->blue($processed) . ' Skipped: ' . $this->Styles->blue($skipped) . '.'); return true; } catch (Exception $e) { $error = $this->other_error . "\n\n" . $e->getMessage() . "\n\n"; if (isset($body) && !empty($body)) { $error .= var_export($body, true) . "\n\n"; } $this->logMsg($error, false, true); return false; } }