Creating and executing eZ Publish tasks with Mugo Queue

The Mugo Queue extension is a generic framework for queuing specific tasks in eZ Publish. It stores tasks in the ezpending_actions table and can execute such tasks in batches. It includes a ready-to-use task for re-building the eZ Find search index, whether for an entire site or only specific parts of a site. (You can build your own fetch function to re-index, say, just objects of a certain content class.) This blog post explains how to develop your own task.

You can download Mugo Queue here. Installing the extension is straightforward, as you basically need to enable the extension and regenerate the eZ Publish autoloads file. For more information, see the installation instructions in extension/mugo_queue/doc/INSTALL.

The following example explains how to create your own task within the Mugo Queue framework. This simple example removes old eZ Publish object drafts from the database. So-called "stale drafts" create editing conflicts and take up space in the database.

Cleaning up drafts is already implemented as a normal eZ Publish cronjob (see cronjobs/old_drafts_cleanup.php); however, having 2 different approaches for the same task enables you to easily see the differences between a normal cronjob and the Mugo Queue approach.

First, we create a new Mugo Task class. Create a new PHP file here:

extension/mugo_queue/classes/tasks/RemoveOldDraftsTask.php

The contents of the file is a new task class that extends the abstract MugoTask class.

class RemoveOldDraftsTask extends MugoTask
{
  public static function create( $parameters )
  {}
 
 
  public function execute( $task_id, $parameters )
  {}
}

It contains 2 required functions. The "create" function adds tasks to the queue. The "execute" function grabs a task from the queue and executes the task. Let's implement each function now:

public function create( $parameters )
{
  $return = array();
 
  // Fetches all versions matching a given status and modification limitation
  $filters = array( 'status'   => array( array( eZContentObjectVersion::STATUS_DRAFT ) ),
                    'modified' => array( '<', time() )
                    // or: 'modified' => array( '<', time() - ( 60 * 60 * 24 * 7 ) )
                  );
 
  $versions = eZPersistentObject::fetchObjectList( eZContentObjectVersion::definition(),
                                                   null, $filters,
                                                   null, null, false );
 
  if( !empty( $versions ) )
  {
      foreach( $versions as $version_row )
      {
          $return[] = $version_row[ 'id' ];
      }
  }
  else
  {
     $this->log( 'Could not find any old drafts.' );
  }
 
  return $return;
} 
 

The "create" function simply fetches all eZ Publish object versions with the status "STATUS_DRAFT". You can see a different "modified" filter as a comment. The alternative "modified" filter limits the result to object versions that are at least 7 days old. For testing purposes, you do not need to use this other filter. The function reads all version IDs from the result and returns those IDs in an array. The input variable "$parameters" comes from the command line script call and is optional. We don't need any parameters for this simple "create" function.

Here is the "execute" function:

public function execute( $task_id, $parameters )
{
  $success = false;
 
  $db = eZDB::instance();
  $db->begin();
 
  $version = eZContentObjectVersion::fetch( $task_id );
 
  if( $version )
  {
    $version->removeThis();
 
    //ezp API has no return value for the function removeThis() - so we need to assume success
    $success = true;
  }
 
  $db->commit();
 
  return $success;
}
 

The framework is responsible for getting a task out of the queue and submits the task ID to the "execute" function. The variable "$parameters" is again optional, and not needed in our example.

With the provided task ID (one of the task IDs we added in the "create" function) we fetch an eZ Publish object version. If that version still exists, we remove it. The code is wrapped in a database transaction. It is important to return a boolean value because the framework only removes the task from the queue if you return "true".

To run the code, we use a command line script. Run the script without any parameters to see all available options:

php extension/mugo_queue/bin/run.php
 

Now let's try our example:

  1. Create a draft by editing an object and then click "Store draft and exit"
  2. Add tasks into the queue:
    php extension/mugo_queue/bin/run.php -k RemoveOldDraftsTask -a create
  3. (optional) Check if you have tasks in the queue:
    php extension/mugo_queue/bin/run.php -k RemoveOldDraftsTask -a list
  4. Execute the tasks:
    php extension/mugo_queue/bin/run.php -k RemoveOldDraftsTask -a execute
  5. Check if you have any remaining tasks in the queue:
    php extension/mugo_queue/bin/run.php -k RemoveOldDraftsTask -a list (optional)

