Software /
code /
prosody
Comparison
plugins/mod_mam/mod_mam.lua @ 8230:154852646095
mod_mam: Use a FIFO queue for scheduling archive expiry
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 12 Sep 2017 14:42:56 +0200 |
parent | 8207:8ea0484871e8 |
child | 8231:97a094fdf101 |
comparison
equal
deleted
inserted
replaced
8229:bf6f80f2d971 | 8230:154852646095 |
---|---|
53 end | 53 end |
54 | 54 |
55 local use_total = true; | 55 local use_total = true; |
56 | 56 |
57 local cleanup; | 57 local cleanup; |
58 | |
59 local function schedule_cleanup(username) | |
60 if cleanup and not cleanup[username] then | |
61 table.insert(cleanup, username); | |
62 cleanup[username] = true; | |
63 end | |
64 end | |
58 | 65 |
59 -- Handle prefs. | 66 -- Handle prefs. |
60 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) | 67 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) |
61 local origin, stanza = event.origin, event.stanza; | 68 local origin, stanza = event.origin, event.stanza; |
62 local user = origin.username; | 69 local user = origin.username; |
95 module:hook("iq-set/self/"..xmlns_mam..":query", function(event) | 102 module:hook("iq-set/self/"..xmlns_mam..":query", function(event) |
96 local origin, stanza = event.origin, event.stanza; | 103 local origin, stanza = event.origin, event.stanza; |
97 local query = stanza.tags[1]; | 104 local query = stanza.tags[1]; |
98 local qid = query.attr.queryid; | 105 local qid = query.attr.queryid; |
99 | 106 |
100 if cleanup then cleanup[origin.username] = true; end | 107 schedule_cleanup(origin.username); |
101 | 108 |
102 -- Search query parameters | 109 -- Search query parameters |
103 local qwith, qstart, qend; | 110 local qwith, qstart, qend; |
104 local form = query:get_child("x", "jabber:x:data"); | 111 local form = query:get_child("x", "jabber:x:data"); |
105 if form then | 112 if form then |
302 if ok then | 309 if ok then |
303 local clone_for_other_handlers = st.clone(stanza); | 310 local clone_for_other_handlers = st.clone(stanza); |
304 local id = ok; | 311 local id = ok; |
305 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); | 312 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); |
306 event.stanza = clone_for_other_handlers; | 313 event.stanza = clone_for_other_handlers; |
307 if cleanup then cleanup[store_user] = true; end | 314 schedule_cleanup(store_user); |
308 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id }); | 315 module:fire_event("archive-message-added", { origin = origin, stanza = clone_for_storage, for_user = store_user, id = id }); |
309 end | 316 end |
310 else | 317 else |
311 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); | 318 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); |
312 end | 319 end |
351 -- Iterating over users is not supported by all authentication modules | 358 -- Iterating over users is not supported by all authentication modules |
352 -- Catch and ignore error if not supported | 359 -- Catch and ignore error if not supported |
353 pcall(function () | 360 pcall(function () |
354 -- If this works, then we schedule cleanup for all known users on startup | 361 -- If this works, then we schedule cleanup for all known users on startup |
355 for user in um.users(module.host) do | 362 for user in um.users(module.host) do |
356 cleanup[user] = true; | 363 schedule_cleanup(user); |
357 end | 364 end |
358 end); | 365 end); |
359 | 366 |
360 -- At odd intervals, delete old messages for one user | 367 -- At odd intervals, delete old messages for one user |
361 module:add_timer(math.random(10, 60), function() | 368 module:add_timer(math.random(10, 60), function() |
362 local user = next(cleanup); | 369 local user = table.remove(cleanup, 1); |
363 if user then | 370 if user then |
364 module:log("debug", "Removing old messages for user %q", user); | 371 module:log("debug", "Removing old messages for user %q", user); |
365 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) | 372 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) |
366 if not ok then | 373 if not ok then |
367 module:log("warn", "Could not expire archives for user %s: %s", user, err); | 374 module:log("warn", "Could not expire archives for user %s: %s", user, err); |