<commit_before>package worker

import (
	"encoding/json"
	"strconv"

	"github.com/AdRoll/goamz/aws"
	"github.com/AdRoll/goamz/sqs"
)

const (
	sqsSizeKey = "ApproximateNumberOfMessages"
)

type sqsMessage struct {
	Msg *sqs.Message
	*Envelope
}

func newSQSMessage(msg *sqs.Message, payload []byte) (*sqsMessage, error) {
	env, err := NewEnvelope(payload)
	if err != nil {
		return nil, err
	}

	benv := &sqsMessage{
		Msg:      msg,
		Envelope: env,
	}

	return benv, nil
}

type SQSQueue struct {
	name   string
	queue  *sqs.Queue
	region aws.Region
}

func NewSQSQueue(name string, opts ...func(*SQSQueue)) (Queue, error) {
	q := &SQSQueue{
		name:   name,
		region: aws.USEast,
	}

	auth, err := aws.EnvAuth()
	if err != nil {
		return nil, err
	}

	// Get a reference to SQS region.
	service := sqs.New(auth, q.region)
	queue, err := service.GetQueue(name)
	if err != nil {
		return nil, err
	}

	q.queue = queue

	return q, nil
}

func (q *SQSQueue) Put(j Job) error {
	typ, err := StructType(j)
	if err != nil {
		return err
	}

	job := &Payload{
		Type: typ,
		Args: j,
	}

	payload, err := json.Marshal(job)
	if err != nil {
		return err
	}

	_, err = q.queue.SendMessage(string(payload))
	return err
}

func (q *SQSQueue) Get() (Message, error) {
	resp, err := q.queue.ReceiveMessage(1)
	if err != nil {
		return nil, err
	}

	if len(resp.Messages) == 0 {
		return nil, &Error{Err: "timeout", IsTimeout: true}
	}

	msg := resp.Messages[0]
	env, err := newSQSMessage(&msg, []byte(msg.Body))
	if err != nil {
		return nil, err
	}

	return env, nil
}

func (q *SQSQueue) Delete(msg Message) error {
	env, ok := msg.(*sqsMessage)
	if !ok {
		return NewErrorFmt("bad sqs envelope")
	}

	_, err := q.queue.DeleteMessage(env.Msg)
	return err
}

func (q *SQSQueue) Reject(msg Message) error {
	return nil
}

func (q *SQSQueue) Size() (uint64, error) {
	resp, err := q.queue.GetQueueAttributes(sqsSizeKey)
	if err != nil {
		return 0, err
	}

	if len(resp.Attributes) == 0 {
		return 0, NewError("bad attribute size")
	}

	attr := resp.Attributes[0]
	if attr.Name != sqsSizeKey {
		return 0, NewError("bad attribute")
	}

	return strconv.ParseUint(attr.Value, 10, 64)
}<commit_msg>Add comments to SQSQueue<commit_after>package worker

import (
	"encoding/json"
	"strconv"

	"github.com/AdRoll/goamz/aws"
	"github.com/AdRoll/goamz/sqs"
)

const (
	sqsSizeKey = "ApproximateNumberOfMessages"
)

// sqsMessage represents a SQS message.
type sqsMessage struct {
	Msg       *sqs.Message // Original SQS message.
	*Envelope              // Holds parsed payload in JSON format.
}

// newSQSMessage returns an instance of sqsMessage.
func newSQSMessage(msg *sqs.Message, payload []byte) (*sqsMessage, error) {
	env, err := NewEnvelope(payload)
	if err != nil {
		return nil, err
	}

	benv := &sqsMessage{
		Msg:      msg,
		Envelope: env,
	}

	return benv, nil
}

// SQSQueue represents a SQS queue, the queue should be configured
// to use redrive policy to move the failed jobs to a dead letter queue.
type SQSQueue struct {
	name   string     // AWS SQS queue name.
	queue  *sqs.Queue // AWS SQS Queue reference.
	region aws.Region // AWS region.
}

// NewSQSQueue returns an instance of SQSQueue using custom options.
func NewSQSQueue(name string, opts ...func(*SQSQueue)) (Queue, error) {
	q := &SQSQueue{
		name:   name,
		region: aws.USEast,
	}

	auth, err := aws.EnvAuth()
	if err != nil {
		return nil, err
	}

	// Get a reference to SQS region.
	service := sqs.New(auth, q.region)
	queue, err := service.GetQueue(name)
	if err != nil {
		return nil, err
	}

	q.queue = queue

	return q, nil
}

// Put puts the job in the queue.
func (q *SQSQueue) Put(j Job) error {
	typ, err := StructType(j)
	if err != nil {
		return err
	}

	job := &Payload{
		Type: typ,
		Args: j,
	}

	payload, err := json.Marshal(job)
	if err != nil {
		return err
	}

	_, err = q.queue.SendMessage(string(payload))
	return err
}

// Get peeks a message from the queue.
func (q *SQSQueue) Get() (Message, error) {
	resp, err := q.queue.ReceiveMessage(1)
	if err != nil {
		return nil, err
	}

	if len(resp.Messages) == 0 {
		return nil, &Error{Err: "timeout", IsTimeout: true}
	}

	msg := resp.Messages[0]
	env, err := newSQSMessage(&msg, []byte(msg.Body))
	if err != nil {
		return nil, err
	}

	return env, nil
}

// Delete deletes a message from the queue.
func (q *SQSQueue) Delete(msg Message) error {
	env, ok := msg.(*sqsMessage)
	if !ok {
		return NewErrorFmt("bad sqs envelope")
	}

	_, err := q.queue.DeleteMessage(env.Msg)
	return err
}

// Reject is NOOP required to implement Queue interface.
func (q *SQSQueue) Reject(msg Message) error {
	return nil
}

// Size returns aproximate queue size.
func (q *SQSQueue) Size() (uint64, error) {
	resp, err := q.queue.GetQueueAttributes(sqsSizeKey)
	if err != nil {
		return 0, err
	}

	if len(resp.Attributes) == 0 {
		return 0, NewError("bad attribute size")
	}

	attr := resp.Attributes[0]
	if attr.Name != sqsSizeKey {
		return 0, NewError("bad attribute")
	}

	return strconv.ParseUint(attr.Value, 10, 64)
}
{'fraction_non_alphanumeric': 0.13741820345032718, 'fraction_numerical': 0.005552250644457664, 'mean_word_length': 3.3296137339055796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3235639', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1894, 'n_words': 558}