Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ async def start(
force_permission_level: ActorPermissionLevel | None = None,
webhooks: list[Webhook] | None = None,
run_name: str | None = None,
abort_with_parent: bool = False,
abort_with_parent: bool | None = None,
) -> Run:
"""Run an Actor on the Apify platform.

Expand Down Expand Up @@ -1109,17 +1109,16 @@ async def start(
migration. To start a new run for an identical call, e.g. when polling, pass a `run_name` that differs
between the calls.
abort_with_parent: If true, the child run is gracefully aborted when this Actor run is gracefully
aborted. It requires `run_name`, and the value is recorded under it, replacing the one from an earlier
call. Once this Actor run is being gracefully aborted, a call with it raises a `RuntimeError` and
starts, resurrects or reattaches no run. A hard abort, a timeout or a crash of this Actor run leaves
the child running.
aborted. The value is recorded with the run, replacing the one from an earlier call under the same
run name or implicit key. If not set, a reused run keeps its recorded value, and a new run is not
aborted with this Actor run. Once this Actor run is being gracefully aborted, a call for a run marked
this way raises a `RuntimeError` and starts, resurrects or reattaches no run, unless the recorded run
`SUCCEEDED`. A hard abort, a timeout or a crash of this
Actor run leaves the child running.

Returns:
Info about the started Actor run
"""
if abort_with_parent and run_name is None:
raise ValueError('`abort_with_parent` requires `run_name`, since only named child runs are tracked.')

if max_items is not None:
_warn_max_items_deprecated()

Expand Down Expand Up @@ -1270,7 +1269,7 @@ async def call(
wait: timedelta | None = None,
logger: logging.Logger | Literal['default'] | None = 'default',
run_name: str | None = None,
abort_with_parent: bool = False,
abort_with_parent: bool | None = None,
) -> Run:
"""Start an Actor on the Apify Platform and wait for it to finish before returning.

Expand Down Expand Up @@ -1314,17 +1313,16 @@ async def call(
migration. To start a new run for an identical call, e.g. when polling, pass a `run_name` that differs
between the calls.
abort_with_parent: If true, the child run is gracefully aborted when this Actor run is gracefully
aborted. It requires `run_name`, and the value is recorded under it, replacing the one from an earlier
call. Once this Actor run is being gracefully aborted, a call with it raises a `RuntimeError` and
starts, resurrects or reattaches no run. A hard abort, a timeout or a crash of this Actor run leaves
the child running.
aborted. The value is recorded with the run, replacing the one from an earlier call under the same
run name or implicit key. If not set, a reused run keeps its recorded value, and a new run is not
aborted with this Actor run. Once this Actor run is being gracefully aborted, a call for a run marked
this way raises a `RuntimeError` and starts, resurrects or reattaches no run, unless the recorded run
`SUCCEEDED`. A hard abort, a timeout or a crash of this
Actor run leaves the child running.

Returns:
Info about the started Actor run.
"""
if abort_with_parent and run_name is None:
raise ValueError('`abort_with_parent` requires `run_name`, since only named child runs are tracked.')

if max_items is not None:
_warn_max_items_deprecated()

