aboutsummaryrefslogtreecommitdiffstats
path: root/core/moduleapi.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2019-03-19 09:04:40 +0000
committerMatthew Wild <mwild1@gmail.com>2019-03-19 09:04:40 +0000
commitdda9c3ad7236b58675d66b7eb23bfb969f9c90d0 (patch)
tree61d65193c9fde14d16eebd913d03568098fb4448 /core/moduleapi.lua
parent8999b2a97c8d5e0d9dcf10d7692f928287d6bc5e (diff)
downloadprosody-dda9c3ad7236b58675d66b7eb23bfb969f9c90d0.tar.gz
prosody-dda9c3ad7236b58675d66b7eb23bfb969f9c90d0.zip
moduleapi: New API for modules to set a status
Diffstat (limited to 'core/moduleapi.lua')
-rw-r--r--core/moduleapi.lua31
1 files changed, 31 insertions, 0 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index c6193cfd..2db7433a 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -17,6 +17,8 @@ local st = require "util.stanza";
local cache = require "util.cache";
local errutil = require "util.error";
local promise = require "util.promise";
+local time_now = require "util.time".now;
+local format = require "util.format".format;
local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
local error, setmetatable, type = error, setmetatable, type;
@@ -513,4 +515,33 @@ function api:measure_global_event(event_name, stat_name)
return self:measure_object_event(prosody.events.wrappers, event_name, stat_name);
end
+local status_priorities = { error = 3, warn = 2, info = 1, core = 0 };
+
+function api:set_status(status_type, status_message, override)
+ local priority = status_priorities[status_type];
+ if not priority then
+ self:log("error", "set_status: Invalid status type '%s', assuming 'info'");
+ status_type, priority = "info", status_priorities.info;
+ end
+ local current_priority = status_priorities[self.status_type] or 0;
+ -- By default an 'error' status can only be overwritten by another 'error' status
+ if (current_priority >= status_priorities.error and priority < current_priority and override ~= true)
+ or (override == false and current_priority > priority) then
+ self:log("debug", "Ignoring status");
+ return;
+ end
+ self.status_type, self.status_message, self.status_time = status_type, status_message, time_now();
+ self:log("debug", "New status: %s", status_type);
+ self:fire_event("module-status/updated", { name = self.name });
+end
+
+function api:log_status(level, msg, ...)
+ self:set_status(level, format(msg, ...));
+ return self:log(level, msg, ...);
+end
+
+function api:get_status()
+ return self.status_type, self.status_message, self.status_time;
+end
+
return api;