Example #1
0
func main() {
	logger.Info("Sending messages...")

	// create sqs client
	client, err := sqs.NewFrom(os.Getenv("ADMIN_AWS_ACCESS_KEY_ID"), os.Getenv("ADMIN_AWS_SECRET_ACCESS_KEY"), "us-east-1")
	if err != nil {
		logger.Fatal("CLIENT ERROR:", err, "asdf", "asdfdasfd")
	}

	// get the SQS queue
	queue, err := client.GetQueue(os.Getenv("SQS_RECIEVE_QUEUE"))
	if err != nil {
		logger.Fatal("QUEUE ERROR:", err)
	}

	for i := 0; i < 100; i++ {
		var wo work_order.WorkOrder
		current_time := time.Now()

		wo.Id = i
		wo.JobId = 1
		wo.Message = strconv.Itoa(rand.Intn(80))
		wo.CreatedAt = &current_time
		wo.UpdatedAt = &current_time
		wo.Queue = os.Getenv("SQS_RECIEVE_QUEUE")

		data, err := json.Marshal(wo)
		if err != nil {
			logger.Error("JSON error: %v", err)
		}
		queue.SendMessage(string(data))
	}

	// quit
	logger.Info("Exiting.")
	os.Exit(0)

}
Example #2
0
func main() {
	logger.Info("Starting worker v%s", VERSION)

	// get worker count
	workers, err := strconv.Atoi(os.Getenv("WORKER_COUNT"))
	if err != nil {
		workers = 10
	}
	logger.Info("Worker count: %d", workers)

	// access key, secret key, receive queue and report queue should be in ENV variables
	logger.Info("SQS queue: %s", os.Getenv("SQS_WORKER_QUEUE"))

	// create sqs client
	client, err := sqs.NewFrom(os.Getenv("SQS_WORKER_ACCESS_KEY"), os.Getenv("SQS_WORKER_SECRET_KEY"), "us-east-1")
	if err != nil {
		logger.Fatal("CLIENT ERROR: %v", err)
	}

	// get the SQS queue
	queue, err := client.GetQueue(os.Getenv("SQS_WORKER_QUEUE"))
	if err != nil {
		logger.Fatal("QUEUE ERROR: %v", err)
	}

	logger.Info("Worker started.")

	// create the wait group
	var wg sync.WaitGroup

	for {
		// get some messages from the sqs queue
		logger.Debug("Checking for messages on the queue...")
		resp, err := queue.ReceiveMessageWithVisibilityTimeout(workers, 60)
		if err != nil {
			logger.Error("Could not receive messages: %v", err)
			time.Sleep(10 * time.Second)
		}

		if cap(resp.Messages) == 0 {
			logger.Debug("Did not find any messages on the queue.")
		}

		// for each message
		for _, message := range resp.Messages {
			// get the message details
			wo, err := work_order.NewFromJson(message.Body)
			if err != nil {
				logger.Error("Could not process SQS message: %s with JSON ERROR: %v", message.MessageId, err)
			} else {
				// process the message in a goroutine
				wg.Add(1)
				go processMessage(queue, message, wo, &wg)
			}
		}

		// wait for each goroutine to exit
		wg.Wait()
	}

}