Skip to content
Open
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
41 changes: 39 additions & 2 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ async def start(
webhooks: list[Webhook] | None = None,
run_name: str | None = None,
abort_with_parent: bool | None = None,
wait_for_resources: bool | timedelta = False,
) -> Run:
"""Run an Actor on the Apify platform.

Expand Down Expand Up @@ -1229,6 +1230,12 @@ async def start(
and `False` replace the recorded value and `None` keeps it. Once the child runs are being aborted,
a call that would start a new run with it raises a `RuntimeError`. It needs child run tracking and
cannot be combined with a custom `token`.
wait_for_resources: Whether to retry the start while the account lacks the memory or a concurrent-run slot
for the run. The start is retried every 10 seconds. `True` retries until the run starts, and
a `timedelta` stops after that time and raises the last error. A run that asks for more memory than
the account has in total never starts, so `True` retries it forever. It has no effect on a call that
returns a recorded run. Nothing is recorded while the start is retried, and once this Actor run is
being aborted, a start that waits for resources raises a `RuntimeError`.

Returns:
Info about the started Actor run
Expand Down Expand Up @@ -1269,6 +1276,7 @@ async def start(
webhooks=to_client_representations(webhooks),
),
abort_with_parent=abort_with_parent,
wait_for_resources=wait_for_resources,
)
return run

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

Expand Down Expand Up @@ -1433,6 +1442,12 @@ async def call(
and `False` replace the recorded value and `None` keeps it. Once the child runs are being aborted,
a call that would start a new run with it raises a `RuntimeError`. It needs child run tracking and
cannot be combined with a custom `token`.
wait_for_resources: Whether to retry the start while the account lacks the memory or a concurrent-run slot
for the run. The start is retried every 10 seconds. `True` retries until the run starts, and
a `timedelta` stops after that time and raises the last error. A run that asks for more memory than
the account has in total never starts, so `True` retries it forever. It has no effect on a call that
returns a recorded run. Nothing is recorded while the start is retried, and once this Actor run is
being aborted, a start that waits for resources raises a `RuntimeError`.

Returns:
Info about the started Actor run.
Expand Down Expand Up @@ -1473,6 +1488,7 @@ async def call(
webhooks=to_client_representations(webhooks),
),
abort_with_parent=abort_with_parent,
wait_for_resources=wait_for_resources,
)
# An earlier process of this Actor run already streamed the start of the log of a recorded run.
run = await self._wait_for_child_run(
Expand Down Expand Up @@ -1515,6 +1531,7 @@ async def _start_child_run(
content_type: str | None = None,
options: dict[str, Any],
abort_with_parent: bool | None,
wait_for_resources: bool | timedelta,
) -> tuple[Run, str | None, bool]:
"""Start a child run and record it, or return the run recorded under its explicit or implicit run name.

Expand All @@ -1531,12 +1548,15 @@ def start_run_now(**kwargs: Any) -> Awaitable[Run]:
return start_run(run_timeout=self._resolve_run_timeout(timeout), **kwargs)

if not self._child_run_registry.is_enabled:
return await start_run_now(), None, True
return await start_run_now(wait_for_resources=wait_for_resources), None, True

is_implicit = run_name is None and self.configuration.auto_memoize_child_runs
if run_name is None and not is_implicit:
run, key = await self._child_run_registry.start(
client=client, start_run=start_run_now, abort_with_parent=bool(abort_with_parent)
client=client,
start_run=start_run_now,
abort_with_parent=bool(abort_with_parent),
wait_for_resources=wait_for_resources,
)
return run, key, True

Expand All @@ -1556,6 +1576,7 @@ def start_run_now(**kwargs: Any) -> Awaitable[Run]:
client=client,
start_run=start_run_now,
abort_with_parent=abort_with_parent,
wait_for_resources=wait_for_resources,
)
return run, key, is_new

