Sepia::Watcher
File system watcher for Sepia objects using crystal-fswatch.
This watcher monitors file system changes in a Sepia storage directory and emits events for created, modified, or deleted objects.
Basic Usage
storage = Sepia::FileStorage.new("./data")
watcher = Sepia::Watcher.new(storage)
# Register a callback to receive events
watcher.on_change do |event|
puts "Event: #{event.type} for #{event.object_class}:#{event.object_id}"
end
# Start watching (this is non-blocking)
watcher.start
# When done, stop watching
watcher.stop
Design Principles
This watcher uses crystal-fswatch for reliable file system monitoring:
- Cross-platform support (Linux, macOS, Windows)
- No hanging issues in spec environments
- Clean lifecycle management
- Thread-safe event handling
Path Structure
The watcher expects paths in the format: storage_path/ClassName/object_id
It will parse these paths and extract the class name and object ID for each event.
Constructors
Class methods
Instance methods
Callback block for event handling
Register a callback to be called when events occur
The callback will be called for each event that occurs while the watcher is running.
watcher.on_change do |event|
puts "Got event: #{event.type}"
end
Path resolver for converting file paths to Sepia object information
Whether the watcher is currently running
Whether the watcher is currently running
Start watching the storage directory for changes
This method is non-blocking and returns immediately. The actual file system monitoring happens in the background.
watcher.start
puts "Watcher started, continuing with other work..."
Stop watching for file system changes
This stops the background monitoring.
watcher.stop
puts "Watcher stopped"
Storage backend being watched