Skip to content

Commit

Permalink
fix: actually set global actual object lifecycle prefs (#323)
Browse files Browse the repository at this point in the history
* fix: actually set global actual object lifecycle prefs

* fix: actually set expiration duration
  • Loading branch information
mederka authored Oct 4, 2024
1 parent 12f303b commit fd07469
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
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

0 comments on commit fd07469

Please sign in to comment.