-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93aa90f
commit a5e1367
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package deployment | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/leapfrogtechnology/shift/deployment/services/mq" | ||
"github.com/streadway/amqp" | ||
) | ||
|
||
func failOnError(err error, msg string) { | ||
if err != nil { | ||
panic(fmt.Sprintf("%s: %s", msg, err)) | ||
} | ||
} | ||
|
||
func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) { | ||
conn := mq.GetConnection() | ||
|
||
ch, err := conn.Channel() | ||
|
||
failOnError(err, "Failed to open a channel") | ||
|
||
q, err := ch.QueueDeclare("Deployment", | ||
false, // durable bool | ||
false, // autoDelete bool | ||
false, // exclusive bool | ||
false, // noWait bool | ||
nil, // args amqp.Table | ||
) | ||
|
||
failOnError(err, "Failed to declare a queue") | ||
|
||
return conn, ch, &q | ||
} | ||
|
||
// Publish message to queue | ||
func Publish(message []byte) { | ||
_, ch, q := getQueue() | ||
|
||
defer ch.Close() | ||
|
||
msg := amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: message, | ||
} | ||
|
||
ch.Publish("", q.Name, false, false, msg) | ||
} | ||
|
||
// Consume listens to message from queue | ||
func Consume(deploy func([]byte)) { | ||
_, ch, q := getQueue() | ||
|
||
defer ch.Close() | ||
|
||
msgs, err := ch.Consume( | ||
q.Name, // queue string | ||
"", // consumer string | ||
true, // autoAck bool | ||
false, // exclusive bool | ||
false, // noLocal bool | ||
false, // noWait bool | ||
nil, //args amqp.Table | ||
) | ||
|
||
failOnError(err, "Failed to register a consumer") | ||
|
||
for msg := range msgs { | ||
deploy(msg.Body) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package trigger | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/leapfrogtechnology/shift/deployment/services/mq" | ||
"github.com/streadway/amqp" | ||
) | ||
|
||
func failOnError(err error, msg string) { | ||
if err != nil { | ||
panic(fmt.Sprintf("%s: %s", msg, err)) | ||
} | ||
} | ||
|
||
func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) { | ||
conn := mq.GetConnection() | ||
|
||
ch, err := conn.Channel() | ||
|
||
failOnError(err, "Failed to open a channel") | ||
|
||
q, err := ch.QueueDeclare("Trigger", | ||
false, // durable bool | ||
false, // autoDelete bool | ||
false, // exclusive bool | ||
false, // noWait bool | ||
nil, // args amqp.Table | ||
) | ||
|
||
failOnError(err, "Failed to declare a queue") | ||
|
||
return conn, ch, &q | ||
} | ||
|
||
// Publish message to queue | ||
func Publish(message []byte) { | ||
_, ch, q := getQueue() | ||
|
||
defer ch.Close() | ||
|
||
msg := amqp.Publishing{ | ||
ContentType: "application/json", | ||
Body: message, | ||
} | ||
|
||
ch.Publish("", q.Name, false, false, msg) | ||
} | ||
|
||
// Consume listens to message from queue | ||
func Consume(deploy func([]byte)) { | ||
_, ch, q := getQueue() | ||
|
||
defer ch.Close() | ||
|
||
msgs, err := ch.Consume( | ||
q.Name, // queue string | ||
"", // consumer string | ||
true, // autoAck bool | ||
false, // exclusive bool | ||
false, // noLocal bool | ||
false, // noWait bool | ||
nil, //args amqp.Table | ||
) | ||
|
||
failOnError(err, "Failed to register a consumer") | ||
|
||
for msg := range msgs { | ||
deploy(msg.Body) | ||
} | ||
} |