Use a MySQL table to create a simple message queue for delayed notifications.
Background processing is always a problem in web applications. Users like to get snappy responses from their web pages, but sometimes processing can take a while. A classic example is a web notification mail-out. The user initiates some process that requires mailing a notice to 100 people, but mailing to 100 people takes a while. Making the server wait for all the mail to go out isn't a good idea. It looks like the application is hung or about to crash. Ideally we could return a page to the user, and then in the background, send out the 100 messages. But how do we do that?
One method is to create a simple database-driven message queue. The web page puts some data into the queue,
which is executed by another process later (usually fired off by the
cron task-scheduling system). The
message queue in this system takes two parameters: the
function to be run and the arguments to the function. Therefore, you can
delay almost any processing you want.
Figure 5-27 shows the
simple schema for the message queue. There are really only two fields:
func, which holds the name of the
args, which holds the
XML version of the arguments.
Figure 5-27. The schema for the queue
Figure 5-28 shows the status of the queue table. It starts empty, with no messages. Then a couple of messages ...