Observers
Observers
Observers are powerful components in Raindrop that let you execute code in response to changes in your resources. Think of them as event listeners that automatically trigger when specific conditions are met in your application.
Types of Observers
Raindrop provides two types of observers to help you build reactive applications:
- Object Observers: Monitor changes to objects in buckets
- Queue Observers: Process messages as they arrive in queues
Object Observers
Object observers watch for changes to objects in your buckets and execute code in response. They’re particularly useful when you need to process files after upload.
To create an object observer, add it to your raindrop.manifest
.
application "demo" { // First define your bucket bucket "observed-bucket" {}
// Then create an observer observer "my-observer" { source { bucket = "observed-bucket" rule { // Specify which actions should trigger the observer actions = ["PutObject", "CompleteMultipartUpload", "CopyObject"] } } }}
import { Each } from '@liquidmetal-ai/raindrop-framework';import { Env } from './raindrop.gen';
export default class extends Each<Body, Env> { async process(message: observers.BucketEventNotification): Promise<void> { console.log(`received message: ${JSON.stringify(message)}`); }}
Supported Trigger Actions
Your observer can respond to the following bucket events:
Object Creation Events
PutObject
- Triggers when objects are uploaded directlyCopyObject
- Triggers when objects are copiedCompleteMultipartUpload
- Triggers when multipart uploads finish
Object Deletion Events
DeleteObject
- Triggers on manual object deletionLifecycleDeletion
- Triggers when objects are deleted by lifecycle rules
Queue Observers
Queue observers process messages as they arrive in a queue. They’re the perfect solution for handling asynchronous tasks and managing background jobs that shouldn’t block your main application flow.
Setting up a queue observer is straightforward. First define your queue, then create an observer that watches it.
// Define the queuequeue "observed-queue" {}
// Create the queue observerobserver "queue-observer" { source { queue = "observed-queue" }}
import { Each } from '@liquidmetal-ai/raindrop-framework';import { Env } from './raindrop.gen';
export default class extends Each<Body, Env> { async process(message: Body): Promise<void> { console.log(JSON.stringify(`received message: ${message}`)); }}export interface Body {}