Skip to content

Commit

Permalink
Make it possible to monitor multiple queues
Browse files Browse the repository at this point in the history
  • Loading branch information
msrb committed Nov 23, 2017
1 parent 0c6ba5d commit da232c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions openshift/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ parameters:
name: SLEEP_INTERVAL
value: "5"

- description: Name of the queue to monitor (without deployment prefix)
displayName: Queue name
- description: A comma-separated list of queues to monitor (without deployment prefix)
displayName: Queues to monitor
required: true
name: SQS_QUEUE_NAME
value: "ingestion_bayesianFlow_v0"
value: "ingestion_bayesianFlow_v0,ingestion_bayesianPackageFlow_v0"

- description: Name of the openshift project used for oc command
displayName: Openshift Project Name
Expand Down
2 changes: 1 addition & 1 deletion scale.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ while true; do
# get number of messages in the given queue
read msg_count replicas <<< $(./sqs_status.py -q ${queue_name})

echo "[$(date -u)] Number of messages in ${queue_name} is ${msg_count}. Replicas needed: ${replicas}."
echo "[$(date -u)] Number of messages in ${queue_name//,/+} is ${msg_count}. Replicas needed: ${replicas}."
set -x
oc -n ${OC_PROJECT} scale --replicas=${replicas} dc ${DC_NAME}
set +x
Expand Down
30 changes: 23 additions & 7 deletions sqs_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import sys
import math
import logging

import boto3

Expand All @@ -20,14 +21,29 @@
default_scale_coef = 100


def get_number_of_messages(queue_name):
"""Return approximate number of messages in given queue.
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger('sqs_status')

:param queue_name: name of the queue to check
:return: approximate number of messages in the given queue

def get_number_of_messages(queues_str):
"""Return approximate number of messages in given queue(s).
:param queues_str: a comma-separated list of queues to check
:return: approximate number of messages in given queues
"""
queue = sqs.get_queue_by_name(QueueName=queue_name)
return int(queue.attributes.get('ApproximateNumberOfMessages') or 0)

queues = [x.strip() for x in queues_str.split(',')]

total_count = 0
for queue_name in queues:
try:
queue = sqs.get_queue_by_name(QueueName=queue_name)
total_count += int(queue.attributes.get('ApproximateNumberOfMessages') or 0)
except Exception as e:
logger.warning('Unable to check queue: {q}'.format(q=queue_name), exc_info=True)
continue

return total_count


def get_number_of_replicas(msg_count):
Expand Down Expand Up @@ -62,6 +78,6 @@ def get_number_of_replicas(msg_count):
print(parser.format_usage())
sys.exit(1)

count = get_number_of_messages(queue_name=args.queue)
count = get_number_of_messages(queues_str=args.queue)
replicas = get_number_of_replicas(msg_count=count)
print('{count} {replicas}'.format(count=count, replicas=replicas))

0 comments on commit da232c5

Please sign in to comment.