Skip to content

Commit

Permalink
feat: Adding healthcheck module with api and server status (#185)
Browse files Browse the repository at this point in the history
* feat: Adding healthcheck module with api and server status

* feat: Adding binary file info to healtcheck

* feat: Restoring format to init and api modules (tabs, parenthesis)
  • Loading branch information
gmatheu committed Jul 9, 2024
1 parent 891439a commit f6a2ef3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
51 changes: 47 additions & 4 deletions lua/codeium/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ local io = require("codeium.io")
local log = require("codeium.log")
local update = require("codeium.update")
local notify = require("codeium.notify")
local util = require("codeium.util")
local api_key = nil
local status = {
api_key_error = nil,
}

local function find_port(manager_dir, start_time)
local files = io.readdir(manager_dir)
Expand Down Expand Up @@ -39,29 +41,43 @@ end
local Server = {}
Server.__index = Server

function Server.check_status()
return status
end

function Server.load_api_key()
local json, err = io.read_json(config.options.config_path)
if err or type(json) ~= "table" then
if err == "ENOENT" then
-- Allow any UI plugins to load
local message = "please log in with :Codeium Auth"
status.api_key_error = message
vim.defer_fn(function()
notify.info("please log in with :Codeium Auth")
notify.info(message)
end, 100)
else
notify.info("failed to load the api key")
local message = "failed to load the api key"
status.api_key_error = message
notify.info(message)
end
api_key = nil
return
end

status.api_key_error = nil
api_key = json.api_key
end

function Server.save_api_key()
local _, result = io.write_json(config.options.config_path, {
api_key = api_key,
})
status.api_key_error = nil

if result then
notify.error("failed to save the api key", result)
local message = "failed to save the api key"
status.api_key_error = message
notify.error(message, result)
end
end

Expand Down Expand Up @@ -145,6 +161,8 @@ function Server:new()
local current_cookie = nil
local workspaces = {}
local healthy = false
local last_heartbeat = nil
local last_heartbeat_error = nil

local function request(fn, payload, callback)
local url = "http://127.0.0.1:" .. port .. "/exa.language_server_pb.LanguageServerService/" .. fn
Expand All @@ -158,8 +176,11 @@ function Server:new()
request("Heartbeat", {
metadata = get_request_metadata(),
}, function(_, err)
last_heartbeat = os.time()
last_heartbeat_error = nil
if err then
notify.warn("heartbeat failed", err)
last_heartbeat_error = err
else
healthy = true
end
Expand All @@ -170,6 +191,28 @@ function Server:new()
return healthy
end

function m.checkhealth(logger)
logger.info("Checking server status")
if m.is_healthy() then
logger.ok("Server is healthy on port: " .. port)
else
logger.warn("Server is unhealthy")
end

logger.info("Language Server binary: " .. update.get_bin_info().bin)

if last_heartbeat == nil then
logger.warn("No heartbeat executed")
else
logger.info("Last heartbeat: " .. os.date("%D %H:%M:%S", last_heartbeat))
if last_heartbeat_error ~= nil then
logger.error(last_heartbeat_error)
else
logger.ok("Heartbeat ok")
end
end
end

function m.start()
m.shutdown()

Expand Down
38 changes: 38 additions & 0 deletions lua/codeium/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local Server = require("codeium.api")

local M = {}

---@diagnostic disable-next-line: deprecated
local start = vim.health.start or vim.health.report_start
---@diagnostic disable-next-line: deprecated
local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
---@diagnostic disable-next-line: deprecated
local error = vim.health.error or vim.health.report_error
---@diagnostic disable-next-line: deprecated
local info = vim.health.info or vim.health.report_info
local health_logger = { ok = ok, info = info, warn = warn, error = error }

local checkhealth = nil

function M.check()
start("Codeium: checking Codeium server status")
local server_status = Server.check_status()
if server_status.api_key_error ~= nil then
error("API key not loaded: " .. server_status.api_key_error)
else
ok("API key properly loaded")
end

if checkhealth == nil then
warn("Codeium: checkhealth is not set")
return
end
checkhealth(health_logger)
end

function M.register(callback)
checkhealth = callback
end

return M
2 changes: 2 additions & 0 deletions lua/codeium/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function M.setup(options)
local Source = require("codeium.source")
local Server = require("codeium.api")
local update = require("codeium.update")
local health = require("codeium.health")
require("codeium.config").setup(options)

local s = Server:new()
Expand All @@ -13,6 +14,7 @@ function M.setup(options)
s.start()
end
end)
health.register(s.checkhealth)

vim.api.nvim_create_user_command("Codeium", function(opts)
local args = opts.fargs
Expand Down

0 comments on commit f6a2ef3

Please sign in to comment.