Skip to content

Commit

Permalink
Feat/fix appending files (#9)
Browse files Browse the repository at this point in the history
* Add tests and fix appending files

* update logs path

* put back log logic
  • Loading branch information
yujingz authored Aug 21, 2024
1 parent 75ff34d commit 6673beb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
25 changes: 25 additions & 0 deletions examples/watch-and-upload/tests/append-factory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# 检查参数数量
if [ "$#" -ne 2 ]; then
echo "用法: $0 <文件名> <时间间隔(秒)>"
exit 1
fi

FILE_NAME=$1
INTERVAL=$2

# 检查文件是否存在,如果不存在则创建
if [ ! -f "$FILE_NAME" ]; then
touch "$FILE_NAME"
echo "创建文件: $FILE_NAME"
fi

# 无限循环,每隔指定时间追加内容
counter=1
while true; do
echo "追加第 $counter 行内容 - $(date)" >>"$FILE_NAME"
echo "已追加第 $counter 行内容"
counter=$((counter + 1))
sleep "$INTERVAL"
done
20 changes: 13 additions & 7 deletions examples/watch-and-upload/watch-and-upload.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

trap 'echo "正在退出脚本..."; exit' INT TERM
set -o errexit
set -o nounset
set -o pipefail
Expand Down Expand Up @@ -32,8 +33,8 @@ if [ ! -d "$WATCH_DIR" ]; then
fi

# 定义日志文件
UPLOAD_LOGS="$WATCH_DIR/.UPLOAD_LOGS"
RECORD_LOGS="$WATCH_DIR/.RECORD_LOGS"
UPLOAD_LOGS="$HOME/.UPLOAD_LOGS"
RECORD_LOGS="$HOME/.RECORD_LOGS"

# 确保日志文件存在
touch "$UPLOAD_LOGS" "$RECORD_LOGS"
Expand Down Expand Up @@ -91,8 +92,8 @@ process_file() {
record_id=$(get_current_record_id)

if cocli record upload "$record_id" "$file"; then
sed -i "\|${file//\//\\/}|d" "$UPLOAD_LOGS"
echo "$(date +'%Y-%m-%d %H:%M:%S')|$file|$md5sum" >>"$UPLOAD_LOGS"
echo "已上传: $file"
else
echo "上传失败: $file" >&2
fi
Expand Down Expand Up @@ -135,10 +136,15 @@ main() {
initialize

echo "开始监控目录: $WATCH_DIR"
fswatch -0 -r -e "(/|^)\.[^/]*$" "$WATCH_DIR" | while read -d "" file; do
if [ -f "$file" ] && [[ "$(basename "$file")" != .* ]]; then
echo "正在处理 $file"
process_file "$file"
fswatch --event Created --event Updated --event MovedTo -0 -r \
-e "(/|^)\.[^/]*$" \
-e "/sed.*\.tmp$" \
"$WATCH_DIR" | while read -d "" event; do

echo $event
if [ -f "$event" ] && [[ "$(basename "$event")" != .* ]]; then
echo "正在处理 $event"
process_file "$event"
fi
done
}
Expand Down

0 comments on commit 6673beb

Please sign in to comment.