bulkmail_db_flush_wait

one of the documented procedures in this installation of the ACS
Usage:
bulkmail_db_flush_wait
What it does:
Run forever, waiting to flush message info to the db
Defined in: /web/philip/tcl/bulkmail-utils.tcl

Source code:


    ns_share bulkmail_db_flush_wait_event
    ns_share bulkmail_db_flush_wait_event_mutex
    ns_share bulkmail_db_flush_queue
    ns_share bulkmail_db_flush_queue_mutex
    ns_share bulkmail_instances_mutex
    ns_share bulkmail_instances

    # Loop forever, waiting for events requesting the flush of the queue.
    # TODO: probably want to have something trigger this to stop.  Maybe 
    # put a timeout on the wait, and have a check of a shared variable.
    while (1) {
	if { [catch {

	    # 2 second timeout
	    ns_event wait $bulkmail_db_flush_wait_event $bulkmail_db_flush_wait_event_mutex 2

	    ns_mutex lock $bulkmail_db_flush_queue_mutex
	    catch {
		set flush_queue $bulkmail_db_flush_queue
		set bulkmail_db_flush_queue [list]
	    }
	    ns_mutex unlock $bulkmail_db_flush_queue_mutex

	    if { [llength $flush_queue] > 0 } {
		set db [ns_db gethandle]
		ns_db dml $db "begin transaction"
		foreach flushed_messages $flush_queue {
		    foreach flush_entry $flushed_messages {
			set bulkmail_id [lindex $flush_entry 0]
			set user_id [lindex $flush_entry 1]
			set sent_date [lindex $flush_entry 2]
			ns_db dml $db "insert into bulkmail_log (bulkmail_id, user_id, sent_date) values ($bulkmail_id, $user_id, to_date('$sent_date', 'YYYY-MM-DD HH24:MI:SS'))"
			
		    }

		    # Even though we're only reading, others may be writing or deleting
		    # our entry for update.  So, we need to lock this.
		    ns_mutex lock $bulkmail_instances_mutex
		    catch {
			set instance_stats [ns_set get $bulkmail_instances $bulkmail_id]
			# instance_stats is a two-item list: queued sent
			set queued_count [lindex $instance_stats 0]
			set sent_count [lindex $instance_stats 1]
		    }
		    ns_mutex unlock $bulkmail_instances_mutex

		    # We need to check if sent_count is empty.  This might occur
		    # if bulkmail_end finished up before db_flush_wait.
		    if ![empty_string_p $sent_count] {
			ns_db dml $db "update bulkmail_instances set n_sent = $sent_count where bulkmail_id = $bulkmail_id"
		    }
		    
		}
		ns_db dml $db "end transaction"
		ns_db releasehandle $db
	    }

	    
	} errmsg] } {
	    ns_log Notice "Caught error: $errmsg in bulkmail_db_flush_wait"
	    if { [info exists db] && ![empty_string_p $db] && [ns_db connected $db]} {
		ns_db releasehandle $db
	    }

	}
	# Unlock the event's mutex
	ns_mutex unlock $bulkmail_db_flush_wait_event_mutex
    }


philg@mit.edu