Skip to content
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

fix: actually set global actual object lifecycle prefs #323

Merged
merged 2 commits into from
Oct 4, 2024
Merged
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
16 changes: 10 additions & 6 deletions projects/fal/src/fal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from fal.api import RouteSignature
from fal.exceptions import RequestCancelledException
from fal.logging import get_logger
from fal.toolkit.file.providers import fal as fal_provider_module
from fal.toolkit.file import get_lifecycle_preference
from fal.toolkit.file.providers.fal import GLOBAL_LIFECYCLE_PREFERENCE

REALTIME_APP_REQUIREMENTS = ["websockets", "msgpack"]

Expand Down Expand Up @@ -266,19 +267,22 @@ async def provide_hints_headers(request, call_next):

@app.middleware("http")
async def set_global_object_preference(request, call_next):
response = await call_next(request)
try:
fal_provider_module.GLOBAL_LIFECYCLE_PREFERENCE = request.headers.get(
"X-Fal-Object-Lifecycle-Preference"
)
preference_dict = get_lifecycle_preference(request) or {}
expiration_duration = preference_dict.get("expiration_duration_seconds")
if expiration_duration is not None:
GLOBAL_LIFECYCLE_PREFERENCE.expiration_duration_seconds = int(
expiration_duration
)

except Exception:
from fastapi.logger import logger

logger.exception(
"Failed set a global lifecycle preference %s",
self.__class__.__name__,
)
return response
return await call_next(request)

@app.exception_handler(RequestCancelledException)
async def value_error_exception_handler(
Expand Down
6 changes: 3 additions & 3 deletions projects/fal/src/fal/toolkit/file/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def from_bytes(

fdata = FileData(data, content_type, file_name)

object_lifecycle_preference = _get_lifecycle_preference(request)
object_lifecycle_preference = get_lifecycle_preference(request)

try:
url = repo.save(fdata, object_lifecycle_preference)
Expand Down Expand Up @@ -191,7 +191,7 @@ def from_path(
)

content_type = content_type or "application/octet-stream"
object_lifecycle_preference = _get_lifecycle_preference(request)
object_lifecycle_preference = get_lifecycle_preference(request)

try:
url, data = repo.save_file(
Expand Down Expand Up @@ -274,7 +274,7 @@ def __del__(self):
shutil.rmtree(self.extract_dir)


def _get_lifecycle_preference(request: Request) -> dict[str, str] | None:
def get_lifecycle_preference(request: Request) -> dict[str, str] | None:
import json

preference_str = (
Expand Down
Loading