Skip to content

Commit

Permalink
added Swiftmailer Beanstalk plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
radutopala committed Jan 23, 2013
1 parent 227e329 commit 591817c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
10 changes: 5 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
Expand All @@ -20,9 +18,11 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('tss_automailer');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->scalarNode('beanstalk')->defaultValue(0)->end()
->end()
;

return $treeBuilder;
}
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/TSSAutomailerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

if ($config['beanstalk']) {
$container->getDefinition('automailer.plugin.beanstalk')->addTag('swiftmailer.plugin');
}
}
}
9 changes: 6 additions & 3 deletions Library/AutomailerSpool.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,19 @@ public function flushQueue(\Swift_Transport $transport, &$failedRecipients = nul
$count = 0;
$time = time();

$limit = !$this->getMessageLimit()?50:$this->getMessageLimit();
$limit = !$this->getMessageLimit()? 50 : $this->getMessageLimit();

$mails = $this->_em->getRepository("TSSAutomailerBundle:Automailer")->findNext($limit);


//first mark all for sending
foreach ($mails as $mail) {

$mail->setIsSending(1);
$this->_em->persist($mail);
$this->_em->flush();

}

foreach ($mails as $mail) {
if($transport->send($mail->getSwiftMessage(), $failedRecipients))
{
$count++;
Expand Down
45 changes: 45 additions & 0 deletions Library/BeanstalkPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace TSS\AutomailerBundle\Library;

/**
* Beanstalk queuing plugin for Swiftmailer
*
* @package Automailer
* @author Radu Topala
*/
class BeanstalkPlugin implements \Swift_Events_SendListener
{

public $container;

public function __construct($container)
{
$this->container = $container;
}

/**
* Not used.
*/
public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
{
}

/**
* Invoked immediately after the Message is sent.
*
* @param Swift_Events_SendEvent $evt
*/
public function sendPerformed(\Swift_Events_SendEvent $evt)
{
if($this->container->has("leezy.pheanstalk")) {

//put a job into pheanstalk default connection, in automailer queue
$pheanstalk = $this->container->get("leezy.pheanstalk");

$pheanstalk
->useTube('automailer')
->put("swiftmailer:spool:send --time-limit=500");
}
}
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

<parameters>
<parameter key="swiftmailer.spool.automailer.class">TSS\AutomailerBundle\Library\AutomailerSpool</parameter>
<parameter key="automailer.plugin.beanstalk.class">TSS\AutomailerBundle\Library\BeanstalkPlugin</parameter>
</parameters>

<services>
<service id="swiftmailer.spool.automailer" class="%swiftmailer.spool.automailer.class%" public="false">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>

<service id="automailer.plugin.beanstalk" class="%automailer.plugin.beanstalk.class%" public="false">
<argument type="service" id="service_container" />
</service>
</services>

</container>
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"symfony/swiftmailer-bridge": "2.1.*",
"symfony/swiftmailer-bundle": ">=2.1.0"
},
"suggest": {
"beanstalkd-binary": "http://kr.github.com/beanstalkd/",
"leezy/pheanstalk-bundle": "Allows integration with beanstalk queue, through internal BeanstalkPlugin"
},
"autoload": {
"psr-0": {
"TSS\\AutomailerBundle": ""
Expand Down

0 comments on commit 591817c

Please sign in to comment.