Php symfony2命令被杀死未知原因
我得到了一个短信cronjob运行与我的服务器每秒钟与flock 而我的剧本被杀的异常,为什么我说它是异常的 有时,我的脚本运行并发送大约2000条短信,没有任何问题 有时现在,我的脚本运行一个新的批173短信和总是有被杀随机 调度员来了Php symfony2命令被杀死未知原因,php,symfony,doctrine,Php,Symfony,Doctrine,我得到了一个短信cronjob运行与我的服务器每秒钟与flock 而我的剧本被杀的异常,为什么我说它是异常的 有时,我的脚本运行并发送大约2000条短信,没有任何问题 有时现在,我的脚本运行一个新的批173短信和总是有被杀随机 调度员来了 <?php namespace MPN\Sms2Bundle\Service; use Doctrine\ORM\EntityManager; use Symfony\Component\EventDispatcher\EventDispatcherI
<?php
namespace MPN\Sms2Bundle\Service;
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use MPN\CRMBundle\Service\UserAwareService;
use MPN\Sms2Bundle\Service\Calculator;
use MPN\Sms2Bundle\Service\Renderer;
use MPN\Sms2Bundle\Manager\SettingManager;
use MPN\DataBundle\Entity\Contact\Contact;
use MPN\DataBundle\Entity\Contact\Phone as ContactPhone;
use MPN\Sms2Bundle\Entity\Sms;
use MPN\Sms2Bundle\Entity\SmsConstants;
use MPN\Sms2Bundle\Entity\Batch;
use MPN\Sms2Bundle\Entity\History;
use MPN\Sms2Bundle\SmsEvents;
use MPN\Sms2Bundle\Event\FailureEvent;
/**
* This class provides low-level methods for sending `sms`.
*
* @see \MPN\Sms2Bundle\Service\Sender::sendNow() for high-level method
*/
class Dispatcher extends UserAwareService
{
private $em;
private $eventDispatcher;
private $calculator;
private $renderer;
private $settingManager;
private $username;
private $password;
private $sender;
public function __construct(EntityManager $em, EventDispatcherInterface $eventDispatcher, SettingManager $settingManager, Calculator $calculator, Renderer $renderer, SecurityContextInterface $securityContext, $username, $password, $sender)
{
$this->em = $em;
$this->eventDispatcher = $eventDispatcher;
$this->settingManager = $settingManager;
$this->calculator = $calculator;
$this->renderer = $renderer;
$this->username = $username;
$this->password = $password;
$this->sender = $sender;
$this->setSecurityContext($securityContext);
}
/**
* Sends $sms to $contactPhone.
*
* You should check if credit is sufficient before sending $sms.
* This method ignores the contact's status, i.e. if it is soft-deleted.
*
* @see \MPN\Sms2Bundle\Service\Calculator::hasEnoughCreditForOneTime() for checking credit balance
* @param string|ContactPhone $contactPhone
* @return boolean|integer Credit spent or FALSE if failed to send.
*/
public function send(Sms $sms, $contactPhone)
{
if (is_string($contactPhone)) {
$mobile = $contactPhone;
$contact = null;
} else {
$mobile = $contactPhone->getNumber();
$contact = $contactPhone->getContact();
}
$content = $this->renderer->render($sms, $contact);
$credit = false;
if ($this->_send($mobile, $content))
$credit = $this->calculator->calculateCredit($sms, $contact);
return $credit;
}
/**
* Sends $sms to all filtered and non-deleted linked contact phones.
* Credits are *DEDUCTED* here.
*
* You should check if credit is sufficient to send $sms.
*
* @see \MPN\Sms2Bundle\Service\Calculator::hasEnoughCreditForOneTime() for checking credit balance
* @param \MPN\Sms2Bundle\Entity\Sms $sms
* @param boolean $andFlush
*/
public function sendToAll(Sms $sms, $andFlush = true)
{
$recipients = array();
if ($sms->getToAllContactTypes() || $sms->getToAllContactGroups() || $sms->getToAllContactPhones()) {
$contacts = $this->em->getRepository('MPN\DataBundle\Entity\Contact\Contact')->findBy(array(
'deleted' => false,
));
foreach ($contacts as $contact)
$recipients = array_merge($recipients, $contact->getMobilePhones());
} else {
$contactTypes = $sms->getContactTypes();
foreach ($contactTypes as $contactType)
$recipients = array_merge($recipients, $contactType->getMobilePhones());
$contactGroups = $sms->getContactGroups();
foreach ($contactGroups as $contactGroup)
$recipients = array_merge($recipients, $contactGroup->getMobilePhones());
$contactPhones = $sms->getContactPhones();
foreach ($contactPhones as $contactPhone)
$recipients[] = $contactPhone;
$company = $sms->getCompany();
if (null !== $company && $company->hasContacts()) {
$recipients = array_merge($recipients, $company->getMobilePhones());
}
}
$recipients = array_unique(array_merge($recipients, $sms->getGeneralContactsAsArray()));
// Create a Batch instance and *flush*
$batch = new Batch();
$batch->setDeliveredAt(new \DateTime());
$sms->addBatch($batch); // add batch increase sentCount by 1
$this->em->flush();
$failedContacts = array();
foreach ($recipients as $contactPhone) {
$isContactPhoneInstance = $contactPhone instanceof ContactPhone;
if ($isContactPhoneInstance) {
$contact = $contactPhone->getContact();
if (!$this->isOwner($contact)
|| $contact->isDeleted()
|| !$sms->testTargetContact($contact)) {
continue;
}
//Check if not mobile number, mean not start with 601, skip it
// if (substr($contact->getPhone(), 0, 3) != '601') {
// continue;
// }
}
$credit = $this->send($sms, $contactPhone);
print ".";
$isSuccessful = $credit !== false;
$history = new History();
if ($isContactPhoneInstance) {
$contact = $contactPhone->getContact();
$content = $this->renderer->render($sms, $contact);
$history
->setRecipientName($contact->getFullName())
->setRecipientNumber($contactPhone->getNumber())
;
} else {
$content = $this->renderer->render($sms);
$history
->setRecipientName('General Contact')
->setRecipientNumber($contactPhone)
;
}
// Notify listeners to handle this failure
if ($isContactPhoneInstance && !$isSuccessful && !in_array($contact, $failedContacts)) {
$event = new FailureEvent($sms, $contact, $content);
$this->eventDispatcher->dispatch(SmsEvents::FAILURE, $event);
// Record this contact so mail won't be sent more than one time
$failedContacts[] = $contact;
}
$history
->setContent($content)
->setCredit((int) $credit)
->setSuccess($isSuccessful)
;
$batch
->addHistory($history)
;
$this->em->flush();
// decrease the credit *now*
// failed to send messages cost 0 credit, so doesn't matter!
$this->settingManager->decreaseCredit($credit);
}
if ($andFlush)
$this->em->flush();
return $batch->getCredit();
}
/**
* Sends a message without logging any information.
*
* @param string $mobile
* @param string $content
*
* @return bool
*/
public function quickSend($mobile, $content)
{
return $this->_send($mobile, $content);
}
/**
* Actually sends the message out.
*
* @param string $mobile
* @param string $content
*
* @return bool
*/
private function _send($mobile, $content)
{
$url = $this->buildUrl($mobile, $content);
$response = @file_get_contents($url);
return !!preg_match('/^1701:\d+$/', $response);
}
private function buildUrl($mobile, $content)
{
// $url = 'http://www.example.com/websmsapi/isendsms.aspx?username=%s&password=%s&sender=%s&type=%d&mobile=%s&message=%s';
// Fake sms server, record all the record only
$url = 'http://example.my/sendsms.php?mobile=%s&message=%s';
$encodedContent = $this->renderer->encode($content);
if ($this->renderer->isUnicodeContent($content))
$type = 3;
else
$type = 1;
// return sprintf(
// $url,
// $this->username,
// $this->password,
// $this->sender,
// $type,
// $mobile,
// $encodedContent
// );
return sprintf(
$url, $mobile, $encodedContent
);
}
}
我将消息保存在日志中,以便我可以查看
在我的调度程序中,每个继任者都会失败
印刷品
我查了一下我的日志文件,有……被杀了
有时……被杀
我如何检查为什么会发生死亡?任何我应该如何修复或调试它,错误消息是如此简单,刚刚被杀死,没有其他消息让我检查它…如果你想阅读更多的源代码,请告诉我,非常感谢谁在这方面的帮助,首先感谢
**update 1:**
我在谷歌上搜索了一段时间后,很多人说这是因为内存泄漏导致的错误,所以我添加了这个
print(sprintf('Memory usage (currently) %dKB/ (max) %dKB', round(memory_get_usage(true) / 1024), memory_get_peak_usage(true) / 1024));print("\r\n");
在dispatcher foreach$recipients中,作为$contactPhone{
下面是结果
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
当服务器没有配置交换并且进程内存不足时,我看到很多这样的事件被杀死。如果你运行循环一段时间,你可能会在某个地方出现内存泄漏。另外,请检查fingers\u Cross logging handler,因为它会缓冲所有东西,以防你需要它。fingers\u Cross log?你是指symfony错误日志吗?我已经在一个地方检查过了pp/logs/prod.log和dev.log,我没有发现任何特殊之处。cron每秒钟运行一次,检查是否有可用的sms发送,foreach循环它如果有,如果内存泄漏,2000++sms如何发送而没有问题?您可能希望实现队列消息传递。顺便说一句,您可以启用php cli e错误日志,并确保日志文件可由php cli写入。
print(sprintf('Memory usage (currently) %dKB/ (max) %dKB', round(memory_get_usage(true) / 1024), memory_get_peak_usage(true) / 1024));print("\r\n");
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114432KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB
Memory usage (currently) 114688KB/ (max) 114688KB