Expand Down Expand Up @@ -1390,7 +1388,7 @@ async def _find_or_start_child_run(
memory_mbytes: int | None,
timeout: timedelta | Literal['inherit'] | None,
webhooks: list[Webhook] | None,
abort_with_parent: bool,
abort_with_parent: bool | None,
) -> tuple[Run, bool]:
run_options = {
'content_type': content_type,
Expand Down Expand Up @@ -1466,7 +1464,7 @@ async def start_task(
webhooks: list[Webhook] | None = None,
token: str | None = None,
run_name: str | None = None,
abort_with_parent: bool = False,
abort_with_parent: bool | None = None,
) -> Run:
"""Start an Actor task on the Apify Platform.

Expand Down Expand Up @@ -1507,17 +1505,16 @@ async def start_task(
migration. To start a new run for an identical call, e.g. when polling, pass a `run_name` that differs
between the calls.
abort_with_parent: If true, the child run is gracefully aborted when this Actor run is gracefully
aborted. It requires `run_name`, and the value is recorded under it, replacing the one from an earlier
call. Once this Actor run is being gracefully aborted, a call with it raises a `RuntimeError` and
starts, resurrects or reattaches no run. A hard abort, a timeout or a crash of this Actor run leaves
the child running.
aborted. The value is recorded with the run, replacing the one from an earlier call under the same
run name or implicit key. If not set, a reused run keeps its recorded value, and a new run is not
aborted with this Actor run. Once this Actor run is being gracefully aborted, a call for a run marked
this way raises a `RuntimeError` and starts, resurrects or reattaches no run, unless the recorded run
`SUCCEEDED`. A hard abort, a timeout or a crash of this
Actor run leaves the child running.

Returns:
Info about the started Actor run.
"""
if abort_with_parent and run_name is None:
raise ValueError('`abort_with_parent` requires `run_name`, since only named child runs are tracked.')

if max_items is not None:
_warn_max_items_deprecated()

Expand Down Expand Up @@ -1568,7 +1565,7 @@ async def call_task(
wait: timedelta | None = None,
token: str | None = None,
run_name: str | None = None,
abort_with_parent: bool = False,
abort_with_parent: bool | None = None,
) -> Run:
"""Start an Actor task on the Apify Platform and wait for it to finish before returning.

Expand Down Expand Up @@ -1609,17 +1606,16 @@ async def call_task(
migration. To start a new run for an identical call, e.g. when polling, pass a `run_name` that differs
between the calls.
abort_with_parent: If true, the child run is gracefully aborted when this Actor run is gracefully
aborted. It requires `run_name`, and the value is recorded under it, replacing the one from an earlier
call. Once this Actor run is being gracefully aborted, a call with it raises a `RuntimeError` and
starts, resurrects or reattaches no run. A hard abort, a timeout or a crash of this Actor run leaves
the child running.
aborted. The value is recorded with the run, replacing the one from an earlier call under the same
run name or implicit key. If not set, a reused run keeps its recorded value, and a new run is not
aborted with this Actor run. Once this Actor run is being gracefully aborted, a call for a run marked
this way raises a `RuntimeError` and starts, resurrects or reattaches no run, unless the recorded run
`SUCCEEDED`. A hard abort, a timeout or a crash of this
Actor run leaves the child running.

Returns:
Info about the started Actor run.
"""
if abort_with_parent and run_name is None:
raise ValueError('`abort_with_parent` requires `run_name`, since only named child runs are tracked.')

if max_items is not None:
_warn_max_items_deprecated()

Expand Down
36 changes: 22 additions & 14 deletions src/apify/_child_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def find_or_start(
client: ApifyClientAsync,
start_run: Callable[[], Awaitable[Run]],
resurrect_run: Callable[[str], Awaitable[Run]],
abort_with_parent: bool = False,
abort_with_parent: bool | None = None,
) -> tuple[Run, bool]:
"""Return the run recorded under `name` or the implicit key, or start one when there is none to reuse.

Expand All @@ -159,45 +159,47 @@ async def find_or_start(
client: Client used to look up the recorded run.
start_run: Starts a new run of the Actor or task.
resurrect_run: Resurrects the recorded run, given its ID.
abort_with_parent: Whether to abort the run when this Actor run is gracefully aborted. It replaces the
recorded value.
abort_with_parent: Whether to abort the run when this Actor run is gracefully aborted. `True` or `False`
replaces the recorded value. `None` keeps it, and a run with no recorded value is not aborted.

Returns:
The run, and whether it was newly started.

Raises:
ValueError: If the recorded run would be reused, but was started for a different Actor, task, input or
run options.
RuntimeError: If `abort_with_parent` is set and this Actor run is being gracefully aborted. No run is
started, resurrected or reattached.
RuntimeError: If this Actor run is being gracefully aborted and the run is marked `abort_with_parent`, by
this call or, when the call does not set it, by the record. No run is started, resurrected or
reattached. A recorded run that `SUCCEEDED` is still returned.
"""
checksum = checksum_request(actor_id=actor_id, task_id=task_id, run_input=run_input, run_options=run_options)
key = name if name is not None else checksum[:_IMPLICIT_KEY_HASH_LENGTH]
new_abort_with_parent = bool(abort_with_parent)

async with self._name_locks.setdefault(key, asyncio.Lock()):
if abort_with_parent and self._parent_aborting:
raise RuntimeError(
f'Child run "{key}" with `abort_with_parent` was not started, since this Actor run is being '
'aborted.'
)

records = await self._load()
record = records.get(key)
marked = record is not None and record.abort_with_parent if abort_with_parent is None else abort_with_parent

if record is None:
self._raise_if_parent_aborting(key, marked=marked)
run = await self._start(
key,
run_name=name,
checksum=checksum,
start_run=start_run,
abort_with_parent=abort_with_parent,
abort_with_parent=new_abort_with_parent,
)
self._clients[key] = client
return run, True

run_client = client.run(record.run_id)
run = await _get_recorded_run(run_client)

# Only a run that already succeeded can be returned without leaving a marked run running.
if run is None or run.status != 'SUCCEEDED':
self._raise_if_parent_aborting(key, marked=marked)

if run is not None and run.status in _SETTLING_STATUSES:
run = await run_client.wait_for_finish()

Expand All @@ -207,7 +209,7 @@ async def find_or_start(
run_name=name,
checksum=checksum,
start_run=start_run,
abort_with_parent=abort_with_parent,
abort_with_parent=record.abort_with_parent if abort_with_parent is None else abort_with_parent,
)
self._clients[key] = client
return run, True
Expand All @@ -218,7 +220,7 @@ async def find_or_start(
'Use a unique `run_name` for each child run.'
)

if record.abort_with_parent != abort_with_parent:
if abort_with_parent is not None and record.abort_with_parent != abort_with_parent:
await self._save(key, record.model_copy(update={'abort_with_parent': abort_with_parent}))

if run.status in _RESURRECTABLE_STATUSES:
Expand All @@ -231,6 +233,12 @@ async def find_or_start(
self._clients[key] = client
return run, False

def _raise_if_parent_aborting(self, key: str, *, marked: bool) -> None:
if marked and self._parent_aborting:
raise RuntimeError(
f'Child run "{key}" with `abort_with_parent` was not started, since this Actor run is being aborted.'
)

def run_clients(self, default_client: ApifyClientAsync) -> dict[str, RunClientAsync]:
"""Return a client for the current run under each recorded key.

Expand Down
Loading
Loading