Skip to content

Commit

Permalink
Add Job.__repr__()
Browse files Browse the repository at this point in the history
This is gonna be useful for monitoring and debugging when jobs appear in
log messages and tracebacks, Sentry issues, etc.
  • Loading branch information
seanh committed May 8, 2024
1 parent 92499da commit 8c58d05
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions h/models/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,20 @@ class Job(Base):
server_default=text("'{}'::jsonb"),
nullable=False,
)

def __repr__(self):
class_name = type(self).__name__
attrs = {
attrname: repr(getattr(self, attrname))
for attrname in [
"id",
"name",
"enqueued_at",
"scheduled_at",
"expires_at",
"priority",
"tag",
"kwargs",
]
}
return f"{class_name}({', '.join(f'{name}={value}' for name, value in attrs.items())})"
43 changes: 43 additions & 0 deletions tests/unit/h/models/job_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from datetime import datetime

from h.models.job import Job


class TestJob:
def test___repr__(self):
job = Job(
id=42,
name="job_name",
enqueued_at=datetime(
year=2024,
month=5,
day=8,
hour=11,
minute=51,
second=23,
),
scheduled_at=datetime(
year=2024,
month=6,
day=1,
hour=0,
minute=0,
second=0,
),
expires_at=datetime(
year=2025,
month=1,
day=1,
hour=0,
minute=0,
second=0,
),
priority=3,
tag="job_tag",
kwargs={"foo": "FOO", "bar": "BAR"},
)

assert (
repr(job)
== "Job(id=42, name='job_name', enqueued_at=datetime.datetime(2024, 5, 8, 11, 51, 23), scheduled_at=datetime.datetime(2024, 6, 1, 0, 0), expires_at=datetime.datetime(2025, 1, 1, 0, 0), priority=3, tag='job_tag', kwargs={'foo': 'FOO', 'bar': 'BAR'})"
)

0 comments on commit 8c58d05

Please sign in to comment.