I want to setup MySQL database health check for a ruby app, basically the response should be
{
"read_success": true,
"write_success": true,
"exception": null
}
My health check should perform the following operation:
Read a table from the database Write something to Database If any of the above operation fails, it should throw the exception as mention in the response.
module API
ApplicationName.controllers :health_check do
get :index do
status = {
read_success: read_successful,
write_success: write_successful,
exception: check_exception
}
[200, {}, [status.to_json]]
end
end
end
def read_successful
begin
ActiveRecord::Base.connection.execute("SELECT 1")
true
rescue
false
end
end
def write_successful
begin
# logic to check write to database, which table should i write to?
true
rescue
false
end
end
def check_exception
begin
ActiveRecord::Base.connection.execute("SELECT 1")
nil
rescue Exception => e
return e.message
end
begin
# logic to check write to database, which table should i write to?
nil
rescue Exception => e
return e.message
end
end
I have tried implementing read health checks as above but dont know how to implement write health checks? Is there any way to implement write health check without creating a new table for healthcheck in the database.
What should be my logic to implement write health check?