Skip to content

Instantly share code, notes, and snippets.

@uneak
Created January 24, 2018 14:27
Show Gist options
  • Save uneak/534a34b190324a20bd73fa24081113d5 to your computer and use it in GitHub Desktop.
Save uneak/534a34b190324a20bd73fa24081113d5 to your computer and use it in GitHub Desktop.
<?php
namespace CampaignBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Uneak\HighConnexionSmsBundle\Sms\HighConnexionMessage;
class MessageStatusCommand extends ContainerAwareCommand {
protected $prospectMessageHelper;
protected $lineByProcess = 600;
protected $processMaxTime = 55;
/**
* @var \Memcached
*/
protected $cache;
protected function configure() {
$this
->setName('campaign:message:status')
->setDescription('Traitement des status des messages')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$time_start = microtime(true);
$output->writeln("----------");
$output->writeln("++ start at ".$time_start);
$this->cache = new \Memcached();
$this->cache->addServer('localhost', 11211);
$this->prospectMessageHelper = $this->getContainer()->get('uneak.prospect.message.helper');
while($this->process($time_start, $output)) {
$output->writeln("@@ sleep 5s");
sleep(5);
};
$time_end = microtime(true);
$time = $time_end - $time_start;
$output->writeln("-- end at ".microtime(true));
$output->writeln("xx total time :".$time);
}
private function process($time_start, OutputInterface $output) {
$output->writeln("## START process ". microtime(true));
// is usage existe, le server est debordé
$isServerBusy = file_exists('/tmp/usage.txt');
$time = microtime(true) - $time_start;
$output->writeln(sprintf("-> test -> usage : %s", ($isServerBusy ? "KO" : "OK")));
$output->writeln(sprintf("-> test -> time : %s" , (($time > $this->processMaxTime) ? "KO" : "OK")));
if ($isServerBusy || $time > $this->processMaxTime) {
$output->writeln("## ! BREAK process");
return false;
}
$keys = $this->cache->getAllKeys();
$output->writeln(sprintf("-> test -> memcached exist : %s", ((!count($keys)) ? "KO" : "OK")));
if (!count($keys)) {
$output->writeln("## ! BREAK process");
return false;
}
$lineByProcess = $this->lineByProcess;
while ($lineByProcess > 0 && count($keys) && !$isServerBusy && $time < $this->processMaxTime) {
$key = array_shift($keys);
$line = $this->cache->get($key);
$this->cache->delete($key);
$output->writeln(sprintf("-> test -> memcached : %s", $line));
$all = unserialize($line);
if (isset($all["ret_id"])) {
$prospectMessage = $this->prospectMessageHelper->getProspectMessageById(intval($all["ret_id"]));
if ($prospectMessage) {
$prospectMessage->setLog($all);
$prospectMessage->setMessage((isset($all["text"])) ? $this->prospectMessageHelper->_smsDecode($all["text"]) : null);
$prospectMessage->setExternalId((isset($all["push_id"])) ? $all["push_id"] : null);
$prospectMessage->setReceiver((isset($all["to"])) ? $all["to"] : null);
$externalStatus = (isset($all["status"])) ? $all["status"] : null;
$prospectMessage->setExternalStatus($externalStatus);
$prospectMessage->setStatus(HighConnexionMessage::PROSPECT_MESSAGE_STATUS($externalStatus));
if (isset($all["dst_ext_id"])) {
$prospectMessage->setStatusMessage($all["dst_ext_id"]);
}
$output->writeln(sprintf("$$ save prospect : %s", $all["ret_id"]));
$this->prospectMessageHelper->saveProspectMessage($prospectMessage);
}
}
$lineByProcess--;
$isServerBusy = file_exists('/tmp/usage.txt');
$time = microtime(true) - $time_start;
$output->writeln(sprintf("-> test -> usage : %s", ($isServerBusy ? "KO" : "OK")));
$output->writeln(sprintf("-> test -> time : %s" , (($time > $this->processMaxTime) ? "KO" : "OK")));
}
$output->writeln("## END process");
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment