-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);
+ }