-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add RemainingTime
option for timeout
argument of Actor.call
and Actor.start
#473
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a992dcb
draft
Pijukatel b832919
Draft of actor call with RemainingTime timeout
Pijukatel 74de324
Draft
Pijukatel b0cc5a7
Update tests
Pijukatel c11b5d7
Merge branch 'master' into remaining-timeout
Pijukatel 7202c17
Update src/apify/_actor.py
Pijukatel 05eed5f
Review comments
Pijukatel 3586632
Merge branch 'master' into remaining-timeout
Pijukatel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from typing import TYPE_CHECKING | ||
|
||
from apify import Actor | ||
|
||
if TYPE_CHECKING: | ||
from .conftest import MakeActorFunction, RunActorFunction | ||
|
||
|
||
async def test_actor_start_remaining_timeout( | ||
make_actor: MakeActorFunction, | ||
run_actor: RunActorFunction, | ||
) -> None: | ||
"""Test that correct timeout is set when using `RemainingTime` value for the `timeout` argument. | ||
|
||
In this test, one Actor starts itself again and checks that the timeout is correctly set on the second Actor run. | ||
Timeout should be the remaining time of the first Actor run calculated at the moment of the other Actor start.""" | ||
|
||
async def main() -> None: | ||
from datetime import datetime, timezone | ||
|
||
async with Actor: | ||
actor_input = (await Actor.get_input()) or {} | ||
if actor_input.get('called_from_another_actor', False) is True: | ||
# If this Actor run was started with a specific argument (the second Actor run), return immediately. | ||
# Asserts checking the timeout are in the first Actor run. | ||
return | ||
|
||
# Start another run of this actor with timeout set to the time remaining in this actor run | ||
other_run_data = await Actor.call( | ||
actor_id=Actor.configuration.actor_id or '', | ||
run_input={'called_from_another_actor': True}, | ||
timeout='RemainingTime', | ||
) | ||
assert other_run_data is not None | ||
try: | ||
# To make sure that the actor is started | ||
await asyncio.sleep(5) | ||
assert other_run_data.options is not None | ||
assert Actor.configuration.timeout_at is not None | ||
assert Actor.configuration.started_at is not None | ||
|
||
remaining_time_after_actor_start = Actor.configuration.timeout_at - datetime.now(tz=timezone.utc) | ||
|
||
assert other_run_data.options.timeout > remaining_time_after_actor_start | ||
assert other_run_data.options.timeout < Actor.configuration.timeout_at - Actor.configuration.started_at | ||
finally: | ||
# Make sure the other actor run is aborted | ||
await Actor.apify_client.run(other_run_data.id).abort() | ||
|
||
actor = await make_actor(label='remaining-timeout', main_func=main) | ||
run_result = await run_actor(actor) | ||
|
||
assert run_result.status == 'SUCCEEDED' | ||
|
||
|
||
async def test_actor_call_remaining_timeout( | ||
make_actor: MakeActorFunction, | ||
run_actor: RunActorFunction, | ||
) -> None: | ||
"""Test that correct timeout is set when using `RemainingTime` value for the `timeout` argument. | ||
|
||
In this test, one Actor starts itself again and checks that the timeout is correctly set on the second Actor run. | ||
Timeout should be the remaining time of the first Actor run calculated at the moment of the other Actor call.""" | ||
|
||
async def main() -> None: | ||
from datetime import datetime, timezone | ||
|
||
async with Actor: | ||
actor_input = (await Actor.get_input()) or {} | ||
if actor_input.get('called_from_another_actor', False) is True: | ||
# If this Actor run was started with a specific argument (the second Actor run), return immediately. | ||
# Asserts checking the timeout are in the first Actor run. | ||
return | ||
|
||
# Start another run of this actor with timeout set to the time remaining in this actor run | ||
other_run_data = await Actor.call( | ||
actor_id=Actor.configuration.actor_id or '', | ||
run_input={'called_from_another_actor': True}, | ||
timeout='RemainingTime', | ||
) | ||
|
||
assert other_run_data is not None | ||
try: | ||
# To make sure that the actor is started | ||
await asyncio.sleep(5) | ||
|
||
assert other_run_data.options is not None | ||
assert Actor.configuration.timeout_at is not None | ||
assert Actor.configuration.started_at is not None | ||
|
||
remaining_time_after_actor_start = Actor.configuration.timeout_at - datetime.now(tz=timezone.utc) | ||
|
||
assert other_run_data.options.timeout > remaining_time_after_actor_start | ||
assert other_run_data.options.timeout < Actor.configuration.timeout_at - Actor.configuration.started_at | ||
finally: | ||
# Make sure the other actor run is aborted | ||
await Actor.apify_client.run(other_run_data.id).abort() | ||
|
||
actor = await make_actor(label='remaining-timeout', main_func=main) | ||
run_result = await run_actor(actor) | ||
|
||
assert run_result.status == 'SUCCEEDED' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the Actor finishes immediately and you just check that the timeout was configured correctly? I agree that it's fine to trust the platform. Could you add a comment here that explains that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more comments