Skip to content

Commit 1e85866

Browse files
authored
test(taskbroker): Add CLI command for sending taskbroker tasks (#81319)
### Overview This PR is responsible for adding a CLI tool for sending tasks to the taskbroker to assist with iterative local development. Refs: #81240 ### Testing ``` sentry run taskbroker-send-tasks --task-function-path sentry.taskworker.tasks.example.say_hello --args '["foobar"]' --repeat 2 ```
1 parent 168f818 commit 1e85866

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/sentry/runner/commands/run.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,53 @@ def taskworker(rpc_host: str, max_task_count: int, **options: Any) -> None:
253253
raise SystemExit(exitcode)
254254

255255

256+
@run.command()
257+
@log_options()
258+
@configuration
259+
@click.option(
260+
"--repeat",
261+
type=int,
262+
help="Number of messages to send to the kafka topic",
263+
default=1,
264+
show_default=True,
265+
)
266+
@click.option(
267+
"--kwargs",
268+
type=str,
269+
help="Task function keyword arguments",
270+
)
271+
@click.option(
272+
"--args",
273+
type=str,
274+
help="Task function arguments",
275+
)
276+
@click.option(
277+
"--task-function-path",
278+
type=str,
279+
help="The path to the function name of the task to execute",
280+
required=True,
281+
)
282+
def taskbroker_send_tasks(
283+
task_function_path: str,
284+
args: str,
285+
kwargs: str,
286+
repeat: int,
287+
) -> None:
288+
from sentry.utils.imports import import_string
289+
290+
try:
291+
func = import_string(task_function_path)
292+
except Exception as e:
293+
click.echo(f"Error: {e}")
294+
raise click.Abort()
295+
task_args = [] if not args else eval(args)
296+
task_kwargs = {} if not kwargs else eval(kwargs)
297+
298+
for _ in range(repeat):
299+
func.delay(*task_args, **task_kwargs)
300+
click.echo(message=f"Successfully sent {repeat} messages.")
301+
302+
256303
@run.command()
257304
@click.option(
258305
"--pidfile",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
5+
from sentry.taskworker.registry import taskregistry
6+
7+
logger = logging.getLogger(__name__)
8+
exampletasks = taskregistry.create_namespace(name="examples")
9+
10+
11+
@exampletasks.register(name="examples.say_hello")
12+
def say_hello(name: str) -> None:
13+
print(f"Hello {name}") # noqa

0 commit comments

Comments
 (0)