Skip to content

Commit

Permalink
fix: Use hashes to compare hubble.sh upgrade (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 authored Sep 7, 2023
1 parent f5f3756 commit 93e43a8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-fireants-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

fix: Use hashes to compare upgrade 'hubble.sh' versions
8 changes: 8 additions & 0 deletions apps/hubble/grafana/grafana-dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@
{
"color": "red",
"value": 0
},
{
"color": "green",
"value": 1
}
]
}
Expand Down Expand Up @@ -793,6 +797,10 @@
{
"color": "red",
"value": 0
},
{
"color": "green",
"value": 1
}
]
}
Expand Down
24 changes: 17 additions & 7 deletions apps/hubble/src/storage/jobs/pruneMessagesJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class PruneMessagesJobScheduler {

start(cronSchedule?: string) {
this._cronTask = cron.schedule(cronSchedule ?? DEFAULT_PRUNE_MESSAGES_JOB_CRON, () => this.doJobs());

// Log the DB Size at startup
setTimeout(() => {
this.logDbSize();
}, 1000);
}

stop() {
Expand All @@ -36,6 +41,15 @@ export class PruneMessagesJobScheduler {
return this._cronTask ? "started" : "stopped";
}

async logDbSize() {
this._engine
.getDb()
.approximateSize()
.then((size) => {
statsd().gauge("rocksdb.approximate_size", size || 0);
});
}

async doJobs(): HubAsyncResult<void> {
if (this._running) {
log.info({}, "prune messages job already running, skipping");
Expand Down Expand Up @@ -66,14 +80,10 @@ export class PruneMessagesJobScheduler {
} while (!finished);

log.info({ timeTakenMs: Date.now() - start }, "finished prune messages job");
this._engine
.getDb()
.approximateSize()
.then((size) => {
statsd().gauge("rocksdb.approximate_size", size || 0);
});

this._running = false;

this.logDbSize();

return ok(undefined);
}
}
32 changes: 23 additions & 9 deletions scripts/hubble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# itself in the process.

# Define the version of this script
CURRENT_VERSION="2"
CURRENT_VERSION="3"

REPO="farcasterxyz/hub-monorepo"
RAWFILE_BASE="https://raw.githubusercontent.com/$REPO"
Expand Down Expand Up @@ -81,22 +81,36 @@ self_upgrade() {
tmp_file=$(mktemp)
fetch_file_from_repo "$SCRIPT_FILE_PATH" "$tmp_file"

local latest_version
latest_version=$(awk -F'="' '/^CURRENT_VERSION=/ { print $2 }' "$tmp_file" | tr -d '"')
local current_hash
local new_hash

# Compare the versions
if [[ "$latest_version" > "$CURRENT_VERSION" ]]; then
echo "Newer version found ($latest_version). Upgrading..."
# Determine the appropriate hash command to use
if command -v sha256sum > /dev/null; then
hash_cmd="sha256sum"
elif command -v shasum > /dev/null; then
hash_cmd="shasum -a 256"
else
echo "Error: No suitable hash command found."
exit 1
fi

# Get the hash of the current script and the new file
current_hash=$($hash_cmd "$0" | awk '{ print $1 }')
new_hash=$($hash_cmd "$tmp_file" | awk '{ print $1 }')

# Compare the hashes
if [[ "$new_hash" != "$current_hash" ]]; then
echo "New version found. Upgrading..."
mv "$tmp_file" "$0" # Overwrite the current script
chmod +x "$0" # Ensure the script remains executable
chmod +rx "$0" # Ensure the script remains executable
echo "✅ Upgrade complete. Restarting with new version..."
echo ""
exec "$0" "$1" || echo "Exec failed with status: $?"
exec "$0" "$@" || echo "Exec failed with status: $?"

# Exit the script because we already "exec"ed the script above
exit 0
else
echo "✅ Latest Script Version: $CURRENT_VERSION."
echo "✅ Latest Script Version."
rm -f "$tmp_file" # Clean up temporary file if no upgrade was needed
fi
}
Expand Down

0 comments on commit 93e43a8

Please sign in to comment.