class Sepia::Watcher

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:

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.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(storage : FileStorage) #

[View source]

Class Method Detail

def self.add_internal_file(path : String) : Nil #

[View source]
def self.internal_file?(path : String) : Bool #

Class methods for internal file tracking


[View source]
def self.remove_internal_file(path : String) : Nil #

[View source]

Instance Method Detail

def callback : Sepia::Event -> Nil? #

Alias for callback_block to match spec expectations


[View source]
def callback_block : Event -> | Nil #

Callback block for event handling


def event_count : Int32 #

Get the number of events processed (for debugging)


[View source]
def on_change(&block : Event -> ) #

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

[View source]
def path_resolver : PathResolver #

Path resolver for converting file paths to Sepia object information


def running : Bool #

Whether the watcher is currently running


def running=(running : Bool) #

Whether the watcher is currently running


def running? : Bool #

Check if the watcher is currently running


[View source]
def start #

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..."

[View source]
def stop #

Stop watching for file system changes

This stops the background monitoring.

watcher.stop
puts "Watcher stopped"

[View source]
def storage : FileStorage #

Storage backend being watched