Skip to content

feat: Add StatusMessageRedirector #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

Pijukatel
Copy link
Contributor

@Pijukatel Pijukatel commented May 20, 2025

Description

Add the option to log status and status messages of another Actor run. If such Actor run is also redirecting logs from another Actor run, then those logs can propagate all the way to the top through the StreamedLog - deep redirection. If the Actor run from which we are redirecting status messages is not redirecting logs from its children, then the log and status redirection remains shallow - only from that Actor run.

Example usage:

Through ActorClient(Async).call

By default it is turned on, so just calling actor like this already redirects the logs to pre-configured logger:

actor_client = ApifyClientAsync(token='token').actor(actor_id='actor_id')
await actor_client.call()

To turn off, pass logger=None

...
await actor_client.call(logger=None)

Or pass custom logger, if desired:

...
await actor_client.call(logger=some_other_logger)

Through RunClient(Async).get_status_message_redirector

With context:

run_client=ApifyClientAsync(token='token').run(run_id='run_id')
status_message_redirector = await run_client.get_status_message_redirector()

async with status_message_redirector:
    # Do stuff while the status from the other actor is being redirected to the logs. Leaving the context stops the redirection.
    ...

Manually:

...
status_message_redirector.start()
# Do stuff while the status from the other actor is being redirected to the logs.
status_message_redirector.stop()

Issues

@github-actions github-actions bot added this to the 115th sprint - Tooling team milestone May 20, 2025
@github-actions github-actions bot added t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics. labels May 20, 2025
@Pijukatel
Copy link
Contributor Author

Pijukatel commented May 20, 2025

Example log of recursive Actor run with highlighted redirected status messages
image

Example Actor:
https://console.apify.com/actors/3QIkuY6bClKBxC9wK

@Pijukatel Pijukatel force-pushed the redirect-status-messages branch from e903a68 to 18f4f51 Compare May 21, 2025 07:51
@Pijukatel Pijukatel requested a review from Copilot May 21, 2025 07:54
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds the new StatusMessageRedirector utility to stream status and status messages from a target Actor run (both shallow and deep redirection), integrates it into the client APIs, and updates tests to cover the new behavior.

  • Introduce StatusMessageRedirector (sync & async) in log.py
  • Add get_status_message_redirector to RunClient (sync & async) in run.py
  • Hook status redirection into ActorClient.call (sync & async) in actor.py
  • Update and extend test_logging.py to exercise status message streaming

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/unit/test_logging.py Add fixtures and new tests for StatusMessageRedirector
src/apify_client/clients/resource_clients/run.py New get_status_message_redirector methods for sync and async runs
src/apify_client/clients/resource_clients/log.py Core StatusMessageRedirector, StatusMessageRedirectorSync/Async
src/apify_client/clients/resource_clients/actor.py Wire up status redirector into ActorClient.call sync & async variants
Comments suppressed due to low confidence (5)

tests/unit/test_logging.py:386

  • [nitpick] We should also add a test for the manual start()/stop() usage of StatusMessageRedirector (outside of a context manager) to ensure that flow is covered.
@respx.mock

src/apify_client/clients/resource_clients/run.py:349

  • create_redirect_logger is used here but not imported; add from apify_client._logging import create_redirect_logger at the top.
to_logger = create_redirect_logger(f'apify.{name}')

src/apify_client/clients/resource_clients/log.py:455

  • asyncio.create_task is called but asyncio is not imported; add import asyncio.
self._logging_task = asyncio.create_task(self._log_changed_status_message())

tests/unit/test_logging.py:168

  • This fixture mutates a class‐level variable without resetting it, which can leak state across tests; consider using yield and restoring the original value after the test.
StatusMessageRedirector._force_propagate = True

src/apify_client/clients/resource_clients/run.py:337

  • Docstring says it returns StatusMessageRedirector, but in sync/async methods it actually returns StatusMessageRedirectorSync/StatusMessageRedirectorAsync; clarify the concrete return type.
Returns:
            `StatusMessageRedirector` instance for redirected logs.

@Pijukatel Pijukatel marked this pull request as ready for review May 21, 2025 08:09
@Pijukatel Pijukatel requested review from vdusek and janbuchar May 21, 2025 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redirect status messages from other actors
1 participant