class
Sepia::Watcher
- Sepia::Watcher
- Reference
- Object
Overview
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.
Defined in:
sepia/watcher.crConstructors
Class Method Summary
- .add_internal_file(path : String) : Nil
-
.internal_file?(path : String) : Bool
Class methods for internal file tracking
- .remove_internal_file(path : String) : Nil
Instance Method Summary
-
#callback : Sepia::Event -> Nil?
Alias for callback_block to match spec expectations
-
#callback_block : Event -> | Nil
Callback block for event handling
-
#event_count : Int32
Get the number of events processed (for debugging)
-
#on_change(&block : Event -> )
Register a callback to be called when events occur
-
#path_resolver : PathResolver
Path resolver for converting file paths to Sepia object information
-
#running : Bool
Whether the watcher is currently running
-
#running=(running : Bool)
Whether the watcher is currently running
-
#running? : Bool
Check if the watcher is currently running
-
#start
Start watching the storage directory for changes
-
#stop
Stop watching for file system changes
-
#storage : FileStorage
Storage backend being watched
Constructor Detail
Class Method Detail
Instance Method Detail
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
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"