Skip to content

Commit

Permalink
Rework how VSCode extension maintain its backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Aug 13, 2024
1 parent 0877781 commit 4297f6a
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 505 deletions.
4 changes: 2 additions & 2 deletions repair
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ suibase_repair() {
if [ -z "$SUIBASE_DAEMON_VERSION_FILE" ]; then
# shellcheck source=SCRIPTDIR/scripts/common/__suibase-daemon.sh
source "$REPAIR_SB_DIR/scripts/common/__suibase-daemon.sh"
update_suibase_daemon_as_needed
start_suibase_daemon_as_needed
fi

if [ -z "$DTP_DAEMON_VERSION_FILE" ]; then
# shellcheck source=SCRIPTDIR/scripts/common/__dtp-daemon.sh
source "$REPAIR_SB_DIR/scripts/common/__dtp-daemon.sh"
update_dtp_daemon_as_needed
start_dtp_daemon_as_needed
fi

# Exit instruction to the user.
Expand Down
25 changes: 14 additions & 11 deletions restart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Source '__globals.sh'.
SUIBASE_DIR="$HOME/suibase"
SCRIPT_COMMON_CALLER="$(readlink -f "$0")"
WORKDIR="localnet"
WORKDIR="none"

main() {
# Detect if suibase is not installed!
Expand All @@ -27,16 +27,19 @@ main() {
exit_if_deps_missing

# Block users from running any concurrent CLI commands.
cli_mutex_lock "localnet"
cli_mutex_lock "mainnet"
cli_mutex_lock "devnet"
cli_mutex_lock "testnet"
cli_mutex_lock "cargobin"
cli_mutex_lock "active"

restart_suibase_daemon
# Check for any JSON-RPC up, except for localnet.
wait_for_json_rpc_up "exclude-localnet"
cli_mutex_lock "suibase_daemon"


update_SUIBASE_DAEMON_PID_var

local _OLD_PID=$SUIBASE_DAEMON_PID
start_suibase_daemon_as_needed

if [ "$_OLD_PID" == "$SUIBASE_DAEMON_PID" ]; then
restart_suibase_daemon
# Check for any JSON-RPC up, except for localnet.
wait_for_json_rpc_up "exclude-localnet"
fi
}

main "$@"
53 changes: 41 additions & 12 deletions scripts/common/__globals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,33 @@ export SUIBASE_CLI_LOCK_ACQUIRED_TESTNET=0
export SUIBASE_CLI_LOCK_ACQUIRED_MAINNET=0
export SUIBASE_CLI_LOCK_ACQUIRED_CARGOBIN=0
export SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE=0
export SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON=0
export SUIBASE_CLI_LOCK_DISABLED=0

# Disable CLI mutex mechanism.
#
# Useful for when a script is called by a script that already acquired the necessary lock.
# In that case, the child script should not use any of the mutex.
cli_mutex_disable() {
SUIBASE_CLI_LOCK_DISABLED=1
}
export -f cli_mutex_disable

# Allow to disable a lock. Used for
cleanup() {
# echo "Cleanup called"
# Clear progress files created by this script.
if [ "$SUIBASE_DAEMON_UPGRADING" == "1" ]; then
if [ "$SUIBASE_DAEMON_UPGRADING" == "true" ]; then
rm -f /tmp/.suibase/suibase-daemon-upgrading >/dev/null 2>&1
fi

# Associative arrays are not working for the trap. bash limitation?
# Did workaround by painfully defining variables for each workdir.
if [ "$SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE" == "1" ]; then
cli_mutex_release "active"
SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE=0
fi

if [ "$SUIBASE_CLI_LOCK_ACQUIRED_LOCALNET" == "1" ]; then
cli_mutex_release "localnet"
SUIBASE_CLI_LOCK_ACQUIRED_LOCALNET=0
Expand All @@ -190,11 +207,10 @@ cleanup() {
SUIBASE_CLI_LOCK_ACQUIRED_CARGOBIN=0
fi

if [ "$SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE" == "1" ]; then
cli_mutex_release "active"
SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE=0
if [ "$SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON" == "1" ]; then
cli_mutex_release "suibase_daemon"
SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON=0
fi

}

# Add color
Expand Down Expand Up @@ -384,8 +400,7 @@ try_locked_command() {
export -f try_locked_command


cli_mutex_lock()
{
cli_mutex_lock() {
# mutex is re-entrant (only first call to cli_mutex_lock will acquire the lock).

# Design choice:
Expand All @@ -404,6 +419,10 @@ cli_mutex_lock()
#
# Worst case, because the lock is in /tmp, a stale lock can be workaround with a reboot...

if [ "$SUIBASE_CLI_LOCK_DISABLED" == "1" ]; then
return
fi

local _WORKDIR=$1

if [ -z "$_WORKDIR" ]; then
Expand Down Expand Up @@ -433,6 +452,9 @@ cli_mutex_lock()
active)
_IS_ACQUIRED=$SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE
;;
suibase_daemon)
_IS_ACQUIRED=$SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON
;;
*)
setup_error "Internal error. cli_mutex_lock called with an unknown workdir $_WORKDIR"
;;
Expand Down Expand Up @@ -467,12 +489,13 @@ cli_mutex_lock()
active)
SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE=1
;;
suibase_daemon)
SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON=1
;;
*)
setup_error "Internal error. cli_mutex_lock called with an unknown workdir $_WORKDIR"
;;
esac

# echo "Lock acquired for $_WORKDIR"
}
export -f cli_mutex_lock

Expand Down Expand Up @@ -506,8 +529,11 @@ cli_mutex_release()
active)
_IS_ACQUIRED=$SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE
;;
suibase_daemon)
_IS_ACQUIRED=$SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON
;;
*)
setup_error "Internal error. cli_mutex_lock called with an unknown workdir $_WORKDIR"
setup_error "Internal error. cli_mutex_release called with an unknown workdir $_WORKDIR"
;;
esac
if [ "$_IS_ACQUIRED" == "0" ]; then
Expand Down Expand Up @@ -537,6 +563,9 @@ cli_mutex_release()
active)
SUIBASE_CLI_LOCK_ACQUIRED_ACTIVE=0
;;
suibase_daemon)
SUIBASE_CLI_LOCK_ACQUIRED_SUIBASE_DAEMON=0
;;
esac

#echo "Lock released for $_WORKDIR"
Expand Down Expand Up @@ -3327,9 +3356,9 @@ has_param() {
}
export -f has_param

export SUIBASE_DAEMON_UPGRADING=0
export SUIBASE_DAEMON_UPGRADING=false
progress_suibase_daemon_upgrading() {
SUIBASE_DAEMON_UPGRADING=1
SUIBASE_DAEMON_UPGRADING=true
mkdir -p "$SUIBASE_TMP_DIR"
touch "$SUIBASE_TMP_DIR/suibase-daemon-upgrading"
}
Expand Down
Loading

0 comments on commit 4297f6a

Please sign in to comment.