Skip to content

Commit

Permalink
Remove duplicate logs and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Feb 18, 2025
1 parent e20b675 commit cf0a1e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
10 changes: 4 additions & 6 deletions task_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def run_tasks(num_tasks: int = 1) -> typing.List[TaskRun]:
tasks = Task.objects.get_tasks_to_process(num_tasks)

if tasks:
logger.debug(f"Running {len(tasks)} tasks")
logger.debug(f"Running {len(tasks)} task(s)")

executed_tasks = []
task_runs = []
Expand All @@ -45,7 +45,7 @@ def run_tasks(num_tasks: int = 1) -> typing.List[TaskRun]:

if task_runs:
TaskRun.objects.bulk_create(task_runs)
logger.debug(f"Finished running {len(task_runs)} tasks")
logger.debug(f"Finished running {len(task_runs)} task(s)")

return task_runs

Expand All @@ -58,7 +58,7 @@ def run_recurring_tasks() -> typing.List[RecurringTaskRun]:
# a problem for now, but we should be mindful of this limitation
tasks = RecurringTask.objects.get_tasks_to_process()
if tasks:
logger.debug(f"Running {len(tasks)} recurring tasks")
logger.debug(f"Running {len(tasks)} recurring task(s)")

task_runs = []

Expand All @@ -85,7 +85,7 @@ def run_recurring_tasks() -> typing.List[RecurringTaskRun]:

if task_runs:
RecurringTaskRun.objects.bulk_create(task_runs)
logger.debug(f"Finished running {len(task_runs)} recurring tasks")
logger.debug(f"Finished running {len(task_runs)} recurring task(s)")

return task_runs

Expand Down Expand Up @@ -121,8 +121,6 @@ def _run_task(task: typing.Union[Task, RecurringTask]) -> typing.Tuple[Task, Tas
err_msg,
exc_info=True,
)
logger.debug("args: %s", str(task.args))
logger.debug("kwargs: %s", str(task.kwargs))

task.mark_failure()

Expand Down
24 changes: 10 additions & 14 deletions tests/unit/task_processor/test_unit_task_processor_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,16 @@ def test_run_task_runs_task_and_creates_task_run_object_when_failure(
task.refresh_from_db()
assert not task.completed

assert len(caplog.records) == 5

log_record = caplog.records[0]
assert log_record.levelname == "ERROR"
assert log_record.message == (
f"Failed to execute task '{task.task_identifier}', with id {task.id}. Exception: {msg}"
)

debug_log_args, debug_log_kwargs = caplog.records[1:]
assert debug_log_args.levelname == "DEBUG"
assert debug_log_args.message == f"args: ['{msg}']"

assert debug_log_kwargs.levelname == "DEBUG"
assert debug_log_kwargs.message == "kwargs: {}"
expected_log_records = [
("DEBUG", "Running 1 task(s)"),
("DEBUG", f"Running task {task.task_identifier} id={task.id} args={task.args} kwargs={task.kwargs}"),
("ERROR", f"Failed to execute task '{task.task_identifier}', with id {task.id}. Exception: {msg}"),
("DEBUG", "Finished running 1 task(s)")
]

assert expected_log_records == [
(record.levelname, record.message) for record in caplog.records
]


def test_run_task_runs_failed_task_again(db):
Expand Down

0 comments on commit cf0a1e9

Please sign in to comment.