fix: use sh -c with inline release-slot.sh for fire-and-forget delegation #47
@@ -7,6 +7,58 @@ WORKTREES_DIR="$KUGETSU_DIR/worktrees"
|
||||
REPOS_CONFIG="$KUGETSU_DIR/repos.json"
|
||||
INDEX_FILE="$KUGETSU_DIR/index.json"
|
||||
NOTIFICATIONS_FILE="$KUGETSU_DIR/notifications.json"
|
||||
LOGS_DIR="$KUGETSU_DIR/logs"
|
||||
MAX_CONCURRENT_AGENTS="${MAX_CONCURRENT_AGENTS:-5}"
|
||||
AGENT_COUNT_FILE="$KUGETSU_DIR/.agent_count"
|
||||
AGENT_LOCK_FILE="$KUGETSU_DIR/.agent_lock"
|
||||
|
||||
acquire_agent_slot() {
|
||||
local timeout="${1:-300}"
|
||||
local waited=0
|
||||
(
|
||||
flock -w 1 200 || { echo "Error: Could not acquire lock" >&2; exit 1; }
|
||||
local count
|
||||
count=$(cat "$AGENT_COUNT_FILE" 2>/dev/null || echo 0)
|
||||
if [ "$count" -lt "$MAX_CONCURRENT_AGENTS" ]; then
|
||||
echo $((count + 1)) > "$AGENT_COUNT_FILE"
|
||||
exit 0
|
||||
fi
|
||||
exit 1
|
||||
) 200>"$AGENT_LOCK_FILE"
|
||||
local result=$?
|
||||
if [ $result -ne 0 ]; then
|
||||
local count
|
||||
count=$(cat "$AGENT_COUNT_FILE" 2>/dev/null || echo 0)
|
||||
if [ $waited -ge $timeout ]; then
|
||||
echo "Error: Timeout waiting for agent slot (max: $MAX_CONCURRENT_AGENTS, current: $count)" >&2
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
release_agent_slot() {
|
||||
(
|
||||
flock -w 1 200 || true
|
||||
local count
|
||||
count=$(cat "$AGENT_COUNT_FILE" 2>/dev/null || echo 0)
|
||||
if [ "$count" -gt 0 ]; then
|
||||
echo $((count - 1)) > "$AGENT_COUNT_FILE"
|
||||
fi
|
||||
) 200>"$AGENT_LOCK_FILE"
|
||||
}
|
||||
|
||||
run_with_limit() {
|
||||
local log_file="$1"
|
||||
shift
|
||||
local cmd=("$@")
|
||||
(
|
||||
"${cmd[@]}" >> "$log_file" 2>&1
|
||||
release_agent_slot
|
||||
) &
|
||||
disown
|
||||
}
|
||||
>>>>>>> Stashed changes
|
||||
|
||||
usage() {
|
||||
cat << 'EOF'
|
||||
|
||||
Reference in New Issue
Block a user