2 * This file contains miscellaneous housekeeping tasks.
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
16 # include <sys/time.h>
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_SELECT_H
28 #include <sys/select.h>
30 #include <libcitadel.h>
33 #include "serv_extensions.h"
34 #include "citserver.h"
36 #include "housekeeping.h"
37 #include "sysdep_decls.h"
41 #include "journaling.h"
43 #include "ctdl_module.h"
46 void check_sched_shutdown(void) {
47 if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
48 CtdlLogPrintf(CTDL_NOTICE, "Scheduled shutdown initiating.\n");
56 * Check (and fix) floor reference counts. This doesn't need to be done
57 * very often, since the counts should remain correct during normal operation.
59 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
63 new_refcounts = (int *) data;
65 ++new_refcounts[(int)qrbuf->QRfloor];
68 void check_ref_counts(void) {
72 int new_refcounts[MAXFLOORS];
74 CtdlLogPrintf(CTDL_DEBUG, "Checking floor reference counts\n");
75 for (a=0; a<MAXFLOORS; ++a) {
79 cdb_begin_transaction();
80 CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
81 cdb_end_transaction();
83 for (a=0; a<MAXFLOORS; ++a) {
85 flbuf.f_ref_count = new_refcounts[a];
86 if (new_refcounts[a] > 0) {
87 flbuf.f_flags = flbuf.f_flags | QR_INUSE;
90 flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
93 CtdlLogPrintf(CTDL_DEBUG, "Floor %d: %d rooms\n", a, new_refcounts[a]);
98 * This is the housekeeping loop. Worker threads come through here after
99 * processing client requests but before jumping back into the pool. We
100 * only allow housekeeping to execute once per minute, and we only allow one
101 * instance to run at a time.
103 void do_housekeeping(void) {
104 static int housekeeping_in_progress = 0;
105 static time_t last_timer = 0L;
106 int do_housekeeping_now = 0;
107 int do_perminute_housekeeping_now = 0;
109 const char *old_name;
112 * We do it this way instead of wrapping the whole loop in an
113 * S_HOUSEKEEPING critical section because it eliminates the need to
114 * potentially have multiple concurrent mutexes in progress.
116 begin_critical_section(S_HOUSEKEEPING);
117 if (housekeeping_in_progress == 0) {
118 do_housekeeping_now = 1;
119 housekeeping_in_progress = 1;
121 if ( (now - last_timer) > (time_t)60 ) {
122 do_perminute_housekeeping_now = 1;
123 last_timer = time(NULL);
126 end_critical_section(S_HOUSEKEEPING);
128 if (do_housekeeping_now == 0) {
133 * Ok, at this point we've made the decision to run the housekeeping
134 * loop. Everything below this point is real work.
137 /* First, do the "as often as needed" stuff... */
138 old_name = CtdlThreadName("House Keeping - Journal");
141 CtdlThreadName("House Keeping - EVT_HOUSE");
142 PerformSessionHooks(EVT_HOUSE); /* perform as needed housekeeping */
144 /* Then, do the "once per minute" stuff... */
145 if (do_perminute_housekeeping_now) {
146 cdb_check_handles(); /* suggested by Justin Case */
147 CtdlThreadName("House Keeping - EVT_TIMER");
148 PerformSessionHooks(EVT_TIMER); /* Run any timer hooks */
154 housekeeping_in_progress = 0;
155 CtdlThreadName(old_name);