Boxlang file watchers

From ortus-docs/coldbox-docs · View interactive version

Boxlang file watchers is an AI agent skill from the GitHub repository ortus-docs/coldbox-docs. Use this skill when implementing BoxLang filesystem watchers: watcherNew/watcherStart/watcherStop lifecycle BIFs, WatcherInstance APIs, event payload handling, recursive directory monitoring, debounce/throttle/atomicWrites tuning, errorThreshold auto-stop behavior, and listener patterns (closure, st

How to use this skill

Skill file

BoxLang File Watchers

Overview

BoxLang includes a runtime watcher service for real-time filesystem automation.

Watchers are ideal for:

Application.bx Integration

You can register app-scoped watchers directly in Application.bx with this.watchers.

For full descriptor behavior (app discovery, multi-app isolation, lifecycle), see

CODE0.

class {

    this.name = "MyApp"

    watcherListener = new app.listeners.HotReloadListener()

    this.watchers = {
        sourceWatcher : {
            paths : [ expandPath( "./src" ) ],
            listener : watcherListener,
            recursive : true,
            debounce : 250,
            atomicWrites : true,
            errorThreshold : 10
        }
    }

}

Supported watcher definition keys commonly include paths, listener, recursive, debounce, throttle, atomicWrites, and errorThreshold.

Event Model

Event kinds:

Listener payload struct keys:

Creating a Watcher

watcherNew() registers a watcher but does not automatically start it unless you chain .start() on the returned instance.

watcher = watcherNew(
    name = "sourceWatcher",
    paths = [ "./src" ],
    listener = ( event ) => {
        println( "[#event.kind#] #event.relativePath#" )
    },
    recursive = true,
    debounce = 250,
    throttle = 0,
    atomicWrites = true,
    errorThreshold = 10,
    force = false
).start()

Accepted paths forms:

Listener Forms

1) Closure / Lambda

listener = ( event ) => {
    if ( event.kind == "modified" ) {
        recompileAsset( event.path )
    }
}

2) Struct Listener

listener = {
    onCreate: ( event ) => onCreated( event ),
    onModify: ( event ) => onModified( event ),
    onDelete: ( event ) => onDeleted( event ),
    onOverflow: ( event ) => fullResync(),
    onEvent: ( event ) => writeLog( text: "Watcher event: #event.kind#", log: "async" )
}

3) Class Name String or Class Instance

watcherNew(
    name = "prodWatcher",
    paths = [ "/srv/import" ],
    listener = "app.listeners.ImportWatcher"
)
watcherNew(
    name = "prodWatcher",
    paths = [ "/srv/import" ],
    listener = new ImportWatcher()
)

For class listeners, implement onEvent( required struct event ) as the baseline and optionally:

Watcher BIFs

Lifecycle distinctions:

WatcherInstance API

watcherNew() and watcherGet() return WatcherInstance:

getStats() includes:

Tuning and Safety

Production Pattern

watcher = watcherNew(
    name = "inbound-files",
    paths = [ "/data/inbound" ],
    listener = new InboundFileListener(),
    recursive = true,
    debounce = 300,
    atomicWrites = true,
    errorThreshold = 5
).start()

if ( !watcher.isRunning() ) {
    watcher.restart()
}

Best Practices

Troubleshooting

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.