4 * This file contains miscellaneous housekeeping tasks.
14 #if TIME_WITH_SYS_TIME
15 # include <sys/time.h>
19 # include <sys/time.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 #include <sys/select.h>
33 #include <libcitadel.h>
36 #include "serv_extensions.h"
37 #include "citserver.h"
39 #include "housekeeping.h"
40 #include "sysdep_decls.h"
44 #include "journaling.h"
46 #include "ctdl_module.h"
49 void check_sched_shutdown(void) {
50 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
51 CtdlLogPrintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
59 * Check (and fix) floor reference counts. This doesn't need to be done
60 * very often, since the counts should remain correct during normal operation.
62 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
66 new_refcounts = (int *) data;
68 ++new_refcounts[(int)qrbuf->QRfloor];
71 void check_ref_counts(void) {
75 int new_refcounts[MAXFLOORS];
77 CtdlLogPrintf(CTDL_DEBUG, "Checking floor reference counts\n");
78 for (a=0; a<MAXFLOORS; ++a) {
82 cdb_begin_transaction();
83 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
84 cdb_end_transaction();
86 for (a=0; a<MAXFLOORS; ++a) {
88 flbuf.f_ref_count = new_refcounts[a];
89 if (new_refcounts[a] > 0) {
90 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
93 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
96 CtdlLogPrintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
101 * This is the housekeeping loop. Worker threads come through here after
102 * processing client requests but before jumping back into the pool. We
103 * only allow housekeeping to execute once per minute, and we only allow one
104 * instance to run at a time.
106 void do_housekeeping(void) {
107 static int housekeeping_in_progress = 0;
108 static time_t last_timer = 0L;
109 int do_housekeeping_now = 0;
110 int do_perminute_housekeeping_now = 0;
112 const char *old_name;
115 * We do it this way instead of wrapping the whole loop in an
116 * S_HOUSEKEEPING critical section because it eliminates the need to
117 * potentially have multiple concurrent mutexes in progress.
119 begin_critical_section(S_HOUSEKEEPING);
120 if (housekeeping_in_progress == 0) {
121 do_housekeeping_now = 1;
122 housekeeping_in_progress = 1;
124 if ( (now - last_timer) > (time_t)60 ) {
125 do_perminute_housekeeping_now = 1;
126 last_timer = time(NULL);
129 end_critical_section(S_HOUSEKEEPING);
131 if (do_housekeeping_now == 0) {
136 * Ok, at this point we've made the decision to run the housekeeping
137 * loop. Everything below this point is real work.
140 /* First, do the "as often as needed" stuff... */
141 old_name = CtdlThreadName("House Keeping - Journal");
144 CtdlThreadName("House Keeping - EVT_HOUSE");
145 PerformSessionHooks(EVT_HOUSE); /* perform as needed housekeeping */
147 /* Then, do the "once per minute" stuff... */
148 if (do_perminute_housekeeping_now) {
149 cdb_check_handles(); /* suggested by Justin Case */
150 CtdlThreadName("House Keeping - EVT_TIMER");
151 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
157 housekeeping_in_progress = 0;
158 CtdlThreadName(old_name);