Currently the script does not produce a lot of output, but you can check the log files var/log/mugo*.log for more information.

Comments

blog comments powered by Disqus

Contact

604-637-6396
hiATmugo.ca

#414-207 W. Hastings St
Vancouver, BC
Canada


RSS icon Twitter icon

We have been using this team for editing, consulting, support and training services in North America for the last 3 years and we still do. The team has shown their expert knowledge of our systems, and is responsive and dedicated. The services they provide include expert consulting on eZ Publish towards large, high-traffic enterprise customers.

CEO Aleksander Farstad , eZ Systems

I relied on Mugo for the development of two complex web applications, and I was extremely impressed with their accurate estimates, consistent and timely delivery of production code, and commitment. They have the rare blend of business knowledge and deep technical expertise, and they excel at listening to business requirements and translating those requirements into intelligent and reliable software products. They are able to work on all aspects of software solutions -- front end/UI, core code, modules and extensions, application integration, and infrastructure -- and they are extremely capable in all of those areas. I highly recommend Mugo to anyone in need of a competent and dependable technology partner.

Co-Founder Graham Tillotson, Tandem, Chicago

Mugo maintains and develops new features for the Rasmussen Reports website, www.rasmussenreports.com. We are impressed with how efficiently they deliver high quality solutions that exactly fit our needs. The Mugo team understands the complexity of our high traffic website and was able to improve our site's overall performance, as well as its search engine rating. For our site's subscribers, Mugo quickly and efficiently developed many new features for our Premium Section.

The Mugo team is great to work with, and I look forward to a long and productive relationship.

Stephen W Smith, Interim CEO, Rasmussen Reports

If you ask me what is the first thing you think of about the Mugo team, I would say that they listen.

For a customer, the worst thing is to work with a partner that doesn't listen to you. That doesn't understand your points. That simply doesn't listen and try to help you.

Daniel Iribarren, ClearCap

Mugo Web is not another service provider. They are your partners.
One of the things I really like about the Mugo team is that they care about my project and myself. That is why I call them my partners.

Daniel Iribarren, ClearCap

Mugo Web is not just a group of geeks. They are a team of highly knowledgeable technology and business people.
They understand that they are helping you to design solutions to satisfy or solve business problems. And not every developer is able to deliver that. I have worked with more than 10 different let's says production centers and what they delivered to me was geek stuff. Not business solutions.
Mugo Web will help you to use geek stuff to solve business problems.

Daniel Iribarren, ClearCap

It's been a pleasure working with Mugo Web. They are fast, efficient and always helpful. They respond positively to questions and offer great solutions to technical problems.

Susan Wright, Director of Operations, Toronto Arts Council and Foundation

We've been extremely pleased with the work Mugo has done for us. They have been responsive and understanding in dealing with the complexities of our needs.

Wendy Prugh, Program Manager, The Christian Science Monitor

The team members we work with at Mugo have demonstrated a high caliber of competency in the work they do, and it instills great confidence in us to know that we have such quality professionals in our corner. Their consistent delivery has helped us develop a fluid system for enacting change and improvements on our website. We certainly look forward to continuing this relationship!

Wendy Prugh, Program Manager, The Christian Science Monitor

Thank you very much Mugo Web for the website work you've done for our company. You valued & respected our time. You produced quality work. You paid attention to detail and understood the JPLradio concept. Most importantly, we appreciate the good business ethic of our contact person your company provided. For that, I/we thank you.

Troy B. Williams, Founder & Program Director, Just Positive Lyrics Radio

We thoroughly enjoy our working relationship with Mugo Web. They are customer-focused, take the time to listen, give good solid advice with our best interests in mind, and have a quick turnaround time. We've had a number of compliments on the new website, which looks goods, functions well, and performs solidly.

Paul O'Sullivan, IT Specialist, New Society Publishers

From our first contact, the team at Mugo has shown an unprecedented level of commitment to our web projects... ending almost 4 years of struggling! In the three months Mugo worked on our new version -- which had stalled with our previous developers -- we have made more progress than ever before. They are fast, precise and thorough, and definitely stand out in the industry!

Sébastien Michel, Director, Frogs-in-NZ

Tweets

Follow Mugo on Twitter