Skip to content

Commit

Permalink
Add health check script/workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Jun 9, 2024
1 parent ba4a9ac commit 9644a1d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/health_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Bang Health Check

on:
schedule:
# Schedule to run at 15:00 UTC first day of every month
- cron: "0 15 1 * *"
workflow_dispatch:

jobs:
health_check:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
bundler-cache: true

- name: Run health check
run: |
bundle exec ruby scripts/health_check.rb
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gem "json"
gem "rspec", "~> 3.13"
gem 'addressable', '~> 2.8', '>= 2.8.6'
gem "addressable", "~> 2.8", ">= 2.8.6"

gem "terminal-table", "~> 3.0"
gem "whirly"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ GEM
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.5.0)
whirly (0.3.0)
unicode-display_width (>= 1.1)

PLATFORMS
ruby
Expand All @@ -32,6 +34,7 @@ DEPENDENCIES
json
rspec (~> 3.13)
terminal-table (~> 3.0)
whirly

BUNDLED WITH
2.5.4
54 changes: 54 additions & 0 deletions scripts/health_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require "json"
require "net/http"
require "uri"
require "whirly"

bangs_json = JSON.parse(File.read("data/bangs.json"))
kagi_bangs_json = JSON.parse(File.read("data/kagi_bangs.json"))
assist_bangs_json = JSON.parse(File.read("data/assistant_bangs.json"))

errored = []
mutex = Mutex.new

def check_url(bang, errored, mutex)
return if bang["u"].start_with?("/")

bang_url = bang["u"].gsub("{{{s}}}", "example").strip
url = URI.parse(bang_url)

Net::HTTP.get_response(url)
rescue => e
mutex.synchronize { errored << [bang, e] }
# puts "#{bang_url} is offline (#{bang["s"]}). Error: \n #{e}"
end

Whirly.configure spinner: "dots"
Whirly.start

all_bangs = bangs_json + kagi_bangs_json + assist_bangs_json
threads = []

all_bangs.each_with_index do |bang, idx|
Whirly.status = "Bang #{bang["s"]} (#{idx + 1}/#{all_bangs.size})"

threads << Thread.new { check_url(bang, errored, mutex) }

# Limit the number of concurrent threads to avoid overwhelming the system
if threads.size >= 100
threads.each(&:join)
threads.clear
end
end

# Ensure all remaining threads are completed
threads.each(&:join)

Whirly.stop

errored.each do |error|
bang, e = error

puts "#{bang["u"]} is offline. Error: \n #{e}"
end

exit 1 if errored.size > 0

0 comments on commit 9644a1d

Please sign in to comment.