Last active
December 7, 2017 13:58
-
-
Save uneak/ba454dde1d67c47bd9c2da743704cc67 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 $dir; | |
protected $lineByProcess = 600; | |
protected $processMaxTime = 60; | |
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); | |
$this->prospectMessageHelper = $this->getContainer()->get('uneak.prospect.message.helper'); | |
$this->dir = $this->getContainer()->get("kernel")->getRootDir() . '/../process'; | |
while($this->process($time_start)) { | |
sleep(5); | |
}; | |
} | |
private function process($time_start) { | |
$usage = file_exists('/tmp/usage.txt'); | |
$time = microtime(true) - $time_start; | |
if ($usage || $time > $this->processMaxTime) { | |
return false; | |
} | |
$files = glob($this->dir."/*.process"); | |
$file = reset($files); | |
if (!$file) { | |
return false; | |
} | |
$lineByProcess = $this->lineByProcess; | |
$contents = file_get_contents($file, LOCK_UN); | |
$lines = explode(PHP_EOL, $contents); | |
while ($lineByProcess > 0 && count($lines) && !$usage && $time < $this->processMaxTime) { | |
$line = array_shift($lines); | |
$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"]); | |
} | |
$this->prospectMessageHelper->saveProspectMessage($prospectMessage); | |
} | |
} | |
$lineByProcess--; | |
$usage = file_exists('/tmp/usage.txt'); | |
$time = microtime(true) - $time_start; | |
} | |
if (!count($lines)) { | |
unlink($file); | |
} else { | |
file_put_contents($file, join(PHP_EOL, $lines), LOCK_UN); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment