<@ULVA73B9P>: Can deployments/flows be triggered b...
# marvin-ai
c
@Marvin: Can deployments/flows be triggered by file system events, such as a file landing in a directory?
m
thought for 117.3 seconds
Short answer: not natively from the filesystem itself. In Prefect 3.x, there isn't a built-in trigger like “watch this directory and start a deployment when a file appears.” But you can do it with a few common patterns: Best options 1. Poll for files on a schedule - Run a deployment every N minutes - The flow checks a directory for new files - Simplest approach 2. Use an external file watcher and trigger Prefect - Use something like
watchdog
,
inotify
, or another filesystem watcher - When a file lands, call Prefect to create a flow run for a deployment - Best if you want near-real-time behavior 3. Emit a custom Prefect event, then use an automation - Your watcher script detects the file - It emits a custom event like
file.landed
- A Prefect automation triggers the deployment from that event - Nice if you want to stay within Prefect’s event/automation model A simple mental model is:
Copy code
text
filesystem watcher/poller -> Prefect event or deployment run request -> flow starts
If you want the easiest setup, I’d recommend a scheduled deployment that polls the directory. If you want true event-driven behavior, I’d recommend an external watcher that either: - calls the deployment run API directly, or - emits a custom event that an automation listens for Example watcher pattern in Python:
Copy code
python
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import asyncio
from prefect.client.orchestration import get_client

DEPLOYMENT_ID = "your-deployment-id"

class Handler(FileSystemEventHandler):
    def on_created(self, event):
        if event.is_directory:
            return
        if event.src_path.endswith(".csv"):
            asyncio.run(trigger_deployment(event.src_path))

async def trigger_deployment(path):
    async with get_client() as client:
        await client.create_flow_run_from_deployment(
            deployment_id=DEPLOYMENT_ID,
            parameters={"file_path": path},
        )

observer = Observer()
observer.schedule(Handler(), "/data/incoming", recursive=False)
observer.start()
observer.join()
A few relevant docs: - Deployments - Automations - Events - Prefect events Python API If you want, I can give you: - a polling flow example - an event-driven automation example - or a full local-directory watcher → Prefect deployment example
upvote 1