Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit 965bc5f

Browse files
committedMay 14, 2018
Add a --no-send flag for testing
Add a flag that stops the program from draining queues or sending ping data. Useful for testing how a queue's data is processed without destroying the queue messages or sending bad or duplicate data to telemetry.mozilla.org.
1 parent 0430857 commit 965bc5f

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed
 

‎committelemetry/pulse.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import logging
1010
from contextlib import closing
11+
from functools import partial
1112

1213
import requests
1314
from kombu import Connection, Exchange, Queue
@@ -18,6 +19,10 @@
1819
log = logging.getLogger(__name__)
1920

2021

22+
def noop(*args, **kwargs):
23+
return None
24+
25+
2126
def changesets_for_pushid(pushid, push_json_url):
2227
"""Return a list of changeset IDs in a repository push.
2328
@@ -45,7 +50,7 @@ def changesets_for_pushid(pushid, push_json_url):
4550
return changesets
4651

4752

48-
def process_push_message(body, message):
53+
def process_push_message(body, message, no_send=False):
4954
"""Process a hg push message from Mozilla Pulse.
5055
5156
The message body structure is described by https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/notifications.html#common-properties-of-notifications
@@ -55,7 +60,10 @@ def process_push_message(body, message):
5560
Args:
5661
body: The decoded JSON message body as a Python dict.
5762
message: A AMQP Message object.
63+
no_send: Do not send any ping data or drain any queues.
5864
"""
65+
ack = noop if no_send else message.ack
66+
5967
log.debug(f'received message: {message}')
6068

6169
payload = body['payload']
@@ -64,7 +72,7 @@ def process_push_message(body, message):
6472
msgtype = payload['type']
6573
if msgtype != 'changegroup.1':
6674
log.info(f'skipped message of type {msgtype}')
67-
message.ack()
75+
ack()
6876
return
6977

7078
pushlog_pushes = payload['data']['pushlog_pushes']
@@ -73,7 +81,7 @@ def process_push_message(body, message):
7381
pcount = len(pushlog_pushes)
7482
if pcount == 0:
7583
log.info(f'skipped message with zero pushes')
76-
message.ack()
84+
ack()
7785
return
7886
elif pcount > 1:
7987
# Raise this as a warning to draw attention. According to
@@ -83,7 +91,7 @@ def process_push_message(body, message):
8391
log.warning(
8492
f'skipped invalid message with multiple pushes (expected 0 or 1, got {pcount})'
8593
)
86-
message.ack()
94+
ack()
8795
return
8896

8997
pushdata = pushlog_pushes.pop()
@@ -96,14 +104,18 @@ def process_push_message(body, message):
96104
log.info(f'processing changeset {changeset}')
97105
ping = payload_for_changeset(changeset, repo_url)
98106

107+
if no_send:
108+
log.info(f'ping data (not sent): {ping}')
109+
continue
110+
99111
# Pings need a unique ID so they can be de-duplicated by the ingestion
100112
# service. We can use the changeset ID for the unique key.
101113
send_ping(changeset, ping)
102114

103-
message.ack()
115+
ack()
104116

105117

106-
def run_pulse_listener(username, password, timeout):
118+
def run_pulse_listener(username, password, timeout, no_send):
107119
"""Run a Pulse message queue listener.
108120
109121
This function does not return.
@@ -149,10 +161,17 @@ def run_pulse_listener(username, password, timeout):
149161
queue.queue_declare()
150162
queue.queue_bind()
151163

164+
callback = partial(process_push_message, no_send=no_send)
165+
152166
# Pass auto_declare=False so that Consumer does not try to declare the
153167
# exchange. Declaring exchanges is not allowed by the Pulse server.
154168
with connection.Consumer(
155-
queue, callbacks=[process_push_message], auto_declare=False
169+
queue, callbacks=[callback], auto_declare=False
156170
) as consumer:
171+
172+
if no_send:
173+
log.info('transmission of ping data has been disabled')
174+
log.info('message acks has been disabled')
175+
157176
log.info('reading messages')
158177
connection.drain_events(timeout=timeout)

‎committelemetry/tool.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ def dump_telemetry(debug, target_repo, node_id):
6767
default=1.0,
6868
help='Timeout, in seconds, to wait for additional queue messages.'
6969
)
70-
def process_queue_messages(debug, user, password, timeout):
70+
@click.option(
71+
'--no-send',
72+
is_flag=True,
73+
help='For testing. Do not send ping data or drain any queues.'
74+
)
75+
def process_queue_messages(debug, user, password, timeout, no_send):
7176
"""Process all queued mercurial repo change messages."""
7277
if debug:
7378
log_level = logging.DEBUG
@@ -76,4 +81,4 @@ def process_queue_messages(debug, user, password, timeout):
7681

7782
logging.basicConfig(stream=sys.stdout, level=log_level)
7883

79-
run_pulse_listener(user, password, timeout)
84+
run_pulse_listener(user, password, timeout, no_send)

0 commit comments

Comments
 (0)
Failed to load comments.