Created
March 26, 2015 01:43
-
-
Save rei999/88e39ff41870faf301dc to your computer and use it in GitHub Desktop.
Kenneth Sample Code - Task to send queued messages to Amazon SNS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MoonLight\MainBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use MoonLight\MainBundle\Entity\Push; | |
use Aml\UserBundle\Entity\User; | |
use Aws\Sns\SnsClient; | |
use Aws\Sns\Exception\EndpointDisabledException; | |
/** | |
* Task to push notifications to Amazon SNS | |
* | |
* Usage: app/console aml:sns --num=100 | |
*/ | |
class AmlSnsCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
// num is the number of messages to be sent by SNS | |
$this | |
->setName('aml:sns') | |
->setDescription('Sns Push Notification for StudyFlashcard') | |
->addOption( | |
'num', | |
null, | |
InputOption::VALUE_REQUIRED, | |
'num of push messages to process from mysql at once' | |
); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln('AmlSns started'); | |
$numToProcess = $input->getOption('num'); | |
$output->writeln('numToProcess=' . $numToProcess); | |
$container = $this->getApplication()->getKernel()->getContainer(); | |
$snsClient = $container->get('platinum_pixs_aws.base')->get('sns'); | |
$em = $container->get('doctrine')->getManager(); | |
$pushMessages = $em->getRepository('MoonLightMainBundle:Push')->findTop($numToProcess, false); | |
$pushCount = count($pushMessages); | |
$lastId = null; // the last id which a messaged is pushed to SNS | |
$didUseDB = false; | |
if($pushCount > 0) { | |
foreach($pushMessages as $push) { | |
$lastId = $push->getId(); | |
$arn = $push->getArn(); | |
if($arn != null) { | |
$aMsg = array('id' => $push->getId(), 'subject' => $push->getSubject(), 'message' => $push->getMessage(), 'type' => $push->getEntityType(), 'entity_id' => $push->getEntityId()); | |
$aMsg = json_encode($aMsg); | |
try { | |
$snsClient->publish(array( | |
'Message' => $aMsg, | |
'TargetArn' => $arn | |
)); | |
// when user deregisters from SNS, delete the SNS device token from DB | |
} catch (\Aws\Sns\Exception\EndpointDisabledException $ede) { | |
$arnUser = $em->getRepository('AmlUserBundle:User')->findByArn($arn); | |
if($arnUser != null) { | |
$arnUser->setArn(null); | |
$em->persist($arnUser); | |
$didUseDB = true; | |
} | |
} | |
} | |
} | |
} | |
if($didUseDB) { | |
$em->flush(); | |
} | |
$numDeleted = $em->getRepository('MoonLightMainBundle:Push')->deleteEverythingBeforeId($lastId); | |
$output->writeln('numDeleted=' . $numDeleted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment