From c3d88f95f8b022def65e95b9584e6a853ffaf1b3 Mon Sep 17 00:00:00 2001 From: Dovie Weinstock Date: Thu, 26 Dec 2024 15:47:44 -0500 Subject: [PATCH] add parralel vault syncing #2 --- bashrc | 1 - setup | 3 +-- sync-vaults.sh | 50 ++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/bashrc b/bashrc index 724331b..9c5f833 100644 --- a/bashrc +++ b/bashrc @@ -14,7 +14,6 @@ export SCRIPTS_REPO_PATH="$REPOS_PATH/$SCRIPTS_REPO" export OBSIDIAN_DIR="Obsidian" export OBSIDIAN_DIR_PATH="$REPOS_PATH/$OBSIDIAN_DIR" export NOTIFICATION_PATH="$STORAGE_PATH/sync-error-notification" -export LAST_SYNC_PATH="$HOME/last_sync.log" alias sync="$HOME/sync-vaults.sh --skip-pause" alias bashrc="nano /data/data/com.termux/files/usr/etc/bash.bashrc" diff --git a/setup b/setup index 578aab8..3795873 100644 --- a/setup +++ b/setup @@ -70,7 +70,7 @@ fi if [ ! -f ~/.ssh/id_rsa ]; then echo -e "${BLUE}Creating an SSH key...${RESET}" - ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa > /dev/null + ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa > /dev/null # #TODO: use better crypto? if cat ~/.ssh/id_rsa.pub | termux-clipboard-set; then echo -e "${BLUE}SSH public key has been copied to your clipboard.${RESET}" @@ -145,4 +145,3 @@ touch $NOTIFICATION_PATH cd $OBSIDIAN_DIR_PATH echo -e "\n${GREEN}Setup complete${RESET}\n" - diff --git a/sync-vaults.sh b/sync-vaults.sh index 0bed97e..083db93 100644 --- a/sync-vaults.sh +++ b/sync-vaults.sh @@ -35,14 +35,20 @@ skip_pause_val="--skip-pause" source "$HOME/log_helper.sh" log_file="$HOME/sync.log" -setup_logging $log_file +setup_logging "$log_file" + +# Create directory to store individual sync logs +temp_dir="${HOME}/obsidian_sync_$$" +mkdir -p "$temp_dir" cmd () { printf "\n\033[0;34m%s\033[0m\n" "$(basename "$PWD")" - $HOME/git-sync -ns 2>&1 | tee $LAST_SYNC_PATH - + # We run git-sync and capture output. In a parallel scenario, + # we redirect to a file, so we won't "live watch" it in this function. + $HOME/git-sync -ns 2>&1 if [ $? -ne 0 ]; then - cat $LAST_SYNC_PATH >> $NOTIFICATION_PATH # Send notifcation + # If there was an error, append the output to NOTIFICATION_PATH + cat "$1" >> "$NOTIFICATION_PATH" fi } @@ -58,7 +64,7 @@ for dir in "$OBSIDIAN_DIR_PATH"/*; do fi done -msg="You can try running 'setup' to see if it helps". +msg="You can try running 'setup' to see if it helps." # Exit if no Git repos are found if [ ${#git_repos[@]} -eq 0 ]; then @@ -68,23 +74,47 @@ fi if [[ -n "$1" && "$1" != "$skip_pause_val" ]]; then # Sync a single repo if [[ " ${git_repos[@]} " =~ " $OBSIDIAN_DIR_PATH/$1 " ]]; then - (cd "$OBSIDIAN_DIR_PATH/$1" && cmd) + repo="$OBSIDIAN_DIR_PATH/$1" + repo_name="$(basename "$repo")" + tmp_log="$temp_dir/${repo_name}.log" + (cd "$repo" && cmd "$tmp_log" > "$tmp_log" 2>&1) + cat "$tmp_log" >> "$log_file" else echo -e "${RED}Specified directory doesn't exist or is not a Git repository.\n${msg}${RESET}" + rm -f "$LOCK_FILE" exit 1 fi -else # Sync all Git repos +else + # Sync all Git repos in parallel + pids=() for repo in "${git_repos[@]}"; do - (cd "$repo" && cmd) + repo_name="$(basename "$repo")" + tmp_log="$temp_dir/${repo_name}.log" + + ( + cd "$repo" && cmd "$tmp_log" > "$tmp_log" 2>&1 + ) & + pids+=($!) done + + # Wait for all background syncs to complete + for pid in "${pids[@]}"; do + wait "$pid" + done + + # Append all temp logs to the main log file + cat "$temp_dir"/*.log >> "$log_file" fi -log_cleanup $log_file +# Cleanup temp logs +rm -rf "$temp_dir" +log_cleanup "$log_file" + +# Pause if no repo was specified and skip_pause was not used if [[ -z "$1" ]]; then bypass_log "echo -e '\n\033[44;97mPress enter to finish...\033[0m' && read none" fi - rm -f "$LOCK_FILE" exit 0