Repository: getsentry/sentry
Pull request: feat(cli): Prevent `run worker` from binding to queues not declared
Fixes #7856.

Problem statement:
Assert that --queues argument(s) actually exist in codebase before starting celeryd.
Currently, if someone runs a celery worker with the argument:`--queues no-way-this-queue-actually-exists-yet`  RabbitMQ will create a queue with the default options, which will cause any future production to the queue to error if there is a configuration mismatch.

Hints from issue discussion:
Not sure this is still important for prod but I think it can also help self-hosted so taking on.
It's still a "gotcha" for saas but we haven't been "got" in quite some time now. 🤞

Commit message: typos, typos everywhere

Patch to review:
diff --git a/src/sentry/runner/commands/run.py b/src/sentry/runner/commands/run.py
index ebf30080112e22..85908367df2bc8 100644
--- a/src/sentry/runner/commands/run.py
+++ b/src/sentry/runner/commands/run.py
@@ -211,10 +211,38 @@ def run_worker(**options):
 @click.option("--without-mingle", is_flag=True, default=False)
 @click.option("--without-heartbeat", is_flag=True, default=False)
 @click.option("--max-tasks-per-child", default=10000)
+@click.option("--ignore-unknown-queues", is_flag=True, default=False)
 @log_options()
 @configuration
-def worker(**options):
+def worker(ignore_unknown_queues, **options):
     """Run background worker instance and autoreload if necessary."""
+
+    from sentry.celery import app
+
+    known_queues = frozenset(c_queue.name for c_queue in app.conf.CELERY_QUEUES)
+
+    if options["queues"] is not None:
+        if not options["queues"].issubset(known_queues):
+            unknown_queues = options["queues"] - known_queues
+            message = "Following queues are not found: %s".format(unknown_queues)
+            if ignore_unknown_queues:
+                options["queues"] -= unknown_queues
+                click.echo(message)
+            else:
+                raise click.ClickException(message)
+
+    if options["exclude_queues"] is not None:
+        if not options["exclude_queues"].issubset(known_queues):
+            unknown_queues = options["exclude_queues"] - known_queues
+            message = "Following queues cannot be excluded as they don't exist: %s".format(
+                unknown_queues
+            )
+            if ignore_unknown_queues:
+                options["exclude_queues"] -= unknown_queues
+                click.echo(message)
+            else:
+                raise click.ClickException(message)
+
     if options["autoreload"]:
         from django.utils import autoreload
 


Code review comments:
diff_hunk: @@ -211,10 +211,38 @@ def run_worker(**options):
 @click.option("--without-mingle", is_flag=True, default=False)
 @click.option("--without-heartbeat", is_flag=True, default=False)
 @click.option("--max-tasks-per-child", default=10000)
+@click.option("--ignore-unknown-queues", is_flag=True, default=False)
 @log_options()
 @configuration
-def worker(**options):
+def worker(ignore_unknown_queues, **options):
     """Run background worker instance and autoreload if necessary."""
+
+    from sentry.celery import app
+
+    known_queues = frozenset(c_queue.name for c_queue in app.conf.CELERY_QUEUES)
+
+    if options["queues"] is not None:
+        if not options["queues"].issubset(known_queues):
+            unknown_queues = options["queues"] - known_queues
+            message = "Following queues are not found: %s".format(unknown_queues)
path: src/sentry/runner/commands/run.py
text: @author:
Bad string formatting.