aboutsummaryrefslogtreecommitdiffstats
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
commit23577330fd9826da26a2ab0a6a3f1d6b82e5dfb8 (patch)
tree61d65193c9fde14d16eebd913d03568098fb4448
parent34f85c79c0f3127ed03d10d8d94d1e9a152798ed (diff)
downloadprosody-23577330fd9826da26a2ab0a6a3f1d6b82e5dfb8.tar.gz
prosody-23577330fd9826da26a2ab0a6a3f1d6b82e5dfb8.zip
moduleapi: New API for modules to set a status
-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;