-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
EventsPipeline
and testing utilities (#14765)
- Loading branch information
1 parent
b7cca3d
commit 334859f
Showing
4 changed files
with
355 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import List | ||
|
||
import pendulum | ||
|
||
from prefect.server.events.schemas.events import ReceivedEvent | ||
from prefect.server.events.services import event_persister | ||
from prefect.server.services import task_run_recorder | ||
from prefect.server.utilities.messaging.memory import MemoryMessage | ||
|
||
|
||
class EventsPipeline: | ||
@staticmethod | ||
def events_to_messages(events) -> List[MemoryMessage]: | ||
messages = [] | ||
for event in events: | ||
received_event = ReceivedEvent( | ||
received=pendulum.now("UTC"), **event.model_dump() | ||
) | ||
message = MemoryMessage( | ||
data=received_event.model_dump_json().encode(), | ||
attributes={"id": str(event.id), "event": event.event}, | ||
) | ||
messages.append(message) | ||
return messages | ||
|
||
async def process_messages(self, messages: List[MemoryMessage]): | ||
for message in messages: | ||
await self.process_message(message) | ||
|
||
async def process_message(self, message: MemoryMessage): | ||
"""Process a single event message""" | ||
|
||
# TODO: Investigate if we want to include triggers/actions etc. | ||
async with task_run_recorder.consumer() as handler: | ||
await handler(message) | ||
|
||
async with event_persister.create_handler(batch_size=1) as handler: | ||
await handler(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.