From eb5fe4c7a556fe54065a40c7b34d12a9ca839b04 Mon Sep 17 00:00:00 2001 From: Dave West Date: Sat, 1 Dec 2007 15:45:08 +0000 Subject: [PATCH] A thread based version of the Auto Purger. This is highly EXPERIMENTAL!!!!!! --- citadel/modules/expire/serv_expire.c | 110 ++++++++++++++++++--------- 1 file changed, 73 insertions(+), 37 deletions(-) diff --git a/citadel/modules/expire/serv_expire.c b/citadel/modules/expire/serv_expire.c index 4902af16b..d856761c8 100644 --- a/citadel/modules/expire/serv_expire.c +++ b/citadel/modules/expire/serv_expire.c @@ -713,49 +713,85 @@ int PurgeEuidIndexTable(void) { } -void purge_databases(void) { - int retval; - static time_t last_purge = 0; - time_t now; - struct tm tm; - - /* Do the auto-purge if the current hour equals the purge hour, - * but not if the operation has already been performed in the - * last twelve hours. This is usually enough granularity. - */ - now = time(NULL); - localtime_r(&now, &tm); - if (tm.tm_hour != config.c_purge_hour) return; - if ((now - last_purge) < 43200) return; - - lprintf(CTDL_INFO, "Auto-purger: starting.\n"); - - retval = PurgeUsers(); - lprintf(CTDL_NOTICE, "Purged %d users.\n", retval); - - PurgeMessages(); - lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged); +void *purge_databases(void *args) +{ + int retval; + static time_t last_purge = 0; + time_t now; + struct tm tm; + + CT_PUSH(); // Makes it easier to access this threads structure + + cdb_allocate_tsd(); + + while (!CtdlThreadCheckStop(CT)) { + /* Do the auto-purge if the current hour equals the purge hour, + * but not if the operation has already been performed in the + * last twelve hours. This is usually enough granularity. + */ + now = time(NULL); + localtime_r(&now, &tm); + if ((tm.tm_hour != config.c_purge_hour) || ((now - last_purge) < 43200)) { + CtdlThreadSleep(60); + continue; + } + + + lprintf(CTDL_INFO, "Auto-purger: starting.\n"); + + if (!CtdlThreadCheckStop(CT)) + { + retval = PurgeUsers(); + lprintf(CTDL_NOTICE, "Purged %d users.\n", retval); + } + + if (!CtdlThreadCheckStop(CT)) + { + PurgeMessages(); + lprintf(CTDL_NOTICE, "Expired %d messages.\n", messages_purged); + } - retval = PurgeRooms(); - lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval); + if (!CtdlThreadCheckStop(CT)) + { + retval = PurgeRooms(); + lprintf(CTDL_NOTICE, "Expired %d rooms.\n", retval); + } - retval = PurgeVisits(); - lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval); + if (!CtdlThreadCheckStop(CT)) + { + retval = PurgeVisits(); + lprintf(CTDL_NOTICE, "Purged %d visits.\n", retval); + } - retval = PurgeUseTable(); - lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval); + if (!CtdlThreadCheckStop(CT)) + { + retval = PurgeUseTable(); + lprintf(CTDL_NOTICE, "Purged %d entries from the use table.\n", retval); + } - retval = PurgeEuidIndexTable(); - lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval); + if (!CtdlThreadCheckStop(CT)) + { + retval = PurgeEuidIndexTable(); + lprintf(CTDL_NOTICE, "Purged %d entries from the EUID index.\n", retval); + } - retval = TDAP_ProcessAdjRefCountQueue(); - lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval); + if (!CtdlThreadCheckStop(CT)) + { + retval = TDAP_ProcessAdjRefCountQueue(); + lprintf(CTDL_NOTICE, "Processed %d message reference count adjustments.\n", retval); + } - lprintf(CTDL_INFO, "Auto-purger: finished.\n"); + if (!CtdlThreadCheckStop(CT)) + { + lprintf(CTDL_INFO, "Auto-purger: finished.\n"); + last_purge = now; /* So we don't do it again soon */ + } + else + lprintf(CTDL_INFO, "Auto-purger: STOPPED.\n"); - last_purge = now; /* So we don't do it again soon */ + } + return NULL; } - /*****************************************************************************/ @@ -846,10 +882,10 @@ CTDL_MODULE_INIT(expire) { if (!threading) { - CtdlRegisterSessionHook(purge_databases, EVT_TIMER); CtdlRegisterProtoHook(cmd_fsck, "FSCK", "Check message ref counts"); } - + else + CtdlThreadCreate("Auto Purger", CTDLTHREAD_BIGSTACK, purge_databases, NULL); /* return our Subversion id for the Log */ return "$Id$"; } -- 2.30.2