Comparison

core/moduleapi.lua @ 9866:09cc8c856e5e

moduleapi: New API for modules to set a status
author Matthew Wild <mwild1@gmail.com>
date Tue, 19 Mar 2019 09:04:40 +0000
parent 9750:65432dc80d90
child 9929:3b776c32b9be
comparison
equal deleted inserted replaced
9865:37278b420c74 9866:09cc8c856e5e
15 local resolve_relative_path = require"util.paths".resolve_relative_path; 15 local resolve_relative_path = require"util.paths".resolve_relative_path;
16 local st = require "util.stanza"; 16 local st = require "util.stanza";
17 local cache = require "util.cache"; 17 local cache = require "util.cache";
18 local errutil = require "util.error"; 18 local errutil = require "util.error";
19 local promise = require "util.promise"; 19 local promise = require "util.promise";
20 local time_now = require "util.time".now;
21 local format = require "util.format".format;
20 22
21 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; 23 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
22 local error, setmetatable, type = error, setmetatable, type; 24 local error, setmetatable, type = error, setmetatable, type;
23 local ipairs, pairs, select = ipairs, pairs, select; 25 local ipairs, pairs, select = ipairs, pairs, select;
24 local tonumber, tostring = tonumber, tostring; 26 local tonumber, tostring = tonumber, tostring;
511 513
512 function api:measure_global_event(event_name, stat_name) 514 function api:measure_global_event(event_name, stat_name)
513 return self:measure_object_event(prosody.events.wrappers, event_name, stat_name); 515 return self:measure_object_event(prosody.events.wrappers, event_name, stat_name);
514 end 516 end
515 517
518 local status_priorities = { error = 3, warn = 2, info = 1, core = 0 };
519
520 function api:set_status(status_type, status_message, override)
521 local priority = status_priorities[status_type];
522 if not priority then
523 self:log("error", "set_status: Invalid status type '%s', assuming 'info'");
524 status_type, priority = "info", status_priorities.info;
525 end
526 local current_priority = status_priorities[self.status_type] or 0;
527 -- By default an 'error' status can only be overwritten by another 'error' status
528 if (current_priority >= status_priorities.error and priority < current_priority and override ~= true)
529 or (override == false and current_priority > priority) then
530 self:log("debug", "Ignoring status");
531 return;
532 end
533 self.status_type, self.status_message, self.status_time = status_type, status_message, time_now();
534 self:log("debug", "New status: %s", status_type);
535 self:fire_event("module-status/updated", { name = self.name });
536 end
537
538 function api:log_status(level, msg, ...)
539 self:set_status(level, format(msg, ...));
540 return self:log(level, msg, ...);
541 end
542
543 function api:get_status()
544 return self.status_type, self.status_message, self.status_time;
545 end
546
516 return api; 547 return api;