Skip to content

Instantly share code, notes, and snippets.

@nathandaly
Created June 8, 2023 18:02
Show Gist options
  • Save nathandaly/0a4724d52695a11eb684de63dab58bd7 to your computer and use it in GitHub Desktop.
Save nathandaly/0a4724d52695a11eb684de63dab58bd7 to your computer and use it in GitHub Desktop.
ChatGPT: Using GoLang's coroutines to handle Laravel jobs
package main
import (
"fmt"
"log"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sqs"
)
func main() {
// Create an AWS session
sess, err := session.NewSession(&aws.Config{
Region: aws.String("your-aws-region"),
// Add other AWS credentials or configuration if necessary
})
if err != nil {
log.Fatal(err)
}
// Create SQS and S3 client
sqsClient := sqs.New(sess)
s3Client := s3.New(sess)
// Retrieve messages from SQS queue
queueURL := "your-sqs-queue-url"
maxMessages := int64(10) // Maximum number of messages to retrieve at once
result, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: &queueURL,
MaxNumberOfMessages: &maxMessages,
})
if err != nil {
log.Fatal(err)
}
// Create a wait group to wait for all goroutines to finish
var wg sync.WaitGroup
// Process each message concurrently using goroutines
for _, msg := range result.Messages {
wg.Add(1)
go func(msg *sqs.Message) {
defer wg.Done()
// Extract S3 object key from Laravel's payload
s3Key := extractS3Key(msg)
// Get the object from S3
objectData, err := getObjectFromS3(s3Client, "your-s3-bucket", s3Key)
if err != nil {
log.Printf("Failed to retrieve object from S3: %v", err)
return
}
// Process the object
processObject(objectData)
// Send the results to the endpoint
err = sendResultsToEndpoint(objectData)
if err != nil {
log.Printf("Failed to send results to endpoint: %v", err)
return
}
// Delete the processed message from SQS
err = deleteMessageFromSQS(sqsClient, queueURL, msg.ReceiptHandle)
if err != nil {
log.Printf("Failed to delete message from SQS: %v", err)
return
}
log.Printf("Successfully processed message: %s", *msg.MessageId)
}(msg)
}
// Wait for all goroutines to finish
wg.Wait()
fmt.Println("All messages processed")
}
func extractS3Key(msg *sqs.Message) string {
// TODO: Extract S3 key from Laravel's payload
// Modify this function based on your Laravel job payload structure
return "your-s3-object-key"
}
func getObjectFromS3(s3Client *s3.S3, bucket string, key string) ([]byte, error) {
// TODO: Get the object from S3
// Modify this function based on your S3 interaction logic
return []byte{}, nil
}
func processObject(objectData []byte) {
// TODO: Process the object
// Add your custom logic to process the object retrieved from S3
}
func sendResultsToEndpoint(objectData []byte) error {
// TODO: Send the results to the endpoint
// Add your custom logic to send the processed results to the desired endpoint
return nil
}
func deleteMessageFromSQS(sqsClient *sqs.SQS, queueURL string, receiptHandle *string) error {
_, err := sqsClient.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: &queueURL,
ReceiptHandle: receiptHandle,
})
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment