-
Notifications
You must be signed in to change notification settings - Fork 15
fix: Unify Actor context manager with init & exit methods #600
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
vdusek
wants to merge
7
commits into
master
Choose a base branch
from
init-exit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+653
−291
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bdc399d
fix: Unify Actor context manager with init & exit methods
vdusek 7735089
fix exception integration test
vdusek 9d76b44
fix actor exit code
vdusek 84d5880
more and better tests
vdusek 0e50649
update docs
vdusek 1636b97
address feedback
vdusek 3419d51
address feedback
vdusek 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,21 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
async with Actor: | ||
# Get input | ||
actor_input = await Actor.get_input() | ||
Actor.log.info('Actor input: %s', actor_input) | ||
|
||
# Your Actor logic here | ||
data = {'message': 'Hello from Actor!', 'input': actor_input} | ||
await Actor.push_data(data) | ||
|
||
# Set status message | ||
await Actor.set_status_message('Actor completed successfully') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,26 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
await Actor.init() | ||
|
||
try: | ||
# Get input | ||
actor_input = await Actor.get_input() | ||
Actor.log.info('Actor input: %s', actor_input) | ||
|
||
# Your Actor logic here | ||
data = {'message': 'Hello from Actor!', 'input': actor_input} | ||
await Actor.push_data(data) | ||
|
||
# Set status message | ||
await Actor.set_status_message('Actor completed successfully') | ||
|
||
finally: | ||
await Actor.exit() | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,13 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
async with Actor: | ||
# Any unhandled exception triggers Actor.fail() automatically | ||
raise RuntimeError('Boom') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,33 @@ | ||
import asyncio | ||
import random | ||
|
||
from apify import Actor | ||
|
||
|
||
async def do_work() -> None: | ||
# Simulate random outcomes: success or one of two exception types. | ||
outcome = random.random() | ||
|
||
if outcome < 0.33: | ||
raise ValueError('Invalid input data encountered') | ||
if outcome < 0.66: | ||
raise RuntimeError('Unexpected runtime failure') | ||
|
||
# Simulate successful work | ||
Actor.log.info('Work completed successfully') | ||
|
||
|
||
async def main() -> None: | ||
await Actor.init() | ||
try: | ||
await do_work() | ||
except ValueError as exc: # Specific error mapping example | ||
await Actor.fail(exit_code=10, exception=exc) | ||
except Exception as exc: # Catch-all for unexpected errors | ||
await Actor.fail(exit_code=91, exception=exc) | ||
else: | ||
await Actor.exit(status_message='Actor completed successfully') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,27 @@ | ||
import asyncio | ||
from datetime import timedelta | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
actor = Actor( | ||
event_listeners_timeout=timedelta(seconds=30), | ||
cleanup_timeout=timedelta(seconds=30), | ||
) | ||
|
||
async with actor: | ||
# Get input | ||
actor_input = await actor.get_input() | ||
actor.log.info('Actor input: %s', actor_input) | ||
|
||
# Your Actor logic here | ||
data = {'message': 'Hello from Actor instance!', 'input': actor_input} | ||
await actor.push_data(data) | ||
|
||
# Set status message | ||
await actor.set_status_message('Actor completed successfully') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,32 @@ | ||
import asyncio | ||
from datetime import timedelta | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
actor = Actor( | ||
event_listeners_timeout=timedelta(seconds=30), | ||
cleanup_timeout=timedelta(seconds=30), | ||
) | ||
|
||
await actor.init() | ||
|
||
try: | ||
# Get input | ||
actor_input = await actor.get_input() | ||
actor.log.info('Actor input: %s', actor_input) | ||
|
||
# Your Actor logic here | ||
data = {'message': 'Hello from Actor!', 'input': actor_input} | ||
await actor.push_data(data) | ||
|
||
# Set status message | ||
await actor.set_status_message('Actor completed successfully') | ||
|
||
finally: | ||
await actor.exit() | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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 |
---|---|---|
@@ -1,7 +1,23 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
async with Actor: | ||
# ... your code here ... | ||
await Actor.reboot() | ||
# Use the KVS to persist a simple reboot counter across restarts. | ||
kvs = await Actor.open_key_value_store() | ||
reboot_counter = await kvs.get_value('reboot_counter', 0) | ||
|
||
# Limit the number of reboots to avoid infinite loops. | ||
if reboot_counter < 3: | ||
await kvs.set_value('reboot_counter', reboot_counter + 1) | ||
Actor.log.info(f'Reboot attempt {reboot_counter + 1}/3') | ||
# Trigger a platform reboot; after restart the code runs from the beginning. | ||
await Actor.reboot() | ||
|
||
Actor.log.info('Reboot limit reached, finishing run') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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 |
---|---|---|
@@ -1,14 +1,24 @@ | ||
import asyncio | ||
|
||
from apify import Actor | ||
|
||
|
||
async def main() -> None: | ||
async with Actor: | ||
await Actor.set_status_message('Here we go!') | ||
# Do some work... | ||
await asyncio.sleep(3) | ||
await Actor.set_status_message('So far so good...') | ||
await asyncio.sleep(3) | ||
# Do some more work... | ||
await Actor.set_status_message('Steady as she goes...') | ||
await asyncio.sleep(3) | ||
# Do even more work... | ||
await Actor.set_status_message('Almost there...') | ||
await asyncio.sleep(3) | ||
# Finish the job | ||
await Actor.set_status_message('Phew! That was not that hard!') | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.