-
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.
Signed-off-by: raihankhan <[email protected]>
- Loading branch information
1 parent
94f6f27
commit 2d68697
Showing
46 changed files
with
6,260 additions
and
29 deletions.
There are no files selected for viewing
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
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,105 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rabbitmq | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
amqp "github.com/rabbitmq/amqp091-go" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func (c *AMQPClient) GetMessagingChannel() *Channel { | ||
// Create a channel for sending and receiving messages | ||
messagingCh, err := c.Channel() | ||
if err != nil { | ||
klog.Error(err, "Failed to Open a Messaging Channel") | ||
return &Channel{nil} | ||
} | ||
return &Channel{messagingCh} | ||
} | ||
|
||
func (ch *Channel) GetNewQueue(name string, isDurable bool, isTypeQuoeum bool) (*amqp.Queue, error) { | ||
// Declare a non-persistent queue, where messages will be sent | ||
q, err := ch.QueueDeclare( | ||
name, // name | ||
isDurable, // durable | ||
false, // delete when unused | ||
false, // exclusive | ||
false, // no-wait | ||
nil, // arguments | ||
) | ||
if err != nil { | ||
klog.Error(err, "Failed to create a queue for publishing message") | ||
return nil, err | ||
} | ||
return &q, nil | ||
} | ||
|
||
func (ch *Channel) PublishMessageOnce(ctx context.Context, queueName string, message string) error { | ||
// Declare a non-persistent queue, where messages will be sent | ||
err := ch.PublishWithContext( | ||
ctx, | ||
"", // exchange | ||
queueName, // routing key | ||
false, // mandatory | ||
false, // immediate | ||
amqp.Publishing{ | ||
ContentType: "text/plain", | ||
Body: []byte(message), | ||
}) | ||
if err != nil { | ||
klog.Error(err, "Failed to publish message") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (ch *Channel) ConsumeMessageOnce(ctx context.Context, cancelFunc context.CancelFunc, queueName string) error { | ||
// start delivering messages through the channel, | ||
// a goroutine will be collecting messages with the context timeout | ||
deliveryCh, err := ch.ConsumeWithContext( | ||
ctx, | ||
queueName, // queue | ||
"", // consumer | ||
true, // auto-ack | ||
false, // exclusive | ||
false, // no-local | ||
false, // no-wait | ||
nil, // args | ||
) | ||
if err != nil { | ||
klog.Error(err, "Failed to consume message") | ||
return err | ||
} | ||
received := false | ||
go func() { | ||
for d := range deliveryCh { | ||
if d.Body != nil { | ||
received = true | ||
cancelFunc() | ||
return | ||
} | ||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
if !received { | ||
return errors.New("failed to consume message due to timeout") | ||
} | ||
return nil | ||
} |
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,44 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package rabbitmq | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func (c *HTTPClient) IsAllNodesRunningInCluster(replicas int) (bool, error) { | ||
nodes, err := c.ListNodes() | ||
if err != nil { | ||
klog.Error(err, "Failed to get node lists") | ||
return true, err | ||
} | ||
|
||
if len(nodes) < replicas { | ||
klog.Info(fmt.Sprintf("Cluster requires %v nodes but only %v nodes joined", replicas, len(nodes))) | ||
return false, nil | ||
} | ||
for _, node := range nodes { | ||
if !node.IsRunning { | ||
klog.Error(err, fmt.Sprintf("Node: %s is not running", node.Name)) | ||
return false, nil | ||
} | ||
} | ||
|
||
klog.Info("All required nodes running in cluster") | ||
return true, nil | ||
} |
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
27 changes: 27 additions & 0 deletions
27
vendor/github.com/michaelklishin/rabbit-hole/v2/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
vendor/github.com/michaelklishin/rabbit-hole/v2/.travis.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.