From 1af8687a223916be1fb59a3b3308eef0042f60a2 Mon Sep 17 00:00:00 2001 From: Riccardo Graziosi <31478034+riggraz@users.noreply.github.com> Date: Tue, 23 May 2023 17:19:08 +0200 Subject: [PATCH] Add healthcheck endpoint and task (#230) --- config/routes.rb | 6 ++++++ lib/tasks/healthcheck.rake | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/tasks/healthcheck.rake diff --git a/config/routes.rb b/config/routes.rb index 0158e066..120f4477 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,4 +56,10 @@ Rails.application.routes.draw do get 'authentication' end end + + # Healthcheck endpoint + get '/health', to: proc { + Tenant.first # to make sure db works + [200, {}, ['success']] + } end diff --git a/lib/tasks/healthcheck.rake b/lib/tasks/healthcheck.rake new file mode 100644 index 00000000..c50d4248 --- /dev/null +++ b/lib/tasks/healthcheck.rake @@ -0,0 +1,20 @@ +require 'rake' +require 'net/http' +require 'uri' + +task :healthcheck, [:port] do |t, args| + port = args[:port] || 3000 + + # Make the HTTP request to the health endpoint + uri = URI.parse("http://localhost:#{port}/health") + response = Net::HTTP.get_response(uri) + + exit_code = response.is_a?(Net::HTTPSuccess) ? 0 : 1 + +rescue + exit_code = 1 + +ensure + puts "Healthcheck returned: #{exit_code.to_s}" + exit(exit_code) +end