CtdlThreadSchedule() considered harmful. Eliminate. Replaced all
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index 69f12fb037bc110485524be5d8039cc3b03e5855..ab01bbd7db9bbc1fb10f4c2049d8be55c49f10a5 100644 (file)
@@ -119,8 +119,6 @@ enum {                              /* Command states for login authentication */
 
 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
 
-citthread_mutex_t smtp_send_lock;
-
 
 /*****************************************************************************/
 /*                      SMTP SERVER (INBOUND) STUFF                          */
@@ -1732,27 +1730,31 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
 
 
 /*
- * smtp_do_queue()
+ * smtp_queue_thread()
  * 
  * Run through the queue sending out messages.
  */
-void *smtp_do_queue(void *arg) {
+void *smtp_queue_thread(void *arg) {
        int num_processed = 0;
        struct CitContext smtp_queue_CC;
 
        CtdlFillSystemContext(&smtp_queue_CC, "SMTP Send");
-       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC );
-       CtdlLogPrintf(CTDL_INFO, "SMTP client: processing outbound queue\n");
+       citthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
+       CtdlLogPrintf(CTDL_DEBUG, "smtp_queue_thread() initializing\n");
 
-       if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
-               CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
-       }
-       else {
-               num_processed = CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_do_procmsg, NULL);
-       }
+       while (!CtdlThreadCheckStop()) {
+               
+               CtdlLogPrintf(CTDL_INFO, "SMTP client: processing outbound queue\n");
 
-       citthread_mutex_unlock (&smtp_send_lock);
-       CtdlLogPrintf(CTDL_INFO, "SMTP client: queue run completed; %d messages processed\n", num_processed);
+               if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
+                       CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
+               }
+               else {
+                       num_processed = CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_do_procmsg, NULL);
+               }
+               CtdlLogPrintf(CTDL_INFO, "SMTP client: queue run completed; %d messages processed\n", num_processed);
+               CtdlThreadSleep(60);
+       }
 
        CtdlClearSystemContext();
        return(NULL);
@@ -1760,38 +1762,6 @@ void *smtp_do_queue(void *arg) {
 
 
 
-/*
- * smtp_queue_thread
- *
- * Create a thread to run the SMTP queue
- *
- * This was created as a response to a situation seen on Uncensored where a bad remote was holding
- * up SMTP sending for long times.
- * Converting to a thread does not fix the problem caused by the bad remote but it does prevent
- * the SMTP sending from stopping housekeeping and the EVT_TIMER event system which in turn prevented
- * other things from happening.
- */
-void smtp_queue_thread (void)
-{
-       if (citthread_mutex_trylock (&smtp_send_lock)) {
-               CtdlLogPrintf(CTDL_DEBUG, "SMTP queue run already in progress\n");
-       }
-       else {
-               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_do_queue, NULL);
-       }
-}
-
-
-
-void smtp_server_going_down (void)
-{
-       CtdlLogPrintf(CTDL_DEBUG, "SMTP module clean up for shutdown.\n");
-
-       citthread_mutex_destroy (&smtp_send_lock);
-}
-
-
-
 /*****************************************************************************/
 /*                          SMTP UTILITY COMMANDS                            */
 /*****************************************************************************/
@@ -1924,11 +1894,9 @@ CTDL_MODULE_INIT(smtp)
                                        CitadelServiceSMTP_LMTP_UNF);
 
                smtp_init_spoolout();
-               CtdlRegisterSessionHook(smtp_queue_thread, EVT_TIMER);
                CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP);
                CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
-               CtdlRegisterCleanupHook (smtp_server_going_down);
-               citthread_mutex_init (&smtp_send_lock, NULL);
+               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, smtp_queue_thread, NULL);
        }
        
        /* return our Subversion id for the Log */