func Main() { var ( SQS *sqs.SQS getUserQueueUrlOutput *sqs.GetQueueUrlOutput getContainerQueueUrlOutput *sqs.GetQueueUrlOutput UserQueueUrl *string ContainerQueueUrl *string Dynamo *dynamodb.DynamoDB socialWorker *workers.SocialWorker ) SQS = sqs.New(&aws.Config{Region: aws.String("cn-north-1")}) getUserQueueUrlOutput, err := SQS.GetQueueUrl(&sqs.GetQueueUrlInput{QueueName: aws.String(USER_QUEUE_NAME)}) if err != nil { glog.Errorln("Error on connect user queue url:", err.Error()) return } UserQueueUrl = getUserQueueUrlOutput.QueueUrl getContainerQueueUrlOutput, err = SQS.GetQueueUrl(&sqs.GetQueueUrlInput{QueueName: aws.String(CONTAINER_QUEUE_NAME)}) if err != nil { glog.Errorln("Error on connect container queue url:", err.Error()) return } ContainerQueueUrl = getContainerQueueUrlOutput.QueueUrl Dynamo = dynamodb.New(&aws.Config{Region: aws.String("cn-north-1")}) socialWorker = workers.NewSocialWorker(SQS, UserQueueUrl, ContainerQueueUrl, Dynamo) socialWorker.Start() }
func ExampleSQS_ChangeMessageVisibilityBatch() { svc := sqs.New(session.New()) params := &sqs.ChangeMessageVisibilityBatchInput{ Entries: []*sqs.ChangeMessageVisibilityBatchRequestEntry{ // Required { // Required Id: aws.String("String"), // Required ReceiptHandle: aws.String("String"), // Required VisibilityTimeout: aws.Int64(1), }, // More values... }, QueueUrl: aws.String("String"), // Required } resp, err := svc.ChangeMessageVisibilityBatch(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleSQS_SetQueueAttributes() { svc := sqs.New(nil) params := &sqs.SetQueueAttributesInput{ Attributes: map[string]*string{ // Required "Key": aws.String("String"), // Required // More values... }, QueueURL: aws.String("String"), // Required } resp, err := svc.SetQueueAttributes(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { // Generic AWS Error with Code, Message, and original error (if any) fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr()) if reqErr, ok := err.(awserr.RequestFailure); ok { // A service error occurred fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()) } } else { // This case should never be hit, the SDK should always return an // error which satisfies the awserr.Error interface. fmt.Println(err.Error()) } } // Pretty-print the response data. fmt.Println(awsutil.StringValue(resp)) }
func ExampleSQS_ChangeMessageVisibility() { sess, err := session.NewSession() if err != nil { fmt.Println("failed to create session,", err) return } svc := sqs.New(sess) params := &sqs.ChangeMessageVisibilityInput{ QueueUrl: aws.String("String"), // Required ReceiptHandle: aws.String("String"), // Required VisibilityTimeout: aws.Int64(1), // Required } resp, err := svc.ChangeMessageVisibility(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleSQS_AddPermission() { svc := sqs.New(session.New()) params := &sqs.AddPermissionInput{ AWSAccountIds: []*string{ // Required aws.String("String"), // Required // More values... }, Actions: []*string{ // Required aws.String("String"), // Required // More values... }, Label: aws.String("String"), // Required QueueUrl: aws.String("String"), // Required } resp, err := svc.AddPermission(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleSQS_DeleteMessageBatch() { sess, err := session.NewSession() if err != nil { fmt.Println("failed to create session,", err) return } svc := sqs.New(sess) params := &sqs.DeleteMessageBatchInput{ Entries: []*sqs.DeleteMessageBatchRequestEntry{ // Required { // Required Id: aws.String("String"), // Required ReceiptHandle: aws.String("String"), // Required }, // More values... }, QueueUrl: aws.String("String"), // Required } resp, err := svc.DeleteMessageBatch(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleSQS_RemovePermission() { sess, err := session.NewSession() if err != nil { fmt.Println("failed to create session,", err) return } svc := sqs.New(sess) params := &sqs.RemovePermissionInput{ Label: aws.String("String"), // Required QueueUrl: aws.String("String"), // Required } resp, err := svc.RemovePermission(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func (q *SQSQueue) Get() (*ScriptCommand, error) { sess := session.New(&aws.Config{ Region: aws.String(viper.GetString("queue.region")), Credentials: credentials.NewStaticCredentials( viper.GetString("credentials.aws_access_id"), viper.GetString("credentials.aws_secret"), "", ), }) svc := sqs.New(sess) params := &sqs.ReceiveMessageInput{ QueueUrl: aws.String(viper.GetString("queue.url")), AttributeNames: []*string{}, MaxNumberOfMessages: aws.Int64(1), MessageAttributeNames: []*string{}, VisibilityTimeout: aws.Int64(1), } resp, err := svc.ReceiveMessage(params) if len(resp.Messages) > 0 { dec := json.NewDecoder(strings.NewReader(*resp.Messages[0].Body)) var cmd *ScriptCommand err = dec.Decode(&cmd) cmd.Receipt = *resp.Messages[0].ReceiptHandle return cmd, err } else { return nil, nil } }
func ExampleSQS_AddPermission() { svc := sqs.New(nil) params := &sqs.AddPermissionInput{ AWSAccountIDs: []*string{ // Required aws.String("String"), // Required // More values... }, Actions: []*string{ // Required aws.String("String"), // Required // More values... }, Label: aws.String("String"), // Required QueueURL: aws.String("String"), // Required } resp, err := svc.AddPermission(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { // Generic AWS error with Code, Message, and original error (if any) fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr()) if reqErr, ok := err.(awserr.RequestFailure); ok { // A service error occurred fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()) } } else { // This case should never be hit, the SDK should always return an // error which satisfies the awserr.Error interface. fmt.Println(err.Error()) } } // Pretty-print the response data. fmt.Println(awsutil.Prettify(resp)) }
func pollMessage(queueUrl string) { sqsClient := sqs.New(&aws.Config{Region: aws.String("us-east-1")}) req := &sqs.ReceiveMessageInput{QueueURL: aws.String(queueUrl)} result, err := sqsClient.ReceiveMessage(req) if nil != err { panic(err) } else { parsedResponse, err := gabs.ParseJSON([]byte(*result.Messages[0].Body)) if nil != err { panic(err) } var payload string payload = parsedResponse.Path("Message").Data().(string) fmt.Println(unpretty(payload)) deleteRequest := &sqs.DeleteMessageInput{QueueURL: aws.String(queueUrl), ReceiptHandle: aws.String(*result.Messages[0].ReceiptHandle)} _, err = sqsClient.DeleteMessage(deleteRequest) if nil != err { panic(err) } } }
// waitForResult waits for the job to be processed and the job result to be added // to the job result SQS queue. This will pool the SQS queue for job results until // a job result matches the file it uploaded. When a match is found the job result // will also be deleted from the queue, and its status written to the console. // If the job result doesn't match the file uploaded by this client, the message // will be ignored, so another client could received it. func waitForResult(bucket, filename, resultQueueURL string) { svc := sqs.New(nil) for { resp, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{ QueueUrl: aws.String(resultQueueURL), VisibilityTimeout: aws.Int64(0), WaitTimeSeconds: aws.Int64(20), }) if err != nil { log.Println("Failed to receive mesasge", err) time.Sleep(30 * time.Second) continue } for _, msg := range resp.Messages { result := &wordfreq.JobResult{} if err := json.Unmarshal([]byte(aws.StringValue(msg.Body)), result); err != nil { log.Println("Failed to unmarshal message", err) continue } if result.Job.Bucket != bucket || result.Job.Key != filename { continue } printResult(result) svc.DeleteMessage(&sqs.DeleteMessageInput{ QueueUrl: aws.String(resultQueueURL), ReceiptHandle: msg.ReceiptHandle, }) return } } }
func main() { flag.Parse() config, err := LoadConfig(configFilePath) if err != nil { log.Fatalf("Error loading config file: %s", err) } logger := buildLogger(config.LogLevel) awsConfig := aws.NewConfig().WithRegion(config.SQSConfig.Region) awsSession := session.New(awsConfig) sqssvc := sqs.New(awsSession) queue := awssqs.NewSQSQueue(sqssvc, logger) iamsvc := iam.New(awsSession) user := awsiam.NewIAMUser(iamsvc, logger) serviceBroker := sqsbroker.New(config.SQSConfig, queue, user, logger) credentials := brokerapi.BrokerCredentials{ Username: config.Username, Password: config.Password, } brokerAPI := brokerapi.New(serviceBroker, logger, credentials) http.Handle("/", brokerAPI) fmt.Println("SQS Service Broker started on port " + port + "...") http.ListenAndServe(":"+port, nil) }
func main() { // Create SQS instance s := sqs.New(session.New(), &aws.Config{Region: aws.String("us-west-2")}) // Create Queue instance q, err := queue.New(s, "test") if err != nil { log.Fatal(err) } for { messages, err := q.ReceiveMessage(option.MaxNumberOfMessages(10)) if err != nil { log.Println(err) continue } if len(messages) > 0 { for _, m := range messages { log.Println(*m.Body) q.DeleteMessage(m.ReceiptHandle) } } } }
func main() { var err error // connect to the database dbconn, err = sql.Open("postgres", PACKAGEBUG_DB) if err != nil { log.Fatal(err) } // make sure the database up err = dbconn.Ping() if err != nil { log.Fatal(err) } // set up aws SDK credentials & config cred := credentials.NewEnvCredentials() _, err = cred.Get() if err != nil { log.Fatal(err) } config := aws.NewConfig() config.Credentials = cred config.Endpoint = &PACKAGEBUG_SQS_ENDPOINT config.Region = &PACKAGEBUG_SQS_REGION sqsconn = sqs.New(config) // dispatch jobs once a day for { <-time.After(24 * time.Hour) go dispatchJobs() } }
// LifecycleEventQueueURL inspects the current autoscaling group and returns // the URL of the first suitable lifecycle hook queue. func (s *Cluster) LifecycleEventQueueURL() (string, error) { asg, err := s.AutoscalingGroup() if err != nil { return "", err } autoscalingSvc := autoscaling.New(s.AwsSession) resp, err := autoscalingSvc.DescribeLifecycleHooks(&autoscaling.DescribeLifecycleHooksInput{ AutoScalingGroupName: asg.AutoScalingGroupName, }) if err != nil { return "", err } sqsSvc := sqs.New(s.AwsSession) for _, hook := range resp.LifecycleHooks { if !strings.HasPrefix(*hook.NotificationTargetARN, "arn:aws:sqs:") { continue } arnParts := strings.Split(*hook.NotificationTargetARN, ":") queueName := arnParts[len(arnParts)-1] queueOwnerAWSAccountID := arnParts[len(arnParts)-2] resp, err := sqsSvc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: &queueName, QueueOwnerAWSAccountId: &queueOwnerAWSAccountID, }) if err != nil { return "", err } return *resp.QueueUrl, nil } return "", ErrLifecycleHookNotFound }
func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %v -queue-url <queue-url> [-region <region>] [-delete] [mesages...]\n", os.Args[0]) flag.PrintDefaults() } flag.Parse() // queue url is required if *queueURL == "" { fmt.Fprintln(os.Stderr, "-queue-url is required") os.Exit(1) } // create an SQS client object svc := sqs.New(session.New(&aws.Config{Region: region})) msgs := flag.Args() if len(msgs) > 0 { if err := sendMessage(svc, msgs...); err != nil { log.Fatal(err) } } count := int64(10) rmsgs, err := receiveMessage(svc, count) if err != nil { log.Fatal(err) } if *delete { if err := deleteMessage(svc, rmsgs...); err != nil { log.Fatal(err) } } }
func SQS_SendMessage(body string, mobileNumber string) { svc := sqs.New(&aws.Config{Region: amazonRegion}) params := &sqs.SendMessageInput{ MessageBody: aws.String(body), QueueURL: aws.String(amazonQueueURL), DelaySeconds: aws.Long(1), MessageAttributes: map[string]*sqs.MessageAttributeValue{ "MobileNumber": { DataType: aws.String("String"), StringValue: aws.String(mobileNumber), }, "CreatingTime": { DataType: aws.String("String"), StringValue: aws.String(time.Now().String()), }, }, } resp, err := svc.SendMessage(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr()) if reqErr, ok := err.(awserr.RequestFailure); ok { fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()) } } else { fmt.Println(err.Error()) } } fmt.Println(awsutil.StringValue(resp)) }
// NewCustomResourceProvisioner returns a new CustomResourceProvisioner with an // sqs client configured from config. func NewCustomResourceProvisioner(db *sql.DB, config client.ConfigProvider) *CustomResourceProvisioner { p := &CustomResourceProvisioner{ Provisioners: make(map[string]customresources.Provisioner), sendResponse: customresources.SendResponse, sqs: sqs.New(config), } p.add("Custom::InstancePort", &InstancePortsProvisioner{ ports: lb.NewDBPortAllocator(db), }) p.add("Custom::ECSService", &ECSServiceResource{ ecs: ecs.New(config), }) store := &dbEnvironmentStore{db} p.add("Custom::ECSEnvironment", newECSEnvironmentProvisioner(&ECSEnvironmentResource{ environmentStore: store, })) p.add("Custom::ECSTaskDefinition", newECSTaskDefinitionProvisioner(&ECSTaskDefinitionResource{ ecs: ecs.New(config), environmentStore: store, })) return p }
func ExampleSQS_GetQueueUrl() { sess, err := session.NewSession() if err != nil { fmt.Println("failed to create session,", err) return } svc := sqs.New(sess) params := &sqs.GetQueueUrlInput{ QueueName: aws.String("String"), // Required QueueOwnerAWSAccountId: aws.String("String"), } resp, err := svc.GetQueueUrl(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
// Return a singleton SQS service instance func SQS() *sqs.SQS { if sqsService == nil { if accessKey() != "" && secretKey() != "" && region() != "" { sqsService = sqs.New(session.New(&aws.Config{ Region: aws.String(region()), Credentials: credentials.NewStaticCredentials(accessKey(), secretKey(), ""), })) } else { log.Printf("no AWS environment variables found; defaulting to EC2 instance profile and region %s.\n", region()) sqsService = sqs.New(session.New(&aws.Config{ Region: aws.String(region()), })) } } return sqsService }
func ExampleSQS_SetQueueAttributes() { sess, err := session.NewSession() if err != nil { fmt.Println("failed to create session,", err) return } svc := sqs.New(sess) params := &sqs.SetQueueAttributesInput{ Attributes: map[string]*string{ // Required "Key": aws.String("String"), // Required // More values... }, QueueUrl: aws.String("String"), // Required } resp, err := svc.SetQueueAttributes(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
// Worker service which reads from an SQS queue pulls off job messages, processes // the jobs, and records the results. The service uses environment variables for // its configuration. // // Requires the following environment variables to be set. // // * WORKER_QUEUE_URL - The SQS queue URL where the service will read job messages // from. Job messages are created when S3 notifies the SQS queue that a file has // been uploaded to a particular bucket. // // * WORKER_RESULT_QUEUE_URL - The SQS queue URL where the job results will be // sent to. // // * WORKER_RESULT_TABLENAME - The name of the DynamoDB table result items should // be recorded to. // // Optionally the follow environment variables can be provided. // // * AWS_REGION - The AWS region the worker will use for signing and making all // requests to. This parameter is only optional if the service is running within // an EC2 instance. If not running in an EC2 instance AWS_REGION is required. // // * WORKER_MESSAGE_VISIBILITY - The ammount of time messges will be hidden in // the SQS job message queue from other services when a service reads that message. // Will also be used to extend the visibility timeout for long running jobs. // Defaults to 60s. // // * WORKER_COUNT - The number of workers in the worker pool. Defaults to the // number of virtual CPUs in the system. // func main() { doneCh := listenForSigInterrupt() cfg, err := getConfig() if err != nil { log.Println("Unable to get config", err) os.Exit(1) } sqsSvc := sqs.New(nil) queue := NewJobMessageQueue(cfg.WorkerQueueURL, cfg.MessageVisibilityTimeout, 5, sqsSvc) go queue.Listen(doneCh) // Job Workers resultsCh := make(chan *wordfreq.JobResult, 10) workers := NewWorkerPool(cfg.NumWorkers, resultsCh, queue, s3.New(nil)) // Notifier to notify a Amazon SNS Topic notify := NewResultNotifier(sqsSvc, cfg.ResultQueueURL) // Recorder to write results to Amazon DynamoDB recorder := NewResultRecorder(cfg.ResultTableName, dynamodb.New(nil)) // Job Progress Collector collector := NewResultCollector(notify, recorder, queue) go collector.ProcessJobResult(resultsCh) // Wait for the workers to complete before continuing on to exit workers.WaitForWorkersDone() close(resultsCh) // Wait for all results to be completed before continuing collector.WaitForResults() }
func main() { qNames := &queuesNames{} endPoint := flag.String("endpoint", "", "IP:PORT of SQS service") region := flag.String("region", "", "SQS AWS region") ssl := flag.Bool("ssl", false, "Use SSL, false by default") flag.Var(qNames, "queues", "The queues' names to create, provide one or a comma separated list of them") flag.Parse() if len(*endPoint) == 0 { log.Fatal("-endpoint parameters is required") } if len(*region) == 0 { log.Fatal("-region parameters is required") } if qNames == nil { log.Fatal("-queues parameter is required") } svc := sqs.New(session.New(aws.NewConfig().WithDisableSSL(!*ssl).WithEndpoint(*endPoint).WithRegion(*region))) for _, qn := range qNames.names { log.Println("Creating queue", qn) createQueue(svc, qn) } }
func main() { // Load Config Path config, err := LoadConfig("config.yml") // If error, panic if err != nil { panic(err) } queue_url := config.getUrl() message_attr := config.getAttributes() // Need to dereference the region pointer to pass to &aws.Config region := *config.getRegion() // Create new sqs queue Object with Config Supplied queue := sqs.New(&aws.Config{Region: region}) // Retrieve a message from the queue message, err := getMessage(queue, queue_url, message_attr) exportMessage(message, config) if err != nil { catchError(err) } }
func ExampleSQS_ReceiveMessage() { svc := sqs.New(session.New()) params := &sqs.ReceiveMessageInput{ QueueUrl: aws.String("String"), // Required AttributeNames: []*string{ aws.String("QueueAttributeName"), // Required // More values... }, MaxNumberOfMessages: aws.Int64(1), MessageAttributeNames: []*string{ aws.String("MessageAttributeName"), // Required // More values... }, VisibilityTimeout: aws.Int64(1), WaitTimeSeconds: aws.Int64(1), } resp, err := svc.ReceiveMessage(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func (q *queuePoller) drainMessages(ctx context.Context) error { service := sqs.New(q.cfg) for { select { case <-ctx.Done(): return ctx.Err() case <-q.closeSignal: return nil default: } msg := sqs.ReceiveMessageInput{ QueueUrl: &q.queueURL, WaitTimeSeconds: &q.waitTimeSeconds, } if q.visibilityTimeout != 0 { msg.VisibilityTimeout = &q.visibilityTimeout } resp, err := service.ReceiveMessage(&msg) if err != nil { return wraperr(err, "cannot receive messagges from queue") } if err := q.forwardMsgs(ctx, resp); err != nil { return err } } }
func ExampleSQS_GetQueueURL() { svc := sqs.New(nil) params := &sqs.GetQueueURLInput{ QueueName: aws.String("String"), // Required QueueOwnerAWSAccountID: aws.String("String"), } resp, err := svc.GetQueueURL(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { // Generic AWS Error with Code, Message, and original error (if any) fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr()) if reqErr, ok := err.(awserr.RequestFailure); ok { // A service error occurred fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()) } } else { // This case should never be hit, the SDK should always return an // error which satisfies the awserr.Error interface. fmt.Println(err.Error()) } } // Pretty-print the response data. fmt.Println(awsutil.StringValue(resp)) }
func (infra *Infrastructure) removeSQSQueue() { svc := sqs.New(session.New(), infra.config) svc.DeleteQueue(&sqs.DeleteQueueInput{ QueueUrl: aws.String(infra.queueURL), }) }
func ExampleSQS_ChangeMessageVisibilityBatch() { svc := sqs.New(nil) params := &sqs.ChangeMessageVisibilityBatchInput{ Entries: []*sqs.ChangeMessageVisibilityBatchRequestEntry{ // Required { // Required ID: aws.String("String"), // Required ReceiptHandle: aws.String("String"), // Required VisibilityTimeout: aws.Long(1), }, // More values... }, QueueURL: aws.String("String"), // Required } resp, err := svc.ChangeMessageVisibilityBatch(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { // Generic AWS Error with Code, Message, and original error (if any) fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr()) if reqErr, ok := err.(awserr.RequestFailure); ok { // A service error occurred fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID()) } } else { // This case should never be hit, the SDK should always return an // error which satisfies the awserr.Error interface. fmt.Println(err.Error()) } } // Pretty-print the response data. fmt.Println(awsutil.StringValue(resp)) }
func setPolicy(key, secret, region, queueARN, queueURL string, topicARNs []string) error { svc := sqs.New(getConfig(key, secret, region)) bs, err := json.Marshal(topicARNs) if err != nil { return fmt.Errorf("error while creating policy for SQS queue: %v", err) } policy := fmt.Sprintf(policyFormat, queueARN, string(bs)) params := &sqs.SetQueueAttributesInput{ Attributes: map[string]*string{ "Policy": aws.String(policy), }, QueueUrl: aws.String(queueURL), } _, err = svc.SetQueueAttributes(params) if awserr, ok := err.(awserr.Error); ok { return fmt.Errorf("aws error while setting policy for SQS queue: %v %v", awserr.Code(), awserr.Message()) } else if err != nil { return fmt.Errorf("error while setting policy for SQS queue: %v", err) } return nil }