示例#1
0
文件: main.go 项目: SalmonMode/ygor
// Send is used to send message to ygord. TODO: replace this by an sqschan.
func Send(message string) error {
	log.Printf("send to ygord: %s", message)
	if cfg.TestMode {
		return nil
	}

	if cfg.YgordQueueName == "" {
		return nil
	}

	client, err := sqs.NewClient(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey,
		cfg.AWSRegionCode)
	if err != nil {
		return err
	}

	url, err := client.GetQueueURL(cfg.YgordQueueName)
	if err != nil {
		return err
	}

	err = client.SendMessage(url, sqs.SQSEncode(message))
	if err != nil {
		return err
	}

	return nil
}
示例#2
0
文件: io_core.go 项目: dglloyd/ygor
// StartAdapters starts all the IO adapters (IRC, Stdin/Stdout, Minions, API)
func StartAdapters() (<-chan error, <-chan error, error) {
	err := StartHTTPAdapter()
	if err != nil {
		return nil, nil, errors.New("error starting http adapter: " +
			err.Error())
	}

	if cfg.TestMode {
		return StartStdioHandler()
	}

	client, err := sqs.NewClient(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey,
		cfg.AWSRegionCode)
	if err != nil {
		return nil, nil, err
	}

	ircerrch, err := StartIRCAdapter(client)
	if err != nil {
		return nil, nil, errors.New("error starting IRC adapter: " +
			err.Error())
	}

	minionerrch, err := StartMinionAdapter(client)
	if err != nil {
		return nil, nil, errors.New("error starting minion adapter: " +
			err.Error())
	}

	return ircerrch, minionerrch, nil
}
示例#3
0
// SendToChannelMinions sends a message to all the minions of the given
// channel.
func SendToChannelMinions(channel, msg string) {
	client, err := sqs.NewClient(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey,
		cfg.AWSRegionCode)
	if err != nil {
		Debug("error: " + err.Error())
		return
	}

	channelCfg, exists := cfg.Channels[channel]
	if !exists {
		Debug("error: " + channel + " has no queue(s) configured")
		return
	}

	urls, err := channelCfg.GetQueueURLs()
	if err != nil {
		Debug("error: unable to load queue URLs, " + err.Error())
		return
	}

	// Send the same exact data to all this channel's minion.
	for _, url := range urls {
		if cfg.TestMode {
			fmt.Printf("[SQS-SendToMinion] %s %s\n", url, msg)
			continue
		}

		err := client.SendMessage(url, sqs.SQSEncode(msg))
		if err != nil {
			Debug("error sending to minion: " + err.Error())
			continue
		}
	}
}
示例#4
0
文件: main.go 项目: SalmonMode/ygor
// startReceivingFromSQS is the call used in a production system to start
// receiving messages. It is not used in test.
func startReceivingFromSQS(incoming chan *sqs.Message) error {
	client, err := sqs.NewClient(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey,
		cfg.AWSRegionCode)
	if err != nil {
		return err
	}

	attrs := sqs.CreateQueueAttributes{
		MaximumMessageSize:            4096,
		ReceiveMessageWaitTimeSeconds: 20,
		VisibilityTimeout:             10,
		MessageRetentionPeriod:        300,
	}

	queueURL, err := client.CreateQueueWithAttributes(cfg.QueueName, attrs)
	if err != nil {
		log.Fatal(err)
	}

	err = Register(cfg.Name, queueURL)
	if err != nil {
		return errors.New("registration failed: " + err.Error())
	}

	ch, errch, err := sqschan.IncomingFromURL(client, queueURL)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for {
			select {
			case err = <-errch:
				log.Printf("error reading sqs message: %s", err.Error())
			case msg := <-ch:
				incoming <- msg
				err = client.DeleteMessage(msg)
				if err != nil {
					log.Printf("error deleting sqs message: %s", err.Error())
				}
			}
		}
	}()

	return nil
}
示例#5
0
// SendToQueue sends a message to our friendly minion via its SQS queue.
func SendToQueue(queueURL, msg string) error {
	if cfg.TestMode {
		fmt.Printf("[SQS-SendToMinion] %s %s\n", queueURL, msg)
		return nil
	}

	client, err := sqs.NewClient(cfg.AWSAccessKeyID, cfg.AWSSecretAccessKey,
		cfg.AWSRegionCode)
	if err != nil {
		return err
	}

	err = client.SendMessage(queueURL, msg)
	if err != nil {
		Debug("error sending to minion: " + err.Error())
	}

	return nil
}
示例#6
0
文件: server.go 项目: SalmonMode/ygor
func (srv *Server) GetSQSClient() (*sqs.Client, error) {
	return sqs.NewClient(srv.Config.AWSAccessKeyID, srv.Config.AWSSecretAccessKey,
		srv.Config.AWSRegionCode)
}