Expand Down Expand Up @@ -1632,6 +1653,7 @@ async def start_task(
token: str | None = None,
run_name: str | None = None,
abort_with_parent: bool | None = None,
wait_for_resources: bool | timedelta = False,
) -> Run:
"""Start an Actor task on the Apify Platform.

Expand Down Expand Up @@ -1678,6 +1700,12 @@ async def start_task(
and `False` replace the recorded value and `None` keeps it. Once the child runs are being aborted,
a call that would start a new run with it raises a `RuntimeError`. It needs child run tracking and
cannot be combined with a custom `token`.
wait_for_resources: Whether to retry the start while the account lacks the memory or a concurrent-run slot
for the run. The start is retried every 10 seconds. `True` retries until the run starts, and
a `timedelta` stops after that time and raises the last error. A run that asks for more memory than
the account has in total never starts, so `True` retries it forever. It has no effect on a call that
returns a recorded run. Nothing is recorded while the start is retried, and once this Actor run is
being aborted, a start that waits for resources raises a `RuntimeError`.

Returns:
Info about the started Actor run.
Expand Down Expand Up @@ -1711,6 +1739,7 @@ async def start_task(
run_input=task_input,
options=checksum_run_options(build=build, webhooks=to_client_representations(webhooks)),
abort_with_parent=abort_with_parent,
wait_for_resources=wait_for_resources,
)
return run

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

Expand Down Expand Up @@ -1783,6 +1813,12 @@ async def call_task(
and `False` replace the recorded value and `None` keeps it. Once the child runs are being aborted,
a call that would start a new run with it raises a `RuntimeError`. It needs child run tracking and
cannot be combined with a custom `token`.
wait_for_resources: Whether to retry the start while the account lacks the memory or a concurrent-run slot
for the run. The start is retried every 10 seconds. `True` retries until the run starts, and
a `timedelta` stops after that time and raises the last error. A run that asks for more memory than
the account has in total never starts, so `True` retries it forever. It has no effect on a call that
returns a recorded run. Nothing is recorded while the start is retried, and once this Actor run is
being aborted, a start that waits for resources raises a `RuntimeError`.

Returns:
Info about the started Actor run.
Expand Down Expand Up @@ -1818,6 +1854,7 @@ async def call_task(
run_input=task_input,
options=checksum_run_options(build=build, webhooks=to_client_representations(webhooks)),
abort_with_parent=abort_with_parent,
wait_for_resources=wait_for_resources,
)
# An earlier process of this Actor run already streamed the start of the log of a recorded run.
run = await self._wait_for_child_run(
Expand Down
110 changes: 103 additions & 7 deletions src/apify/_child_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import math
from abc import ABC, abstractmethod
from contextlib import asynccontextmanager, suppress
from datetime import timedelta
from logging import getLogger
from typing import TYPE_CHECKING, Any, NamedTuple
from weakref import WeakValueDictionary

from pydantic import BaseModel, ConfigDict, Field, ValidationError
from pydantic.alias_generators import to_camel

from apify_client.errors import NotFoundError
from apify_client.errors import ApifyApiError, NotFoundError
from apify_client.http_clients import StreamedRequestBody

from apify._utils import docs_group
Expand Down Expand Up @@ -48,6 +49,12 @@
STATUS_MAX_AGE_SECS = 10
"""How long an observed active status counts toward the concurrency limit before the run is fetched again."""

_RESOURCE_LIMIT_ERROR_TYPES = frozenset({'actor-memory-limit-exceeded', 'concurrent-runs-limit-exceeded'})
"""Error types the API rejects a run start with while the account lacks the memory or a run slot for it."""

WAIT_FOR_RESOURCES_COOLDOWN_SECS = 10
"""Time between two attempts to start a run that the API rejected for lack of account resources."""

_NOT_FOUND_GRACE_SECS = 3
"""How long a recorded run that the API reports as missing is looked up again before it counts as gone."""

Expand Down Expand Up @@ -395,6 +402,8 @@ def __init__(
"""Client the run under each key was last started or looked up with. Lost with the process."""
self._abort_task: asyncio.Task[None] | None = None
"""Aborts the runs marked `abort_with_parent`. Once it exists, no new run marked `abort_with_parent` starts."""
self._abort_started = asyncio.Event()
"""Set once the abort begins, to wake up the starts that wait for account resources."""
self._start_ids = itertools.count()
self._warned_excessive_runs = False
self._starts_in_flight = 0
Expand All @@ -420,6 +429,7 @@ async def find_or_start(
client: ApifyClientAsync,
start_run: Callable[[], Awaitable[Run]],
abort_with_parent: bool | None = None,
wait_for_resources: bool | timedelta = False,
) -> tuple[Run, bool]:
"""Return the run recorded under `key`, or start one when there is none.

