Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz committed Jan 6, 2025
1 parent c5821de commit 2d23cb9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/prefect/locking/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from typing import Optional

import anyio
import pendulum
import pydantic_core
from typing_extensions import TypedDict

from prefect.logging.loggers import get_logger
from prefect.types import DateTime
from prefect.types._datetime import Duration, parse_datetime

from .protocol import LockManager

Expand Down Expand Up @@ -64,7 +64,7 @@ def _get_lock_info(self, key: str, use_cache: bool = True) -> Optional[_LockInfo
lock_info["path"] = lock_path
expiration = lock_info.get("expiration")
lock_info["expiration"] = (
pendulum.parse(expiration) if expiration is not None else None
parse_datetime(expiration) if expiration is not None else None
)
self._locks[key] = lock_info
return lock_info
Expand All @@ -86,7 +86,7 @@ async def _aget_lock_info(
lock_info["path"] = lock_path
expiration = lock_info.get("expiration")
lock_info["expiration"] = (
pendulum.parse(expiration) if expiration is not None else None
parse_datetime(expiration) if expiration is not None else None
)
self._locks[key] = lock_info
return lock_info
Expand Down Expand Up @@ -117,7 +117,7 @@ def acquire_lock(
)
return self.acquire_lock(key, holder, acquire_timeout, hold_timeout)
expiration = (
DateTime.now("utc") + pendulum.duration(seconds=hold_timeout)
DateTime.now("utc") + Duration(seconds=hold_timeout)
if hold_timeout is not None
else None
)
Expand Down Expand Up @@ -166,7 +166,7 @@ async def aacquire_lock(
)
return self.acquire_lock(key, holder, acquire_timeout, hold_timeout)
expiration = (
DateTime.now("utc") + pendulum.duration(seconds=hold_timeout)
DateTime.now("utc") + Duration(seconds=hold_timeout)
if hold_timeout is not None
else None
)
Expand Down
5 changes: 2 additions & 3 deletions src/prefect/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
PREFECT_LOGGING_TO_API_MAX_LOG_SIZE,
PREFECT_LOGGING_TO_API_WHEN_MISSING_FLOW,
)
from prefect.types._datetime import from_timestamp


class APILogWorker(BatchedQueueService[Dict[str, Any]]):
Expand Down Expand Up @@ -219,9 +220,7 @@ def prepare(self, record: logging.LogRecord) -> Dict[str, Any]:
worker_id=worker_id,
name=record.name,
level=record.levelno,
timestamp=pendulum.from_timestamp(
getattr(record, "created", None) or time.time()
),
timestamp=from_timestamp(getattr(record, "created", None) or time.time()),
message=self.format(record),
).model_dump(mode="json")

Expand Down
29 changes: 26 additions & 3 deletions src/prefect/types/_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@

from typing import Annotated, Any

from pendulum import FixedTimezone, format_diff, from_format, instance, parse, tz
from pendulum import (
FixedTimezone,
format_diff,
parse,
tz,
)
from pendulum import (
from_format as from_format_pendulum,
)
from pendulum import (
from_timestamp as from_timestamp_pendulum,
)
from pendulum import (
instance as instance_pendulum,
)
from pendulum.tz import UTC, Timezone
from pydantic import Field
from pydantic_extra_types.pendulum_dt import Date as PydanticDate
Expand Down Expand Up @@ -32,21 +46,30 @@ def datetime_from_format(
tz: str | Timezone = UTC,
locale: str | None = None,
) -> DateTime:
return PydanticDateTime(from_format(string, fmt, tz, locale)) # type: ignore
return from_format_pendulum(string, fmt, tz, locale) # type: ignore


def parse_datetime(string: str) -> DateTime:
return parse(string) # type: ignore


def datetime_instance(value: Any) -> DateTime:
return instance(value) # type: ignore
"""
Here to fulfull the needs that pendulum.instance meets, but it
is often used ambiguously and we should phase this out over time
in favor of more explicit datetime utilities.
"""
return instance_pendulum(value) # type: ignore


def local_timezone() -> Timezone | FixedTimezone:
return tz.local_timezone()


def from_timestamp(timestamp: float) -> DateTime:
return from_timestamp_pendulum(timestamp) # type: ignore


__all__ = [
"DateTime",
"Date",
Expand Down
5 changes: 2 additions & 3 deletions tests/docker/test_registry_pushes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def contexts() -> Path:
@pytest.fixture(scope="module")
def frozen_now():
now = DateTime.now("UTC")
with mock.patch("DateTime.now", return_value=now):
with mock.patch("prefect.types.DateTime.now", return_value=now):
yield now


Expand All @@ -45,8 +45,7 @@ def howdy(docker: DockerClient, worker_id: str, frozen_now: DateTime) -> str:
image.add_line(f'ENTRYPOINT [ "echo", "{message}" ]')
image_id = image.build()

greeting = docker.containers.run(image_id, remove=True).decode().strip()
assert greeting == message
assert docker.containers.run(image_id, remove=True).decode().strip() == message # type: ignore

# Give the image a unit tag for this run we we can confirm it is only untagged but
# not removed by the process of pushing it to the registry
Expand Down

0 comments on commit 2d23cb9

Please sign in to comment.