bulkmail_end

one of the documented procedures in this installation of the ACS
Usage:
bulkmail_end   db   bulkmail_id
What it does:
Finalizes the info regarding the instance in bulkmail_instances
Defined in: /web/philip/tcl/bulkmail-utils.tcl

Source code:


    ns_share bulkmail_db_flush_wait_event_mutex
    ns_share bulkmail_db_flush_wait_event
    ns_share bulkmail_instances_mutex
    ns_share bulkmail_instances
    ns_share bulkmail_finished_instances_mutex
    ns_share bulkmail_finished_instances
    ns_share bulkmail_instance_finished_event_mutex
    ns_share bulkmail_instance_finished_event
    ns_share bulkmail_message_queue_mutex
    
    # Spit out any lingering messages in the message queue.  If we have
    # anything still queued up, this will start them sending
    ns_mutex lock $bulkmail_message_queue_mutex
    catch {
	bulkmail_process_message_queue
    }
    ns_mutex unlock $bulkmail_message_queue_mutex

    # Wait until all of our messages have been sent.
    while (1) {
	# a bulkmail_instance_finished_event is triggered when all of an
	# instance's queued messages have been sent. Let's wait 10 seconds
	# on the event.  If it times out, we'll check to see if our instance
	# is finished, in case we missed the event (Thanks, Henry!)
	ns_event wait $bulkmail_instance_finished_event $bulkmail_instance_finished_event_mutex 10

	# Check to see if the instance we're waiting on is one that finished.
	ns_mutex lock $bulkmail_finished_instances_mutex
	if { [catch {
	    set n_sent [ns_set get $bulkmail_finished_instances $bulkmail_id]
	} errmsg] } {
	    ns_log Error "Error getting n_sent: $errmsg\n"
	}
	ns_log Notice "bulkmail_id: $bulkmail_id, n_sent: $n_sent\n"
	ns_mutex unlock $bulkmail_finished_instances_mutex

	# It was us, so let's finish up
	if ![empty_string_p $n_sent] {
	    # Remove this instance from the list of finished instances
	    ns_mutex lock $bulkmail_finished_instances_mutex
	    catch {
		ns_set delkey $bulkmail_finished_instances $bulkmail_id
	    }
	    ns_mutex unlock $bulkmail_finished_instances_mutex

	    # We want to wait until the db_flush_wait proc is done flushing
	    # (the mutex for the wait event is unlocked) before we try 
	    # telling it to flush.  We'll do this by trying to lock the
	    # mutex.
	    ns_mutex lock $bulkmail_db_flush_wait_event_mutex
	    ns_mutex unlock $bulkmail_db_flush_wait_event_mutex

	    # Now trigger it.
	    ns_event set $bulkmail_db_flush_wait_event

	    # Remove this instance from bulkmail_instances
	    # This is placed far down, because it's called in
	    # bulkmail_db_flush_wait (so we need to make sure we don't
	    # call it until all our guys have made it.)
	    ns_mutex lock $bulkmail_instances_mutex
	    catch {
		ns_set delkey $bulkmail_instances $bulkmail_id
	    }
	    ns_mutex unlock $bulkmail_instances_mutex

	    # Wahoo! We're all clear kid!
	    break
	}
    }

    # Once everything is done, let's finalize the data
    # It should have already been done by the db_flush proc, but just in
    # case, let's set it here.
    ns_db dml $db "update bulkmail_instances set end_date = sysdate where bulkmail_id = $bulkmail_id"



philg@mit.edu