diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 4e52ecd..0780869 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -61,6 +61,10 @@ jobs: - name: Test with pytest run: poetry run pytest + # The lock file has RQ 3; also run the tests against RQ 2. + - name: Test with pytest (RQ 2) + run: poetry run pip install "rq>=2,<3" && poetry run pytest + - name: Test django sample app run: cd ./sample-apps/django_sample && poetry install --all-extras --no-interaction && bin/test diff --git a/judoscale/rq/collector.py b/judoscale/rq/collector.py index 3a310ab..c168cc6 100644 --- a/judoscale/rq/collector.py +++ b/judoscale/rq/collector.py @@ -69,7 +69,7 @@ def _busy_jobs(self, queue: Queue) -> Optional[Tuple[int, Optional[float]]]: entries = self.redis.zrangebyscore(registry.key, f"({time.time()}", "+inf") pipeline = self.redis.pipeline() for entry in entries: - # RQ 2 stores `:` entries, RQ 1 job IDs. + # RQ 2+ stores `:` entries, RQ 1 job IDs. job_id = entry.decode().split(":")[0] pipeline.hget(Job.key_for(job_id), "started_at") # RQ 1 parses to a naive datetime, though the time is in UTC. @@ -111,11 +111,10 @@ def collect(self) -> List[Metric]: if job := self.oldest_job(queue): if job.enqueued_at is not None: - # RQ stores `enqueued_at` as a naive datetime object, which - # means it doesn't have any timezone information associated - # with it. - # But since the time is, in fact, in UTC, we can just replace - # the timezone with UTC and then convert it to a timestamp. + # `enqueued_at` is in UTC, but RQ before 2.2 returns it as a + # naive datetime, so set the timezone to UTC before converting + # it to a timestamp. RQ 2.2+ already returns it in UTC, so this + # leaves it unchanged. # https://github.com/rq/rq/blob/42ac7d4150951b5f5fba8be11573920c1e6f6e69/rq/queue.py#L1003 # enqueued_at = job.enqueued_at.replace( diff --git a/poetry.lock b/poetry.lock index d2bd20f..098b0f0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -348,6 +348,20 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "croniter" +version = "7.0.0" +description = "croniter provides iteration for datetime object with cron like format" +optional = true +python-versions = ">=3.9" +files = [ + {file = "croniter-7.0.0-py3-none-any.whl", hash = "sha256:92090bce3cef195a037a46b78430c4cdd6250e90671d9d7dcd9e486147daaca4"}, + {file = "croniter-7.0.0.tar.gz", hash = "sha256:a0b2568303c774f6a4ea2d4ce2e72b1ee911029c7070d64a1befe05cdb107f5a"}, +] + +[package.dependencies] +python-dateutil = "*" + [[package]] name = "django" version = "4.2.24" @@ -865,18 +879,19 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] [[package]] name = "rq" -version = "2.3.3" +version = "3.0.1" description = "RQ is a simple, lightweight, library for creating background jobs, and processing them." optional = true -python-versions = ">=3.8" +python-versions = ">=3.10" files = [ - {file = "rq-2.3.3-py3-none-any.whl", hash = "sha256:2202c4409c4c527ac4bee409867d6c02515dd110030499eb0de54c7374aee0ce"}, - {file = "rq-2.3.3.tar.gz", hash = "sha256:20c41c977b6f27c852a41bd855893717402bae7b8d9607dca21fe9dd55453e22"}, + {file = "rq-3.0.1-py3-none-any.whl", hash = "sha256:e56109f4354ab4ae035d20fe1c2c5769c60bf39873f426de509e82c01f64ef44"}, + {file = "rq-3.0.1.tar.gz", hash = "sha256:2a030c133ccbe71908d04f1df10e97ac2a3d482d5e0f382a0c12deb30e5ba3ec"}, ] [package.dependencies] click = ">=5" -redis = ">=3.5,<6.0.0 || >6.0.0" +croniter = "*" +redis = ">=5.0.1" [[package]] name = "six" @@ -1034,4 +1049,4 @@ rq = ["rq"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "cd9a27288e1a14ff6d12c845b35ba85ac1254947f8bc99a8d24b32d1f8a95a17" +content-hash = "77d0780dac9a1137e89885a6482818ddfec996bad4177f6436a552807a252fdc" diff --git a/pyproject.toml b/pyproject.toml index f7977d4..4d0d5a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ django = { version = ">=2.1.0,<7.0.0", optional = true } flask = { version = ">=1.1.0,<4.0.0", optional = true } celery = { version = ">=4.4.0,<6.0.0", extras = ["redis"], optional = true } dramatiq = { version = ">=1.13.0,<3.0.0", extras = ["redis"], optional = true } -rq = { version = ">=1.0.0,<3.0.0", optional = true } +rq = { version = ">=1.0.0,<4.0.0", optional = true } [tool.poetry.group.dev.dependencies] black = "^26.5.1" diff --git a/sample-apps/flask_rq_sample/poetry.lock b/sample-apps/flask_rq_sample/poetry.lock index 135c1dd..874c318 100644 --- a/sample-apps/flask_rq_sample/poetry.lock +++ b/sample-apps/flask_rq_sample/poetry.lock @@ -2,13 +2,13 @@ [[package]] name = "async-timeout" -version = "4.0.2" +version = "5.0.1" description = "Timeout context manager for asyncio programs" optional = false -python-versions = ">=3.6" +python-versions = ">=3.8" files = [ - {file = "async-timeout-4.0.2.tar.gz", hash = "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15"}, - {file = "async_timeout-4.0.2-py3-none-any.whl", hash = "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"}, + {file = "async_timeout-5.0.1-py3-none-any.whl", hash = "sha256:39e3809566ff85354557ec2398b55e096c8364bacac9405a7a1fa429e77fe76c"}, + {file = "async_timeout-5.0.1.tar.gz", hash = "sha256:d9321a7a3d5a6a5e187e824d2fa0793ce379a202935782d555d6e9d2735677d3"}, ] [[package]] @@ -155,6 +155,20 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "croniter" +version = "7.0.0" +description = "croniter provides iteration for datetime object with cron like format" +optional = false +python-versions = ">=3.9" +files = [ + {file = "croniter-7.0.0-py3-none-any.whl", hash = "sha256:92090bce3cef195a037a46b78430c4cdd6250e90671d9d7dcd9e486147daaca4"}, + {file = "croniter-7.0.0.tar.gz", hash = "sha256:a0b2568303c774f6a4ea2d4ce2e72b1ee911029c7070d64a1befe05cdb107f5a"}, +] + +[package.dependencies] +python-dateutil = "*" + [[package]] name = "flask" version = "2.3.3" @@ -238,7 +252,7 @@ i18n = ["Babel (>=2.7)"] [[package]] name = "judoscale" -version = "1.11.0" +version = "1.17.0" description = "Official Python package for the Judoscale autoscaler" optional = false python-versions = "^3.10" @@ -248,14 +262,15 @@ develop = true [package.dependencies] flask = {version = ">=1.1.0,<4.0.0", optional = true} requests = "<3.0.0" -rq = {version = ">=1.0.0,<3.0.0", optional = true} +rq = {version = ">=1.0.0,<4.0.0", optional = true} [package.extras] asgi = [] celery-redis = ["celery[redis] (>=4.4.0,<6.0.0)"] django = ["django (>=2.1.0,<7.0.0)"] +dramatiq-redis = ["dramatiq[redis] (>=1.13.0,<3.0.0)"] flask = ["flask (>=1.1.0,<4.0.0)"] -rq = ["rq (>=1.0.0,<3.0.0)"] +rq = ["rq (>=1.0.0,<4.0.0)"] [package.source] type = "directory" @@ -320,23 +335,42 @@ files = [ {file = "MarkupSafe-2.1.2.tar.gz", hash = "sha256:abcabc8c2b26036d62d4c746381a6f7cf60aafcc653198ad678306986b09450d"}, ] +[[package]] +name = "python-dateutil" +version = "3.0.0" +description = "Extensions to the standard Python datetime module" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" +files = [ + {file = "python_dateutil-3.0.0-py2.py3-none-any.whl", hash = "sha256:3c79630202e4152c6add678446984236efe4f57e84d8e892aab0637bab82e9fd"}, + {file = "python_dateutil-3.0.0.tar.gz", hash = "sha256:7ebcb3c29a92f3ceafe79a7a48a5c45677e9e004bbeec5742ac0bbb00560b7a2"}, +] + +[package.dependencies] +six = ">=1.6" +tzdata = "*" + [[package]] name = "redis" -version = "4.5.1" +version = "8.1.0" description = "Python client for Redis database and key-value store" optional = false -python-versions = ">=3.7" +python-versions = ">=3.10" files = [ - {file = "redis-4.5.1-py3-none-any.whl", hash = "sha256:5deb072d26e67d2be1712603bfb7947ec3431fb0eec9c578994052e33035af6d"}, - {file = "redis-4.5.1.tar.gz", hash = "sha256:1eec3741cda408d3a5f84b78d089c8b8d895f21b3b050988351e925faf202864"}, + {file = "redis-8.1.0-py3-none-any.whl", hash = "sha256:a4fe1aac3d3b3cc791d4b3d5931c5a956045dc951ee74d1c913ee3ac4d2ee9fb"}, + {file = "redis-8.1.0.tar.gz", hash = "sha256:6e1a19beef9225c83efd689c7e6b7da2d5215b1f42cd13b7fc3714d0a09c7b25"}, ] [package.dependencies] -async-timeout = ">=4.0.2" +async-timeout = {version = ">=4.0.3", markers = "python_full_version < \"3.11.3\""} [package.extras] -hiredis = ["hiredis (>=1.0.0)"] -ocsp = ["cryptography (>=36.0.1)", "pyopenssl (==20.0.1)", "requests (>=2.26.0)"] +circuit-breaker = ["pybreaker (>=1.4.0)"] +hiredis = ["hiredis (>=3.2.0)"] +jwt = ["pyjwt (>=2.13.0)"] +ocsp = ["cryptography (>=36.0.1)", "pyopenssl (>=20.0.1)", "requests (>=2.31.0)"] +otel = ["opentelemetry-api (>=1.39.1)", "opentelemetry-exporter-otlp-proto-http (>=1.39.1)", "opentelemetry-sdk (>=1.39.1)"] +xxhash = ["xxhash (>=3.6.0,<3.7.0)"] [[package]] name = "requests" @@ -361,18 +395,19 @@ use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] [[package]] name = "rq" -version = "1.13.0" +version = "3.0.1" description = "RQ is a simple, lightweight, library for creating background jobs, and processing them." optional = false -python-versions = ">=3.6" +python-versions = ">=3.10" files = [ - {file = "rq-1.13.0-py2.py3-none-any.whl", hash = "sha256:621966d7cbf96d5609557a4bd3fd77f749d6d10997d2e353a3e89a14e08eea16"}, - {file = "rq-1.13.0.tar.gz", hash = "sha256:5bb0380a17597200520731686766bb72faf16ebffb602663560d91ea2c9e7103"}, + {file = "rq-3.0.1-py3-none-any.whl", hash = "sha256:e56109f4354ab4ae035d20fe1c2c5769c60bf39873f426de509e82c01f64ef44"}, + {file = "rq-3.0.1.tar.gz", hash = "sha256:2a030c133ccbe71908d04f1df10e97ac2a3d482d5e0f382a0c12deb30e5ba3ec"}, ] [package.dependencies] -click = ">=5.0.0" -redis = ">=3.5.0" +click = ">=5" +croniter = "*" +redis = ">=5.0.1" [[package]] name = "setuptools" @@ -390,6 +425,28 @@ docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "pygments-g testing = ["build[virtualenv]", "filelock (>=3.4.0)", "flake8 (<5)", "flake8-2020", "ini2toml[lite] (>=0.9)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pip (>=19.1)", "pip-run (>=8.8)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf", "pytest-timeout", "pytest-xdist", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel"] testing-integration = ["build[virtualenv]", "filelock (>=3.4.0)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.2.0)", "pytest", "pytest-enabler", "pytest-xdist", "tomli", "virtualenv (>=13.0.0)", "wheel"] +[[package]] +name = "six" +version = "1.17.0" +description = "Python 2 and 3 compatibility utilities" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" +files = [ + {file = "six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274"}, + {file = "six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81"}, +] + +[[package]] +name = "tzdata" +version = "2026.6" +description = "Provider of IANA time zone data" +optional = false +python-versions = ">=2" +files = [ + {file = "tzdata-2026.6-py2.py3-none-any.whl", hash = "sha256:0a10eec6e13ef16b627fabed9a5f2ba61a62b1c67f327305f4e7e412a70b4aec"}, + {file = "tzdata-2026.6.tar.gz", hash = "sha256:85d2cd9c09f81f0bb29db27759fc83af4f1e620efb814f8c5bf8b4cc4feece6d"}, +] + [[package]] name = "urllib3" version = "1.26.14" @@ -426,4 +483,4 @@ watchdog = ["watchdog (>=2.3)"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "a9fae9742aaf738f09baad260df3b7c43081f9a7449033f711ee44706ba8cba9" +content-hash = "50d751c296d07ac7b5c8d8f1d18cf5c78c49b5445168fe427b3b6a54724333a1" diff --git a/sample-apps/flask_rq_sample/pyproject.toml b/sample-apps/flask_rq_sample/pyproject.toml index e2e9cd3..4ffbadf 100644 --- a/sample-apps/flask_rq_sample/pyproject.toml +++ b/sample-apps/flask_rq_sample/pyproject.toml @@ -9,7 +9,7 @@ readme = "README.md" [tool.poetry.dependencies] python = "^3.10" Flask = "^2.2.3" -rq = "^1.13.0" +rq = "^3.0.0" gunicorn = "^20.1.0" judoscale = { path = "../../", extras=["rq", "flask"], develop = true } diff --git a/tests/test_collectors.py b/tests/test_collectors.py index 00e02f3..2203e90 100644 --- a/tests/test_collectors.py +++ b/tests/test_collectors.py @@ -7,6 +7,7 @@ import redis.exceptions import rq.utils as rq_utils from pytest import approx, fixture, raises +from rq.job import Job from judoscale.celery.collector import CeleryMetricsCollector from judoscale.core.metric import Metric @@ -931,9 +932,9 @@ def test_collect_with_busy_job_tracking(self, worker_1): redis.zremrangebyscore.assert_not_called() hgets = redis.pipeline.return_value.hget.call_args_list assert [c.args for c in hgets] == [ - (b"rq:job:job1", "started_at"), - (b"rq:job:job2", "started_at"), - (b"rq:job:job3", "started_at"), + (Job.key_for("job1"), "started_at"), + (Job.key_for("job2"), "started_at"), + (Job.key_for("job3"), "started_at"), ] assert metrics[2].measurement == "qt"