We have an application with million events go through every hour. Using RabbitMQ and PHP-AMQP we process them through our own made consumers. Once in a while the connection drops.
Consumers are running on DO App Platform (worker) and the RabbitMQ is a droplet.
Consumer is a Laravel Command.
public function handle() {
$queuesToListen = $this->constructQueues();
$queueJobs = $this->extractQueueJobs($this->queues);
if(is_array($queuesToListen) && count($queuesToListen) > 0){
$this->output->writeln('<fg=green>[EVENTS] Found ' . count($queuesToListen) . ' queues to subscribe to, starting to listen</>');
while(true) {
foreach ($queuesToListen as $queueName) {
$this->output->writeln('<fg=yellow>Let\'s listen to queue ' . $queueName." </>");
$this->currentQueue = $queueName;
try {
$connection = AMQPStreamConnection::create_connection([
['host' => env('RABBITMQ_HOST'), 'port' => env('RABBITMQ_PORT'), 'user' => env('RABBITMQ_USER'), 'password' => env('RABBITMQ_PASSWORD'), 'vhost' => env('RABBITMQ_VHOST')]
], ['heartbeat' => constant($queueJobs[$this->currentQueue] . '::HEARTBEAT_RATE')]);
$channel = $connection->channel();
$channel->queue_declare($queueName, false, true, false, false);
echo ' [*] Connected to queue ' . $queueName . ' , waiting for messages.', "\n";
$channel->basic_qos(null, 10, null);
$channel->basic_consume($queueName, '', false, false, false, false, [$this, 'processEvent']);
// Loop as long as the channel has callbacks registered
while (count($channel->callbacks)) {
$channel->wait(null, false, 2);
}
} catch (\PhpAmqpLib\Exception\AMQPExceptionInterface $e) {
echo "AMQP Exception: ", $e->getMessage(), "\n";
echo "AMQP Code: ", $e->getCode(), "\n";
$channel->close(); //LINE 72
$connection->close();
}
}
}
}else{
$this->output->writeln('<fg=red>[EVENTS] No queues to process, command stopped</>');
}
}
Error we get are:
PhpAmqpLib\Exception\AMQPConnectionClosedException: Broken pipe or closed connection
#19 /vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php(235): PhpAmqpLib\Wire\IO\StreamIO::write
#18 /vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(408): PhpAmqpLib\Connection\AbstractConnection::write
#17 /vendor/php-amqplib/php-amqplib/PhpAmqpLib/Connection/AbstractConnection.php(536): PhpAmqpLib\Connection\AbstractConnection::send_channel_method_frame
#16 /vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php(248): PhpAmqpLib\Channel\AbstractChannel::send_method_frame
#15 /vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AMQPChannel.php(198): PhpAmqpLib\Channel\AMQPChannel::close
#14 /vendor/sdk-blueprint-service/src/RabbitMQ/Console/Consume.php(72):BluePrint\RabbitMQ\Console\Consume::handle
#13 /vendor/illuminate/container/BoundMethod.php(36): Illuminate\Container\BoundMethod::Illuminate\Container{closure}
#12 /vendor/illuminate/container/Util.php(40): Illuminate\Container\Util::unwrapIfClosure
#11 /vendor/illuminate/container/BoundMethod.php(93): Illuminate\Container\BoundMethod::callBoundMethod
#10 /vendor/illuminate/container/BoundMethod.php(37): Illuminate\Container\BoundMethod::call
#9 /vendor/illuminate/container/Container.php(653): Illuminate\Container\Container::call
#8 /vendor/illuminate/console/Command.php(136): Illuminate\Console\Command::execute
#7 /vendor/symfony/console/Command/Command.php(298): Symfony\Component\Console\Command\Command::run
#6 /vendor/illuminate/console/Command.php(121): Illuminate\Console\Command::run
#5 /vendor/symfony/console/Application.php(1024): Symfony\Component\Console\Application::doRunCommand
#4 /vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application::doRun
#3 /vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application::run
#2 /vendor/illuminate/console/Application.php(94): Illuminate\Console\Application::run
#1 /vendor/laravel/lumen-framework/src/Console/Kernel.php(116): Laravel\Lumen\Console\Kernel::handle
#0 /artisan(35): null
Whenever this error occurs the application stops processing the events. And we manually have to redeploy the application.
How can we prevent this from crashing? Is there a way to detect if a channel is still open?
Related
I am developing a custom wp plugin that deals with twitter so I am using this as repository https://github.com/J7mbo/twitter-api-php
This is my code for the shortcode:
function ct_twitter_shortcode_func( $atts ){
ob_start();
include_once '/includes/twitter-api-php-master/TwitterAPIExchange.php';
$ct_twitter_keys = get_option("ct_twitter_keys");
$settings = array(
'oauth_access_token' => $ct_twitter_keys["OAUTH_ACCESS_TOKEN"],
'oauth_access_token_secret' => $ct_twitter_keys["OAUTH_ACCESS_TOKEN_SECRET"],
'consumer_key' => $ct_twitter_keys["CONSUMER_KEY"],
'consumer_secret' => $ct_twitter_keys["CONSUMER_SECRET"]
);
//echo dirname(__FILE__) . '/cacert.pem';
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$getfield = '?screen_name=bikegearup&count=20';
$twitter = new TwitterAPIExchange($settings);
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => '/includes/twitter-api-php-master/cacert.pem'));
return ob_get_clean();
}
add_shortcode( 'ct_twitter', 'ct_twitter_shortcode_func' );
but I got this kind of error:
Fatal error: Uncaught exception 'Exception' with message 'error setting certificate verify locations: CAfile: /includes/twitter-api-php-master/cacert.pem CApath: none' in C:\xampp\htdocs\pluto\wp-content\plugins\ct-socials\shortcodes\ct-twitter\includes\twitter-api-php-master\TwitterAPIExchange.php:321 Stack trace: #0 C:\xampp\htdocs\pluto\wp-content\plugins\ct-socials\shortcodes\ct-twitter\class-fw-shortcode-ct-twitter.php(23): TwitterAPIExchange->performRequest(true, Array) #1 [internal function]: ct_twitter_shortcode_func(Array, '', 'ct_twitter') #2 C:\xampp\htdocs\pluto\wp-includes\shortcodes.php(345): call_user_func('ct_twitter_shor...', Array, '', 'ct_twitter') #3 [internal function]: do_shortcode_tag(Array) #4 C:\xampp\htdocs\pluto\wp-includes\shortcodes.php(223): preg_replace_callback('/\[(\[?)(ct_soc...', 'do_shortcode_ta...', '[ct_twitter sty...') #5 [internal function]: do_shortcode('[ct_twitter sty...') #6 C:\xampp\htdocs\pluto\wp-includes\class-wp-hook.php(298): call_user_func_array('do_shortcode', Arra in C:\xampp\htdocs\pluto\wp-content\plugins\ct-socials\shortcodes\ct-twitter\includes\twitter-api-php-master\TwitterAPIExchange.php on line 321
I also tried to include https://curl.haxx.se/ca/cacert.pem but still it won't work.. can anyone help me with this?
any help will be appreciated, thanks in advance...
Hi im new in slim 3 framework (Api for mobile) i would like to know how to upload an image to a server, e seacrh for examples didnt find much, this is the code i have but it does not work.Any help would be great thanks.
$app->post('/photo', function ($request, $response) use ($app) {
$files = $request->getUploadedFiles();
if (empty($files['newfile'])) {
throw new Exception('Expected a newfile');
}
$newfile = $files['newfile'];
if ($newfile->getError() === UPLOAD_ERR_OK) {
$uploadFileName = $newfile->getClientFilename();
$newfile->moveTo("../photos/");
}
});
The error it gives me
<h1>Slim Application Error</h1>
<p>The application could not run because of the following error:</p>
<h2>Details</h2>
<div>
<strong>Type:</strong> Error
</div>
<div>
<strong>Message:</strong> Call to a member function getError() on null
</div>
<div>
<strong>File:</strong> /home/bitstudi/public_html/api_pricegram/public/index.php
</div>
<div>
<strong>Line:</strong> 155
</div>
<h2>Trace</h2>
#0 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#1 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(476): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(370): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 /home/bitstudi/public_html/api_pricegram/vendor/slim/slim/Slim/App.php(295): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 /home/bitstudi/public_html/api_pricegram/public/index.php(161): Slim\App->run()
#10 {main}
i solved like this
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Slim\Http\UploadedFile;
require '../vendor/autoload.php';
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
$container = $app->getContainer();
$container['upload_directory'] ='../photos/';
$app->post('/photo', function (Request $request, Response $response) use ($app) {
$directory = $this->get('upload_directory');
$uploadedFiles = $request->getUploadedFiles();
$uploadedFile = $uploadedFiles['picture'];
if($uploadedFile->getError() === UPLOAD_ERR_OK) {
$filename = moveUploadedFile($directory, $uploadedFile);
$response->write('uploaded ' . $filename . '<br/>');
}
});
function moveUploadedFile($directory, UploadedFile $uploadedFile){
$extension = pathinfo($uploadedFile->getClientFilename(),
PATHINFO_EXTENSION);
$basename = bin2hex(random_bytes(8));
$filename = sprintf('%s.%0.8s', $basename, $extension);
$uploadedFile->moveTo($directory . DIRECTORY_SEPARATOR . $filename);
return $filename;
}
$app->run();
I trying to call a web service using SOAP protocol with PHP, but it doesn't work.
The url on documentation is:
URL: http://api.teleport.com.br/wsdl/Teleport
My code:
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$url = "http://api.teleport.com.br/wsdl/Teleport";
try {
$client = new SoapClient($url);
$function = 'ConsultaVeiculos';
$arguments= array('ConsultaVeiculos' => array(
"senha" => "*************"
));
$options = array('location' => $url);
$result = $client->__soapCall($function, $arguments);
print_r($result);
} catch (Exception $e) {
print $result." = ".$e;
}
The error:
= SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't find in 'http://api.teleport.com.br/wsdl/Teleport' in PATH: SoapClient->SoapClient('http://api.tele...') #1 [internal function]: FUNCION->testSOAPService() #2 PATH/CodeIgniter.php(359): call_user_func_array(Array, Array) #3 PATH(219): require_once(PATH) #4 {main}
How to correctly do this?
Thanks
I am having problems getting a list of users for accounts
If I put the account number in the try me page (https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountUserLinks/list) it works
$client = new Google_Client();
$client->setAuthConfigFile($_SERVER['DOCUMENT_ROOT'] . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_MANAGE_USERS);
$client=gaGetClient();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
//this doesn't work
$accountUserlinks=$analytics->management_accountUserLinks->listManagementAccountUserLinks('123456');
//this works
$man_accounts = $analytics->management_accounts->listManagementAccounts();
$accounts = [];
foreach ($man_accounts['items'] as $account) {
$accounts[] = [ 'id' => $account['id'], 'name' => $account['name'] ];
echo $account['name'] . " (" . $account['id'] . ")<br/>";
}
}
I get this error message
Fatal error: Uncaught exception 'Google_Service_Exception' with
message 'Error calling GET
https://www.googleapis.com/analytics/v3/management/accounts/123456/entityUserLinks:
(403) Insufficient Permission' in
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php:110
Stack trace: #0
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(62):
Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request),
Object(Google_Client)) #1 [internal function]:
Google_Http_REST::doExecute(Object(Google_Client),
Object(Google_Http_Request)) #2
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Task/Runner.php(174):
call_user_func_array(Array, Array) #3
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php(46):
Google_Task_Runner->run() #4
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Goo
in
/home/grant/www/productaudit.hcpservices.co.uk/htdocs/current/google-api-php-client/src/Google/Http/REST.php
on line 110
Can anyone think what I am doing wrong. I am thinking it is a scope issue, but I have stated my scopes
Thanks
Grant
I am unable to send mail using localhost connection and correct code . But getting this error
Zend\Mail\Protocol\Exception\RuntimeException
File:
/var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/AbstractProtocol.php:348
Message:
5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsl
5.7.14 6EWOp6ZKxNQid3Q84gVgHX7gRcrLH3FRL88s1Y-aurZXgnxEziLaRJrbc2OxtCgugbO98W
5.7.14 CMXNsqYvCG5EwG5iP9GU2N3NErFFURRUqffsIRCJW_u7_4WdRXu_Rb1mZjTITQm8VexWbn
5.7.14 tDk2rYv_9cv7NAcQUi5eItDC9HhF0mKns_yLRj1Rsl3nz0sQmn0rtfiJkg_mF8hQelwSAZ
5.7.14 F943KqTxTU3tWiZzO54KNZdUd3YWI> Please log in via your web browser and
5.7.14 then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 tn5sm2706283pac.32 - gsmtp
Stack trace:
#0 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp/Auth/Login.php(83): Zend\Mail\Protocol\AbstractProtocol->_expect(235)
#1 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Protocol/Smtp.php(193): Zend\Mail\Protocol\Smtp\Auth\Login->auth()
#2 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(340): Zend\Mail\Protocol\Smtp->helo('localhost')
#3 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(325): Zend\Mail\Transport\Smtp->connect()
#4 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(336): Zend\Mail\Transport\Smtp->lazyLoadConnection()
#5 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mail/Transport/Smtp.php(205): Zend\Mail\Transport\Smtp->connect()
#6 /var/www/html/rustagi/module/ZfcAdmin/src/ZfcAdmin/Controller/AdminController.php(185): Zend\Mail\Transport\Smtp->send(Object(Zend\Mail\Message))
#7 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): ZfcAdmin\Controller\AdminController->SendMailAction()
#8 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#13 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#14 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#15 /var/www/html/rustagi/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 /var/www/html/rustagi/vendor/ZF2/library/Zend/Mvc/Application.php(313): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#17 /var/www/html/rustagi/index.php(17): Zend\Mvc\Application->run()
#18 {main}
I am using this code :-
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 587,
'connection_class' => 'login',
'connection_config' => array(
'username' => 'someone#gmail.com',
'password' => '********',
'ssl'=> 'tls',
),
));
$fileContents = fopen("/usr/share/pixmaps/faces/sky.jpg", 'r');
$attachment = new Mime\Part($fileContent);
$attachment->type = 'image/jpg';
$attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
// $this->renderer = $this->getServiceLocator()->get('ViewRenderer');
$content = "gdgdfgdfgdfgddfg";
// make a header as html
$html = new MimePart($content);
$html->type = "text/html";
$body = new MimeMessage();
$body->setParts(array($html,$attachment));
// instance mail
$mail = new Mail\Message();
$mail->setBody($body); // will generate our code html from template.phtml
$mail->setFrom('someone#gmail.com','Sender Name');
$mail->setTo('someone1#gmail.com');
$mail->setSubject('Your Subject');
$transport = new Mail\Transport\Smtp($options);
$transport->send($mail);
How can i achieve this .I've searched alot on this but getting no solution for this your help will be highly appriciated thanx.
Sorry if i'm digging up this subject but i got the same problem. The send function seems to not works but there is no error message...
Here is a part of my code:
$mail = new Mail\Message();
$options = new Mail\Transport\SmtpOptions(array(
'name' => 'localhost',
'host' => 'smtp.gmail.com',
'port'=> 465,
'connection_class' => 'login',
'connection_config' => array(
'username' => 'someone#gmail.com',
'password' => '***',
'ssl'=> 'ssl',
),
));
$mail->setBody("This is the text of the email");
$mail->setFrom('noreply#reims.fr','Ecole Municipal des Sports');
$mail->addTo('someone#gmail.com');
$mail->setSubject('Inscription annulée');
$transport = new Mail\Transport\Sendmail('someone#gmail.com',$options);
$transport->send($mail);
var_dump($mail);
var_dump($transport);
var_dump($transport->send($mail));
change line
'ssl'=> 'tls' // tls causes problems when sending via gmail
to
'ssl'=> 'ssl'
be sure that in the configuration ini there is no overwriting entry
smtp.params.ssl = "tls" // this is bad -> tls causes problems when sending via gmail
Nothing to do with TLS. You should also allow less secure apps through your gmail account here. Worked for me..!!
see your configuration is false
using ssl or tls?
i suggest to you using phpmailer script
first active SMTP service from php.ini
search: [mail function]
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
and next time you must using PHPMailer class ~>
Download PHPMailer
template function:
Use this ↓
<?php
require_once ("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer(true);
$mail->isSMTP();
try{
$mail->host = "smtp#gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "username#email.com";
$mail->Password = "yourPassword";
$mail->AddAddress("target#email.com");
$mail->SetForm("yourMail#Email.com","your name");
$mail->Subject = "emailSubject";
$mail->CharSet = "UTF-8";
$mail->ContentType = "text/htm";
$mail->msgHTML("MailContent");
$mail->send();
echo 'Seccess';
}
catch(phpmailerException $mail){
echo $mail->errorMessage();
}
catch(Exception $mail){
echo $mail->getMessage();
}
?>