[MOD] Make the cleanup *do a thing*.

This commit is contained in:
LDA 2024-06-23 21:14:07 +02:00
commit 10e140e2a2
5 changed files with 62 additions and 2 deletions

View file

@ -77,6 +77,8 @@ Main(void)
cron = CronCreate( 10 SECONDS );
CronEvery(cron, 30 MINUTES, ParseeCleanup, conf.handlerArgs);
CronStart(cron);
Log(LOG_NOTICE, "Creating XMPP listener thread...");
if (pthread_create(&xmpp_thr, NULL, ParseeXMPPThread, conf.handlerArgs))
{

View file

@ -1,7 +1,10 @@
#include <Parsee.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Util.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <Routes.h>
@ -49,7 +52,50 @@ void
ParseeCleanup(void *datp)
{
ParseeData *data = datp;
Array *chats;
char *chat;
size_t i;
uint64_t ts = UtilTsMillis();
Log(LOG_NOTICE, "Cleaning up...");
chats = DbList(data->db, 1, "chats");
for (i = 0; i < ArraySize(chats); i++)
{
DbRef *ref;
HashMap *json, *stanzas;
char *stanza;
JsonValue *val;
Array *to_delete;
size_t j;
chat = ArrayGet(chats, i);
ref = DbLock(data->db, 2, "chats", chat);
json = DbJson(ref);
stanzas = JsonValueAsObject(HashMapGet(json, "stanzas"));
to_delete = ArrayCreate();
while (HashMapIterate(stanzas, &stanza, (void **) &val))
{
HashMap *obj = JsonValueAsObject(val);
uint64_t age = JsonValueAsInteger(HashMapGet(obj, "age"));
uint64_t dur = ts - age;
if ((dur > (5 MINUTES)))
{
ArrayAdd(to_delete, StrDuplicate(stanza));
}
}
for (j = 0; j < ArraySize(to_delete); j++)
{
stanza = ArrayGet(to_delete, j);
JsonValueFree(HashMapDelete(stanzas, stanza));
Free(stanza);
}
ArrayFree(to_delete);
DbUnlock(data->db, ref);
}
DbListFree(chats);
/* TODO */
}

View file

@ -2,6 +2,7 @@
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Util.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Sha.h>
#include <Cytoplasm/Log.h>
@ -544,8 +545,9 @@ ParseePushStanza(ParseeData *data, char *chat_id, char *stanza_id)
{
DbRef *ref;
HashMap *j;
HashMap *stanzas;
HashMap *stanzas, *obj;
bool new_stanzas = false;
uint64_t age = UtilTsMillis();
if (!data || !chat_id || !stanza_id)
{
return;
@ -565,7 +567,9 @@ ParseePushStanza(ParseeData *data, char *chat_id, char *stanza_id)
new_stanzas = true;
}
JsonValueFree(HashMapSet(stanzas, stanza_id, JsonValueNull()));
obj = HashMapCreate();
HashMapSet(obj, "age", JsonValueInteger(age));
JsonValueFree(HashMapSet(stanzas, stanza_id, JsonValueObject(obj)));
if (new_stanzas)
{

View file

@ -2,6 +2,7 @@
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
XMLElement *
XMLCreateTag(char *name)

View file

@ -1,5 +1,8 @@
#include <XML.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <string.h>
XMLElement *
@ -72,6 +75,10 @@ XMLDecode(Stream *stream, bool autofree)
}
break;
case XML_LEXER_DATA:
if (!peek())
{
break;
}
top = XMLCreateText(event->data);
XMLAddChild(peek(), top);
break;