/** * Order a pizza. * * @param Request $request * @param $id The pizza id. * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response */ public function orderAction(Request $request, $id) { $apiUrl = $this->getParameter("api_url"); $client = new Client(); $container = $this->container; if ($this->getBreaker()->isAvailable('order')) { $LibratoClient = new LibratoClient($container->getParameter("librato_email"), $container->getParameter("librato_token")); $LibratoClient->post('/metrics', array('gauges' => array(array('name' => 'CircuitBreaker', 'value' => 0)))); try { $res = $client->request('POST', $apiUrl . '/orders', ['json' => ['id' => (int) $id], 'timeout' => 30, 'on_stats' => function (TransferStats $stats) use($container) { if ($stats->hasResponse()) { $client = new LibratoClient($container->getParameter("librato_email"), $container->getParameter("librato_token")); $client->post('/metrics', array('gauges' => array(array('name' => 'ResponseTime', 'value' => $stats->getTransferTime()), array('name' => 'ResponseStatusCode', 'value' => $stats->getResponse()->getStatusCode()), array('name' => 'ApiRequested', 'value' => 1)))); } }]); $command = json_decode($res->getBody()->getContents(), true); $this->addFlash('success', "Votre commande " . $command['id'] . " a bien été passée !"); return $this->redirect($this->generateUrl('pizzapi_core_homepage')); } catch (\Exception $e) { $this->getBreaker()->reportFailure('order'); $LibratoClient = new LibratoClient($container->getParameter("librato_email"), $container->getParameter("librato_token")); $LibratoClient->post('/metrics', array('gauges' => array(array('name' => 'CircuitBreaker', 'value' => 1)))); $content = $this->render('TwigBundle:Exception:error404.html.twig'); return new Response($content, 404, array('Content-Type', 'text/html')); } } $content = $this->render('TwigBundle:Exception:error404.html.twig'); return new Response($content, 404, array('Content-Type', 'text/html')); }
protected function execute(InputInterface $input, OutputInterface $output) { $apiUrl = $this->getContainer()->getParameter('api_url'); $redis = $this->getContainer()->get('snc_redis.cache'); try { $client = new Client(); $res = $client->request('GET', $apiUrl . "/pizzas", ['source' => 'get/pizzas', 'on_stats' => function (TransferStats $stats) { if ($stats->hasResponse()) { $client = new LibratoClient($this->getContainer()->getParameter("librato_email"), $this->getContainer()->getParameter("librato_token")); $client->post('/metrics', array('gauges' => array(array('name' => 'ResponseTime', 'value' => $stats->getTransferTime(), 'source' => 'get/pizzas'), array('name' => 'ResponseStatusCode', 'value' => $stats->getResponse()->getStatusCode(), 'source' => 'get/pizzas'), array('name' => 'ApiRequested', 'value' => 2, 'source' => 'get/pizzas')))); } }]); $pizzaList = json_decode($res->getBody()->getContents(), true); $redis->set('pizzas', json_encode($pizzaList)); $output->writeln("Update redis cache completed !"); } catch (\Exception $e) { var_dump($e->getMessage()); die; } }
/** * Kicks everything off * * @return void */ public function main() { // Build up the data we are sending to Librato $data = array('title' => $this->getTitle(), 'start_time' => time()); $description = $this->getDesc(); if ($description !== null) { $data['description'] = $description; } // Send annotation to Librato $client = new Client($this->getUsername(), $this->getPassword()); $result = $client->post('/annotations/' . $this->getName(), $data); // Did Librato accept the annotation if (!isset($result->errors)) { // Successful $this->log('Successfully sent annotation to Librato', Project::MSG_INFO); } else { // Failed $msg = 'Failed to send annotation to Librato: ' . $result->errors->request[0]; if ($this->haltOnFailure) { throw new BuildException($msg); } else { $this->log($msg, Project::MSG_ERR); } } }