Skip to content

Commit

Permalink
Opened files on the host are not visible by lsof inside the docker. I…
Browse files Browse the repository at this point in the history
…nstead, check that file properties remain stable for a specified amount of time.
  • Loading branch information
jlesage committed May 24, 2017
1 parent 2d4c30c commit 72fac73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ RUN sed -i 's/<application type="normal">/<application type="normal" title="Hand
RUN \
apk --no-cache add \
# For watchfolder
findutils \
lsof
findutils

# Generate and install favicons.
RUN \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ of this parameter has the format `<VARIABLE_NAME>=<VALUE>`.
|`AUTOMATED_CONVERSION_PRESET`| HandBrake preset used by the automatic video converter. See the [Automatic Video Conversion](#automatic-video-conversion) section for more details. | "Very Fast 1080p30" |
|`AUTOMATED_CONVERSION_FORMAT`| Video container format used by the automatic video converter for output files. This is typically the video filename extension. See the [Automatic Video Conversion](#automatic-video-conversion) section for more details. | "mp4" |
|`AUTOMATED_CONVERSION_KEEP_SOURCE`| When set to `0`, a video that has been successfully converted is removed from the watch folder. | 1 |
|`AUTOMATED_CONVERSION_SOURCE_STABLE_TIME`| Time during which properties (e.g. size, time, etc) of a video file in the watch folder need to remain the same. This is to avoid processing a file that is being copied. | 5 |
|`HANDBRAKE_DEBUG`| Setting this to `1` enables HandBrake debug logging. Log messages are sent to `/config/handbrake.debug.log` (container path). **NOTE**: When enabled, a lot of information is generated and the log file will grow quickly. Make sure to enable this temporarily and only when needed. | (unset) |

[TimeZone]: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
Expand Down
31 changes: 16 additions & 15 deletions rootfs/etc/services.d/watchfolder/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -u # Treat unset variables as an error.

AUTOMATED_CONVERSION_PRESET="${AUTOMATED_CONVERSION_PRESET:-Very Fast 1080p30}"
AUTOMATED_CONVERSION_FORMAT="${AUTOMATED_CONVERSION_FORMAT:-mp4}"
AUTOMATED_CONVERSION_SOURCE_STABLE_TIME="${AUTOMATED_CONVERSION_SOURCE_STABLE_TIME:-5}"

FAILED_CONVERSIONS="/config/failed_conversions"
SUCCESSFUL_CONVERSIONS="/config/successful_conversions"
Expand All @@ -28,14 +29,15 @@ WATCHDIR_HASH_update() {
WATCHDIR_HASH="$(WATCHDIR_HASH_calculate)"
}

WATCHDIR_HASH_invalidate() {
WATCHDIR_HASH=INVALID
}

WATCHDIR_HASH_changed() {
[ "$WATCHDIR_HASH" != "$(WATCHDIR_HASH_calculate)" ]
}

get_hash() {
file="$1"
stat -c '%n %s %Y' "$file" | md5sum | cut -d' ' -f1
}

process_file() {
file="$1"

Expand All @@ -46,17 +48,8 @@ process_file() {
return
fi

# Skip file if it is being accessed (may be currently being copied).
if lsof -f -- "$file" &>/dev/null; then
log "Skipping file '$file': currently being accessed."
# Invalidate the watch directory hash. If we don't, we may never
# process this file because the hash may remain the same.
WATCHDIR_HASH_invalidate
return
fi

# Get hash of the file from its name, size and time.
hash="$(stat -c '%n %s %Y' "$file" | md5sum | cut -d' ' -f1)"
# Get hash of the file from its properties.
hash="$(get_hash "$file")"

# Skip file if it has been already successfully processed.
if grep -q -w "$hash" "$SUCCESSFUL_CONVERSIONS"; then
Expand All @@ -70,6 +63,14 @@ process_file() {
return
fi

# Skip file if it is not stable.
echo "Waiting $AUTOMATED_CONVERSION_SOURCE_STABLE_TIME seconds before processing '$file'..."
sleep $AUTOMATED_CONVERSION_SOURCE_STABLE_TIME
if [ "$hash" != "$(get_hash "$file")" ]; then
log "Skipping file '$file': currently being copied."
return
fi

log "Starting conversion of '$file' ($hash)..."
basename="$(basename "$file" | sed 's/\.[^.]*$//')"
$HANDBRAKE_CLI -i "$file" \
Expand Down

0 comments on commit 72fac73

Please sign in to comment.