Expand All @@ -434,13 +444,17 @@ async def find_or_start(
start_run: Starts a new run of the Actor or task.
abort_with_parent: Whether to abort the run when this Actor run is aborted. `True` and `False` replace
the value recorded under `key`, and `None` keeps it. A new run records `None` as `False`.
wait_for_resources: Whether to retry starting a new run while the API rejects it for the memory or
concurrent-run limit of the account. `True` retries until the run starts, and a `timedelta` stops after
that time and raises the last error.

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

Raises:
ValueError: If the run recorded under `key` was started with a different request.
RuntimeError: If a new run with `abort_with_parent` would start while this Actor run is being aborted.
RuntimeError: If a new run with `abort_with_parent` would start while this Actor run is being aborted, or
a new run would have to wait for account resources then.
"""
async with self._key_locks.setdefault(key, asyncio.Lock()):
record = self._get_records().get(key)
Expand Down Expand Up @@ -473,15 +487,20 @@ async def find_or_start(
)

self._check_not_aborting(key, abort_with_parent=bool(abort_with_parent))
async with self._start_in_flight(key):
run = await start_run()

async def record_run(run: Run) -> str:
new_record = ChildRunRecord(
run_id=run.id,
status=run.status,
abort_with_parent=bool(abort_with_parent),
checksum=checksum,
)
await self._store(key, new_record)
return key

run, _ = await self._start_with_retries(
key, start_run=start_run, record_run=record_run, wait_for_resources=wait_for_resources
)
self._clients[key] = client
return run, True

Expand All @@ -491,31 +510,106 @@ async def start(
client: ApifyClientAsync,
start_run: Callable[[], Awaitable[Run]],
abort_with_parent: bool = False,
wait_for_resources: bool | timedelta = False,
) -> tuple[Run, str]:
"""Start a run without a run name and record it.

Args:
client: Client the run is started with.
start_run: Starts the run of the Actor or task.
abort_with_parent: Whether to abort the run when this Actor run is aborted.
wait_for_resources: Whether to retry the start while the API rejects it for the memory or concurrent-run
limit of the account, as in `find_or_start`.

Returns:
The run, and its key in the registry.

Raises:
RuntimeError: If `abort_with_parent` is set and this Actor run is being aborted. No run is started.
RuntimeError: If `abort_with_parent` is set and this Actor run is being aborted, or the start would have
to wait for account resources then. No run is started.
"""
# The lock under a key of its own lets an abort of the child runs wait until the start is recorded.
async with self._key_locks.setdefault(f'start:{next(self._start_ids)}', asyncio.Lock()):
self._check_not_aborting('new child run', abort_with_parent=abort_with_parent)
async with self._start_in_flight('new child run'):
run = await start_run()

async def record_run(run: Run) -> str:
key = run_key(run.id)
record = ChildRunRecord(run_id=run.id, status=run.status, abort_with_parent=abort_with_parent)
await self._store(key, record)
return key

run, key = await self._start_with_retries(
'new child run', start_run=start_run, record_run=record_run, wait_for_resources=wait_for_resources
)
self._clients[key] = client
return run, key

async def _start_with_retries(
self,
name: str,
*,
start_run: Callable[[], Awaitable[Run]],
record_run: Callable[[Run], Awaitable[str]],
wait_for_resources: bool | timedelta,
) -> tuple[Run, str]:
"""Start a run and record it, retrying the start while the account lacks the resources for the run.

Between two attempts, the start is not in flight, so it counts neither toward the concurrency limit nor for
the wait before a reboot, metamorph or exit.

Returns:
The run, and its key in the registry.
"""
deadline = None
if isinstance(wait_for_resources, timedelta):
deadline = asyncio.get_running_loop().time() + wait_for_resources.total_seconds()

while True:
try:
async with self._start_in_flight(name):
run = await start_run()
key = await record_run(run)
except ApifyApiError as exc:
if wait_for_resources is False or exc.type not in _RESOURCE_LIMIT_ERROR_TYPES:
raise
await self._wait_for_resources(name, exc, deadline)
else:
return run, key

async def _wait_for_resources(self, name: str, exc: ApifyApiError, deadline: float | None) -> None:
"""Wait before the next attempt to start a run that the API rejected for lack of account resources.

Args:
name: Name of the child run for the log and errors.
exc: The error of the rejected start.
deadline: Event loop time at which the retrying stops, or `None` to retry until the run starts.

Raises:
ApifyApiError: `exc`, if the deadline passed.
RuntimeError: If this Actor run is being aborted.
"""
delay: float = WAIT_FOR_RESOURCES_COOLDOWN_SECS
if deadline is not None:
remaining = deadline - asyncio.get_running_loop().time()
if remaining <= 0:
raise exc
delay = min(delay, remaining)

if not self._abort_started.is_set():
logger.info(
'Not enough resources to start child run "%s", retrying in %.3g s: %s', name, delay, exc.message
)
try:
await asyncio.wait_for(self._abort_started.wait(), delay)
except TimeoutError:
return

# The parent is killed soon, and its abort waits for the lock of this start.
raise RuntimeError(
f'Child run "{name}" was not started, since this Actor run is being aborted and the account lacks the '
'resources for the run.'
) from exc

def child_runs(self, default_client: ApifyClientAsync) -> list[ChildRun]:
"""Return the key, the record and a client of every recorded run, in recording order.

Expand Down Expand Up @@ -561,6 +655,7 @@ async def abort_runs_with_parent(self, client: ApifyClientAsync) -> None:
"""
if self._abort_task is None:
self._abort_task = asyncio.create_task(self._abort_runs_with_parent(client))
self._abort_started.set()
# The calls that wait for the concurrency limit hold the locks that the abort waits for, and give up now.
self._capacity_changed.set()
try:
Expand Down Expand Up @@ -779,6 +874,7 @@ async def load(self) -> None:
"""
self._is_closed = False
self._abort_task = None
self._abort_started.clear()
self._starts_allowed.set()
async with self._lock:
try:
Expand Down
Loading