Cédric Mourizard

Monitoring your Email Manager Queue with Sugar CLI

Blog Post created by Cédric Mourizard on Oct 10, 2016
This post originally appeared on the SynoLab blog hosted by Synolia, an Elite SugarCRM Partner. This post describes how to extend the new Sugar CLI framework to add commands that allow Sugar Administrators to monitor the Sugar e-mail queue.

 

Since the Sugar 7.7.1.0 version, SugarCRM introduced a Sugar CLI tool based on Symfony Console. This Sugar CLI tool is under beta version at this moment (August 2016) and can be changed in the future.

 

We will see in this article how to use this new Sugar CLI to add a command which provides some statistics from the Email Manager Queue. We want to display how many emails by campaign by module are waiting to be sent.

 

The first step is to define the Command itself

 

To perform this operation we implementSugarcrm\Sugarcrm\Console\CommandRegistry\Mode\InstanceModeInterface because our command must be executed only on an installed Sugar instance. The only required things to do is to provide the command name, the description, and the help text on the configure() method and then to implement our logic in the execute() method. We are using a SugarQuery to retrieve the data and display the result on a table. We can imagine externalizing this part and using it in an API and creating unit tests.

//File: custom/Synolia/EmailMan/Console/Command/ListQueueCommand.phpnamespace Synolia\EmailMan\Console\Command;use Sugarcrm\Sugarcrm\Console\CommandRegistry\Mode\InstanceModeInterface;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;use Symfony\Component\Console\Helper\Table;use Symfony\Component\Console\Helper\TableSeparator;/**

 

*

 

* Email Manager Queue statistics

 

*

 

*/

class ListQueueCommand extends Command implements InstanceModeInterface

 

{

 

    /**

 

     * {inheritdoc}

 

     */

 

    protected function configure()

 

    {

 

        $this

 

            ->setName('synolia:emailman:queue')

 

            ->setDescription('Show Email Manager Queue statistics')

 

            ->setHelp('This command displays statistics from Email Manager Queue.')

 

        ;

 

    }

 

    /**

 

     * {inheritdoc}

 

     */

 

    protected function execute(InputInterface $input, OutputInterface $output)

 

    {

 

        $result = $this->getSugarQuery()->execute();

 

        $nbEmailsToSent = 0;

 

        $table = new Table($output);

 

        $table->setHeaders(array('Campaign', 'Module', 'Count'));

 

        foreach ($result as $row) {

 

            $table->addRow(array($row['name'], $row['related_type'], $row['record_count']));

 

            $nbEmailsToSent += $row['record_count'];

 

        }

 

        $table->addRow(new TableSeparator());

 

        $table->addRow(array('# Emails to send', '', $nbEmailsToSent));

 

        $table->render();

 

    }

 

    /**

 

     * @return \SugarQuery

 

     * @throws \SugarQueryException

 

     */

 

    protected function getSugarQuery()

 

    {

 

        $sq = new \SugarQuery();

 

        $sq->from(\BeanFactory::newBean('EmailMan'))

 

            ->joinTable('campaigns', array(

 

                    'alias' => 'campaigns',

 

                    'joinType' => 'LEFT',

 

                    'linkingTable' => true)

 

            )->on()->equalsField('campaigns.id', 'emailman.campaign_id');

 

        $sq->select(array('campaigns.name', 'emailman.related_type'))->setCountQuery();

 

        $sq->groupBy('campaigns.name')

 

            ->groupBy('emailman.related_type');

 

        return $sq;

 

    }

 

}

 

Declare the Command

 

Now we need to make this command available by using the application/Ext/Console framework:

//File: custom/Extension/application/Ext/Console/SynoliaEmailManListQueueCommand.phpSugarcrm\Sugarcrm\Console\CommandRegistry\CommandRegistry::getInstance()->addCommand(new Synolia\EmailMan\Console\Command\ListQueueCommand());

 

Add our namespace

 

To use our own namespace we can follow one of the way described in our previous article by using theapplication/Ext/Utils framework:

//File: custom/Extension/application/Ext/Utils/SynoliaEmailManConsoleCommandNamespace.phpSugarAutoLoader::addNamespace('Synolia\\EmailMan\\Console\\Command', 'custom/Synolia/EmailMan/Console/Command', 'psr4');

 

Perform a Quick Repair and Rebuild et voilà!

 

Thanks to Jelle Vink about his presentation of this new Sugar CLI tools at UnCon 2016!

 

You can find more information about Sugar CLI on the Sugar Developer Guide.

Outcomes