Zend Queue with Magento

  • Sumo

Zend Queue with Magento

By using Zend Queue with magento we can create an event driven asynchronous integration system.

Just think about how much work you will offload from Magento when integrating with other systems and Magento itself.

To keep things simple, let’s pretend that you send emails to your customers every time a new product gets added. Usually these products get added during the day which also happens to be when your customers are most active buying in the site. An alert about a new product is very important but you don’t want to bug down your email server. That’s where Zend Queue comes to the rescue. In this example I am using MySQL to store the queue. If you follow the same example remember to create the tables first, these can be found under: lib/Zend/Queue/Adapter/Db/mysql.sql

Also it will make more sense to use MemcacheQ or Apache ActiveMQ to offload mysql.

Here’s an example:

Let’s pretend you dispatch an event with some key like ‘object_container’ which holds your data so in your observer class:

public function sendEmails($observer){
	Mage::helper('OfflineSync')->saveEmailsOffline($observer->getData('object_container'));
}

Helper class

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
/**
* Description of Data
 *
* @author letas
 */
class ZendQueue_OfflineSync_Helper_Data extends Mage_Core_Helper_Data {
    protected $_name = "general";
    protected $_registry = array();
    protected $_queue = null;
    protected function getQueue() {
        if (!isset($this->_registry[$this->_name])) {
            $db = simplexml_load_file('app' . DS . 'etc' . DS . 'local.xml');
            $db = $db->global->resources->default_setup->connection;
            $queueOptions = array(
                Zend_Queue::NAME => $this->_name,
                'driverOptions' => array(
                    'host' => $db->host,
                    'port' => $db->port,
                    'username' => $db->username,
                    'password' => $db->password,
                    'dbname' => $db->dbname,
                    'type' => 'pdo_mysql',
                    Zend_Queue::TIMEOUT => 1,
                    Zend_Queue::VISIBILITY_TIMEOUT => 1
                )
            );
            //// Create a database queue
            $this->_registry[$this->_name] = new Zend_Queue('Db', $queueOptions);
        }
        return $this->_registry[$this->_name];
    }
    public function getEmailsOffline() {
        try {
            $this->_name = "offline_email";
            //cache here / singlenton
            $this->_queue = $this->getQueue();
            foreach ($this->_queue->receive() as $message) {
               //send the real mail now
	   //delete this message
	  $this->_queue->deleteMessage($message);
            }
        } catch (Exception $e) {
            Mage::logException($e);
            return -1;
        }
        return 1;
    }
    public function saveEmailsOffline($emails) {
        if (isset($emails)) {
            if (is_array($emails) || is_object($emails)) {
                $emails = serialize($emails);
            }
            $this->_name = "offline_email";
            //cache here / singlenton
            $this->getQueue()->send($emails);
        }
        return $this;
    }
}

Now you can also configure a crontab in your module config.xml, your code may look like this:

	Mage::helper('OfflineSync')->getEmailsOffline();

And you are done!!! Pretty simple right.

The possibilities are endless. Orders / Customer exports. Everything can be queued up.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Author: Luis Tineo

Husband, Father, performance improvement junkie, biker and video gamer, Linux user and in my day job I'm a Software Engineer at BuyerQuest.

Share This Post On
  • Matthew Dolman

    Thank you so much this is exactly what I needed. Been looking for days to asynchronously execute some code in Magento, this is a lifesaver. Can’t believe how simple it is.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Pingback: Catalog Rules updates with Zend Queue - Memoir of a Magento developer()