Skip to content

feat: redirect run logs from a given time and resume after the platform closes the stream - #1253

Draft
Pijukatel wants to merge 1 commit into
masterfrom
claude/affectionate-fermi-74vvu9
Draft

Pijukatel wants to merge 1 commit into
masterfrom
claude/affectionate-fermi-74vvu9

Conversation

@Pijukatel

Copy link
Copy Markdown
Contributor

Summary

Client-side workaround for apify/apify-core#26653: the platform closes a log stream that has carried no data for about 330 s, even when the run is still going, so log redirection of a quiet run stops for good.

  • since argument. RunClient.get_streamed_log(), StreamedLog, and their async counterparts take since: datetime | None and redirect only the lines logged at that time or later. A naive datetime is treated as UTC, like elsewhere in the client.
  • from_start deprecated. It still works and emits a DeprecationWarning that points at the caller. from_start=True maps to since=None, and from_start=False to since=datetime.now(tz=UTC). Passing both raises ValueError. The pattern follows the actor_standby_* deprecation.
  • Resume after the stream closes. get_streamed_log() now passes the run client to the streamed log. When a stream ends, the client checks the run status:
    • Run still going: reopen the stream. The API replays the log from its start, and the client skips lines up to and including the last redirected one. It tracks how many lines share that millisecond timestamp, so lines logged in the same millisecond are neither lost nor doubled.
    • Stream broke off with an error and the run has finished meanwhile: reopen once more to catch the last lines.
    • Stream ended on its own and the run has finished: stop, as before.
  • Cut-off last line. On a stream that may be reopened, the unterminated last line is dropped rather than logged, because the reopened stream delivers it in full.
  • Backoff. Reopens that fail before delivering anything back off from 1 s up to 30 s.
  • Docs. The logging concept page describes since and the resume behaviour, the examples use since, and a new Upgrading to v4 guide covers removing from_start.

StreamedLog built directly, without run_client, behaves as before: no status checks and no reopens.

Notes for review

  • Cost per reopen. The public log API can't start from a given point, so every reopen downloads the whole log again (up to about 10 MB) plus one run status request. For a quiet run that means one replay roughly every 5.5 minutes. A since/lastLines parameter on the API would remove the replay; that is a separate apify-core change.
  • Known gaps:
    • If the platform closes the stream cleanly and the run finishes before the status check, lines logged in that window are missed. A stream that breaks off with an error is covered by the catch-up reopen.
    • Resume works from log timestamps. A run that migrates to a host whose clock is behind can produce a line or two duplicated around the resume point.
  • Behaviour change. A client-side stream timeout (the 24 h cap of no_timeout in Impit) on a running run now reopens the stream instead of ending the redirection. The two timeout tests were updated for that.
  • Possible flake. test_actor_call_redirect_logs_to_custom_logger_async failed once in the first parallel run and passed in 14 reruns since. A likely cause, not confirmed: the reopen status checks share the mock's status counter with the status message watcher. Worth watching in CI.
  • New docs page. The v4 upgrading guide is a new page under docs/04_upgrading/ and will show in the docs sidebar before v4 exists.

Testing

  • uv run poe check-code: lint, type check, docstring check, and 2990 unit tests pass.
  • New unit tests, sync and async:
    • resume after a stream closes while the run is going (same-millisecond lines, cut-off last line)
    • one catch-up reopen after a stream breaks off and the run finishes
    • no reopen for a finished run
    • reopen backoff
    • from_start deprecation mapping and warning location
    • rejecting both since and from_start
    • naive since read as UTC
  • Not tested against the real platform.

Generated by Claude Code

…rm closes the stream

`RunClient.get_streamed_log()`, `StreamedLog`, and their async counterparts take
a `since` datetime and redirect only the log lines logged at that time or later.
The `from_start` argument is deprecated in favour of it: `from_start=True` maps to
`since=None` and `from_start=False` to `since=datetime.now(tz=UTC)`. It emits a
`DeprecationWarning` and will be removed in v4, which the new v4 upgrading guide
describes.

The platform closes a log stream that has carried no data for several minutes,
even when the run is still going (apify/apify-core#26653). A streamed log created
by `get_streamed_log()` now checks the run status when its stream ends, reopens
the stream if the run has not finished, and skips the replayed lines up to the
last redirected one. A stream that broke off with an error gets one more reopen
after the run finishes, so the last lines are not lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ocuAHA5qh4VqF6Ps7XR5p
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.03106% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.37%. Comparing base (4b9b1a4) to head (56f958a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/_streamed_log.py 94.83% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1253      +/-   ##
==========================================
- Coverage   95.68%   91.37%   -4.31%     
==========================================
  Files          65       65              
  Lines        6770     6901     +131     
==========================================
- Hits         6478     6306     -172     
- Misses        292      595     +303     
Flag Coverage Δ
integration ?
unit 91.37% <95.03%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants