Skip to content

Commit

Permalink
perf: improve inotify watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschubek committed Nov 29, 2024
1 parent f62a2d7 commit c3a5e85
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/fswatcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Debounce time in seconds
DEBOUNCE_TIME=5

# Declare an associative array to track timers
declare -A file_timers

process_event() {
local file="$1"
# if just a regular file or folder edited -> scan it
# if if it is a dbmeta.yml file -> scan the whole folder by stripping the .dbmeta.yml suffix
if [[ "$file" == *".dbmeta.yml" ]]; then
file="${file%.dbmeta.yml}"
fi

php /var/www/html/worker.php --scan "$file"
}

# watch mounted /var/www/html/public for changes
inotifywait -q -m -r -e create,attrib,delete,modify,move --format '%w%f' /var/www/html/public | while read -r file; do

# Cancel any existing debounce timer for this path
if [[ -n "${file_timers[$file]}" ]]; then
# echo "Cancel indexing for $file due to new event (inotify)"
kill "${file_timers[$file]}" 2>/dev/null
fi

# echo "Queueing event for $file (inotify)"
# Start a new debounce timer for this path
(
sleep "$DEBOUNCE_TIME"
process_event "$file"
) &

# Record the PID of the background process
file_timers["$file"]=$!
done

0 comments on commit c3a5e85

Please sign in to comment.