Zend_Queue simple example for offline processing
Adapted class
<?php
/**
* Zend_Queue offline processing hack example
*
* @author Dave Marshall
* @version $Rev: $
* @since $Date: $
* @link $URL: $
*/
class EmailSender {
private static $queue = null;
/**
* Set Queue
*
* @param Zend_Queue $queue
*/
public static function setQueue($queue)
{
self::$queue = $queue;
}
/**
* Takes an array of addresses and sends an email to each one.
*
* @see reallySendEmail
* @see sendQueuedEmails
* @param array $address
*/
public static function sendEmail($address)
{
if (self::$queue === null) {
// just send them now if we don't have a queue instance
return self::reallySendEmail($address);
}
self::$queue->send(serialize(func_get_args()));
}
/**
* Takes an array of addresses and sends an email to each one.
*
* @see sendEmail
* @param array $address
*/
private static function reallySendEmail($address)
{
/**
* Code in here
*/
echo 'Sending email to ' . implode(', ', $address) . PHP_EOL;
}
/**
* Reads emails from the queue and sends them
*
* @param int $count - The number of queued items to process
*/
public static function sendQueuedEmails($count)
{
/**
* Should really check the queue is good here
*/
$messages = self::$queue->receive(intval($count));
foreach($messages as $msg) {
$args = unserialize($msg->body);
call_user_func_array(array(__CLASS__, 'reallySendEmail'), $args);
self::$queue->deleteMessage($msg);
}
}
}
Usage in application code
<?php
set_include_path(
'path/to/zend/framework' . PATH_SEPARATOR
. 'path/to/zend/framework/incubator' . PATH_SEPARATOR
. get_include_path()
);
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
define('DB_SERVER', 'localhost');
define('DB_PORT', 3306);
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'emailsender');
$config = array(
'name' => 'transmittal',
'driverOptions' => array(
'host' => DB_SERVER,
'port' => DB_PORT,
'username' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME,
'type' => 'pdo_mysql'
)
);
// Create a database queue
$queue = new Zend_Queue('Db', $config);
$queue->createQueue('myqueue'); // called for good measure
EmailSender::setQueue($queue);
EmailSender::sendEmail(array('yourname@gmail.com'));
Usage for crontab script
<?php
set_include_path(
'path/to/zend/framework' . PATH_SEPARATOR
. 'path/to/zend/framework/incubator' . PATH_SEPARATOR
. get_include_path()
);
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
define('DB_SERVER', 'localhost');
define('DB_PORT', 3306);
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'emailsender');
$config = array(
'name' => 'transmittal',
'driverOptions' => array(
'host' => DB_SERVER,
'port' => DB_PORT,
'username' => DB_USER,
'password' => DB_PASS,
'dbname' => DB_NAME,
'type' => 'pdo_mysql'
)
);
// Create a database queue
$queue = new Zend_Queue('Db', $config);
$queue->createQueue('myqueue'); // called for good measure
EmailSender::setQueue($queue);
EmailSender::sendQueuedEmails(5);
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
This is a little hacky, but a nice way to demonstrate Zend_Queue.
An application we are developing sometimes had to send a lot of emails on request from the user, sending them was taking too long 'online', so we refactored the class to add the calls to a queue, allowing a scheduled task to send the emails 'offline'.
This was tested against the latest trunk of Zend Framework and the incubator.
- Created:
-
davedevelopment
- Edited:
-
davedevelopment
- Revision Id:
- 113
- Edit Message:
- Added Zend Framework Version the code was tested with
- ZF Version
- 1.7.8
- Tags:
- Zend_Queue queue user experience
- Comments:
- 1
- Points:
- 0 (0 votes)
8 months ago
Got quite a lot of views on this, but didn't get any feedback, would be appreciated.