Log a warning message if housekeeping has not run in more than 5 minutes
[citadel.git] / citadel / housekeeping.c
1 /*
2  * This file contains miscellaneous housekeeping tasks.
3  *
4  * Copyright (c) 1987-2017 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <libcitadel.h>
17
18 #include "ctdl_module.h"
19 #include "serv_extensions.h"
20 #include "room_ops.h"
21 #include "internet_addressing.h"
22 #include "journaling.h"
23
24 void check_sched_shutdown(void) {
25         if ((ScheduledShutdown == 1) && (ContextList == NULL)) {
26                 syslog(LOG_NOTICE, "housekeeping: scheduled shutdown initiating");
27                 server_shutting_down = 1;
28         }
29 }
30
31
32 /*
33  * Check (and fix) floor reference counts.  This doesn't need to be done
34  * very often, since the counts should remain correct during normal operation.
35  */
36 void check_ref_counts_backend(struct ctdlroom *qrbuf, void *data) {
37
38         int *new_refcounts;
39
40         new_refcounts = (int *) data;
41
42         ++new_refcounts[(int)qrbuf->QRfloor];
43 }
44
45
46 void check_ref_counts(void) {
47         struct floor flbuf;
48         int a;
49
50         int new_refcounts[MAXFLOORS];
51
52         syslog(LOG_DEBUG, "housekeeping: checking floor reference counts");
53         for (a=0; a<MAXFLOORS; ++a) {
54                 new_refcounts[a] = 0;
55         }
56
57         cdb_begin_transaction();
58         CtdlForEachRoom(check_ref_counts_backend, (void *)new_refcounts );
59         cdb_end_transaction();
60
61         for (a=0; a<MAXFLOORS; ++a) {
62                 lgetfloor(&flbuf, a);
63                 flbuf.f_ref_count = new_refcounts[a];
64                 if (new_refcounts[a] > 0) {
65                         flbuf.f_flags = flbuf.f_flags | QR_INUSE;
66                 }
67                 else {
68                         flbuf.f_flags = flbuf.f_flags & ~QR_INUSE;
69                 }
70                 lputfloor(&flbuf, a);
71                 syslog(LOG_DEBUG, "housekeeping: floor %d has %d rooms", a, new_refcounts[a]);
72         }
73 }       
74
75
76 /*
77  * This is the housekeeping loop.  Worker threads come through here after
78  * processing client requests but before jumping back into the pool.  We
79  * only allow housekeeping to execute once per minute, and we only allow one
80  * instance to run at a time.
81  */
82 static int housekeeping_in_progress = 0;
83 static time_t last_timer = 0L;
84 void do_housekeeping(void) {
85         int do_housekeeping_now = 0;
86         int do_perminute_housekeeping_now = 0;
87         time_t now;
88
89         /*
90          * We do it this way instead of wrapping the whole loop in an
91          * S_HOUSEKEEPING critical section because it eliminates the need to
92          * potentially have multiple concurrent mutexes in progress.
93          */
94         begin_critical_section(S_HOUSEKEEPING);
95         if (housekeeping_in_progress == 0) {
96                 do_housekeeping_now = 1;
97                 housekeeping_in_progress = 1;
98         }
99         end_critical_section(S_HOUSEKEEPING);
100
101         now = time(NULL);
102         if (do_housekeeping_now == 0) {
103                 if ( (now - last_timer) > (time_t)300 ) {
104                         syslog(LOG_WARNING,
105                                 "housekeeping: WARNING: housekeeping loop has not run for %ld minutes.  Is something stuck?",
106                                 ((now - last_timer) / 60)
107                         );
108                 }
109                 return;
110         }
111
112         /*
113          * Ok, at this point we've made the decision to run the housekeeping
114          * loop.  Everything below this point is real work.
115          */
116
117         if ( (now - last_timer) > (time_t)60 ) {
118                 do_perminute_housekeeping_now = 1;
119                 last_timer = time(NULL);
120         }
121
122         /* First, do the "as often as needed" stuff... */
123         JournalRunQueue();
124         PerformSessionHooks(EVT_HOUSE);
125
126         /* Then, do the "once per minute" stuff... */
127         if (do_perminute_housekeeping_now) {
128                 cdb_check_handles();                    /* suggested by Justin Case */
129                 PerformSessionHooks(EVT_TIMER);         /* Run any timer hooks */
130         }
131
132         /*
133          * All done.
134          */
135         begin_critical_section(S_HOUSEKEEPING);
136         housekeeping_in_progress = 0;
137         end_critical_section(S_HOUSEKEEPING);
138 }
139
140
141 void CtdlDisableHouseKeeping(void)
142 {
143         int ActiveBackgroundJobs;
144         int do_housekeeping_now = 0;
145         struct CitContext *nptr;
146         int nContexts, i;
147
148 retry_block_housekeeping:
149         syslog(LOG_INFO, "housekeeping: trying to disable services");
150         begin_critical_section(S_HOUSEKEEPING);
151         if (housekeeping_in_progress == 0) {
152                 do_housekeeping_now = 1;
153                 housekeeping_in_progress = 1;
154         }
155         end_critical_section(S_HOUSEKEEPING);
156         if (do_housekeeping_now == 0) {
157                 usleep(1000000);
158                 goto retry_block_housekeeping;
159         }
160         
161         syslog(LOG_INFO, "housekeeping: checking for running server jobs");
162
163 retry_wait_for_contexts:
164         /* So that we don't keep the context list locked for a long time
165          * we create a copy of it first
166          */
167         ActiveBackgroundJobs = 0;
168         nptr = CtdlGetContextArray(&nContexts) ;
169         if (nptr)
170         {
171                 for (i=0; i<nContexts; i++) 
172                 {
173                         if ((nptr[i].state != CON_SYS) || (nptr[i].lastcmd == 0))
174                                 continue;
175                         ActiveBackgroundJobs ++;
176                         syslog(LOG_INFO, "jousekeeping: job CC[%d] active; use TERM if you don't want to wait for it", nptr[i].cs_pid);
177                 
178                 }
179         
180                 free(nptr);
181
182         }
183         if (ActiveBackgroundJobs != 0) {
184                 syslog(LOG_INFO, "housekeeping: found %d running jobs, need to wait", ActiveBackgroundJobs);
185                 usleep(5000000);
186                 goto retry_wait_for_contexts;
187         }
188         syslog(LOG_INFO, "housekeeping: disabled now.");
189 }
190
191
192 void CtdlEnableHouseKeeping(void)
193 {
194         begin_critical_section(S_HOUSEKEEPING);
195         housekeeping_in_progress = 0;
196         end_critical_section(S_HOUSEKEEPING);
197 }