/** * @param $channel * @param $message */ public function consume($channel, $message) { $this->logger->info('consuming message'); if ($channel == 'saveInBulk') { $this->service->saveInBulk($message); } }
/** * Find the next job that should be executed. * By default, just selects any job instance that is in the database that isn't * already executing, hasn't already been finished, and hasn't errored out. * @return a job array (id, job_type, [user_id], [arg_id]) or {@code false} if there is none */ function findJob(Connection $db, Logger $logger) { // TODO timeout jobs // mark all repeatedly failing jobs as failing $execution_limit = Config::get("job_execution_limit", 5); $q = $db->prepare("SELECT * FROM jobs WHERE is_executed=0 AND is_executing=0 AND execution_count >= ?"); $q->execute(array($execution_limit)); if ($failed = $q->fetchAll()) { $logger->info("Found " . number_format(count($failed)) . " jobs that have executed too many times ({$execution_limit})"); foreach ($failed as $f) { $q = $db->prepare("UPDATE jobs SET is_executed=1,is_error=1 WHERE id=?"); $q->execute(array($f['id'])); $logger->info("Marked job " . $f['id'] . " as failed"); } } // find first a job that has zero execution count $q = $db->prepare("SELECT * FROM jobs WHERE " . $this->defaultFindJobQuery() . " AND execution_count=0 LIMIT 1"); $q->execute(); if ($job = $q->fetch()) { return $job; } // or, any job $q = $db->prepare("SELECT * FROM jobs WHERE " . $this->defaultFindJobQuery() . " LIMIT 1"); $q->execute(); return $q->fetch(); }
public function notify(Service\Record $record, DOMDocument $config, Logger $logger) { $activity = $config->getElementsByTagName('activity')->item(0); if ($activity !== null) { $logger->info('Create user activity template'); try { $templates = $activity->childNodes; for ($i = 0; $i < $templates->length; $i++) { $template = $templates->item($i); if (!$template instanceof DOMElement) { continue; } if ($template->nodeName == 'template') { $type = $template->getAttribute('type'); $verb = $template->getAttribute('verb'); $table = $template->getAttribute('table'); $path = $template->getAttribute('path'); $summary = $template->nodeValue; if (isset($this->registry['table.' . $table])) { $table = $this->registry['table.' . $table]; } else { throw new Exception('Invalid table ' . $table); } if (!empty($type) && !empty($verb) && !empty($table) && !empty($summary)) { $this->sql->insert($this->registry['table.user_activity_template'], array('type' => $type, 'verb' => $verb, 'table' => $table, 'path' => $path, 'summary' => $summary)); $logger->info('> Created user activity template'); $logger->info($summary); } } } } catch (\Exception $e) { $logger->error($e->getMessage()); } } }
function storeMarkets($markets, Logger $logger) { $logger->info("Storing " . count($markets) . " markets persistently"); // find all existing markets $existing = $this->getMarkets(true); // remove removed markets foreach ($existing as $pair) { if (array_search($pair, $markets) === false) { $logger->info("Removing pair " . implode("/", $pair)); $q = $this->db->prepare("DELETE FROM exchange_pairs WHERE exchange=? AND currency1=? AND currency2=?"); $q->execute(array($this->exchange->getCode(), $pair[0], $pair[1])); } } // add new markets foreach ($markets as $pair) { if (array_search($pair, $existing) === false) { if (strlen($pair[0]) != 3) { $logger->info("Ignoring currency '" . $pair[0] . "': not three characters long"); continue; } if (strlen($pair[1]) != 3) { $logger->info("Ignoring currency '" . $pair[1] . "': not three characters long"); continue; } $logger->info("Adding pair " . implode("/", $pair)); $q = $this->db->prepare("INSERT INTO exchange_pairs SET exchange=?, currency1=?, currency2=?"); $q->execute(array($this->exchange->getCode(), $pair[0], $pair[1])); } } // reset cache $this->cached_markets = null; }
/** * @test */ public function itShouldCollect() { $this->serializer->normalize(Argument::any())->shouldBeCalled()->willReturn([]); $this->logger->pushHandler(Argument::any())->shouldBeCalled()->willReturn(true); $this->logger->info(Argument::type('string'), Argument::type('array'))->shouldBeCalled(); $this->collector->collect($this->requestObject, ['logFile' => 'test.log']); }
/** * Remove a directory and its contents recursively. Use with caution. */ private function delFileTree($path, $force = false) { if (!file_exists($path)) { return; } if (!is_dir($path)) { $this->logger->info("f: {$path}"); if (file_exists($path) && $force === true) { unlink($path); } return; } $directoryIterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); $fileIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST); foreach ($fileIterator as $file) { if ($file->isDir()) { $this->logger->info("d: {$file->getRealPath()}"); if ($force === true) { rmdir($file->getRealPath()); } } else { $this->logger->info("f: {$file->getRealPath()}"); if ($force === true) { unlink($file->getRealPath()); } } } $this->logger->info("d: {$file->getRealPath()}"); if ($force === true) { rmdir($path); } }
function fetchAllRates(Logger $logger) { $url = "https://api.vircurex.com/api/get_info_for_currency.json"; $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); $result = array(); $ignored = 0; foreach ($json as $pair2 => $pairs) { if ($pair2 == "status") { continue; } foreach ($pairs as $pair1 => $market) { if ($market['last_trade'] == 0 || $market['lowest_ask'] == 0 || $market['highest_bid'] == 0) { // ignore empty markets $ignored++; continue; } $currency1 = $this->getCurrencyCode($pair1); $currency2 = $this->getCurrencyCode($pair2); if (CurrencyOrder::hasOrder($currency1) && CurrencyOrder::hasOrder($currency2)) { if (!CurrencyOrder::isOrdered($currency1, $currency2)) { // do not duplicate ordered currencies continue; } } $rate = array("currency1" => $currency1, "currency2" => $currency2, "last_trade" => $market['last_trade'], "volume" => $market['volume'], 'bid' => $market['highest_bid'], 'ask' => $market['lowest_ask']); $result[] = $rate; } } $logger->info("Ignored " . $ignored . " markets with last trade price of 0"); return $result; }
function storeSupportedCurrencies($currencies, Logger $logger) { $logger->info("Storing " . count($currencies) . " currencies persistently"); // find all existing currencies $existing = $this->getSupportedCurrencies(true); // remove removed currencies foreach ($existing as $currency) { if (array_search($currency, $currencies) === false) { $logger->info("Removing currency {$currency}"); $q = $this->db->prepare("DELETE FROM account_currencies WHERE exchange=? AND currency=?"); $q->execute(array($this->exchange->getCode(), $currency)); } } // add new currencies foreach ($currencies as $currency) { if (array_search($currency, $existing) === false) { if (strlen($currency) != 3) { $logger->info("Ignoring currency '" . $currency . "': not three characters long"); continue; } $logger->info("Adding currency {$currency}"); $q = $this->db->prepare("INSERT INTO account_currencies SET exchange=?, currency=?"); $q->execute(array($this->exchange->getCode(), $currency)); } } // reset cache $this->cached_currencies = null; }
function fetchAllRates(Logger $logger) { $params = $this->generatePostData("getmarkets"); $url = "https://www.cryptsy.com/api"; $logger->info($url); $raw = Fetch::post($url, $params['post'], array(), $params['headers']); $json = Fetch::jsonDecode($raw); if (!$json['success']) { throw new ExchangeRateException($json['error']); } $result = array(); foreach ($json['return'] as $market) { $key = $market['label']; if ($market['last_trade'] == 0) { $logger->info("Ignoring '{$key}' market: last trade price is 0"); continue; } $currency1 = $this->getCurrencyCode($market['secondary_currency_code']); $currency2 = $this->getCurrencyCode($market['primary_currency_code']); $rate = array("currency1" => $currency1, "currency2" => $currency2, "last_trade" => $market['last_trade'], "volume" => $market['current_volume'], 'low' => $market['low_trade'], 'high' => $market['high_trade']); if ($this->shouldSwitch($currency1, $currency2)) { $rate = array('currency1' => $rate['currency2'], 'currency2' => $rate['currency1'], 'last_trade' => 1 / $rate['last_trade'], 'volume' => $rate['volume'] / $rate['last_trade'], 'low' => $rate['high'] == 0 ? 0 : 1 / $rate['high'], 'high' => $rate['low'] == 0 ? 0 : 1 / $rate['low']); } $result[] = $rate; } return $result; }
function fetchAllRates(Logger $logger) { $url = "https://www.coins-e.com/api/v2/markets/data/"; $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); $result = array(); $retired = 0; foreach ($json as $key => $market) { if ($market['status'] == 'retired') { $retired++; continue; } if ($market['marketstat']['ltp'] == 0) { $logger->info("Ignoring '{$key}' market: last trade price is 0"); continue; } $pairs = explode("_", $key, 2); $currency1 = $this->getCurrencyCode($pairs[0]); $currency2 = $this->getCurrencyCode($pairs[1]); $rate = array("currency1" => $currency2, "currency2" => $currency1, "last_trade" => $market['marketstat']['ltp'], "bid" => $market['marketstat']['bid'], "ask" => $market['marketstat']['ask'], "volume" => $market['marketstat']['24h']['volume'], "high" => $market['marketstat']['24h']['h'], "low" => $market['marketstat']['24h']['l'], "avg" => $market['marketstat']['24h']['avg_rate']); $result[] = $rate; } $logger->info("Ignored {$retired} retired markets"); return $result; }
public function notify(Service\Record $record, DOMDocument $config, Logger $logger) { $api = $config->getElementsByTagName('api')->item(0); if ($api !== null) { $logger->info('Create api'); try { $services = $api->childNodes; for ($i = 0; $i < $services->length; $i++) { $service = $services->item($i); if (!$service instanceof DOMElement) { continue; } if ($service->nodeName == 'service') { $types = $service->getElementsByTagName('type'); $uri = $service->getElementsByTagName('uri')->item(0); if ($uri instanceof DOMElement) { $endpoint = rtrim($record->path . $uri->nodeValue, '/'); $this->sql->insert($this->registry['table.xrds'], array('serviceId' => $record->id, 'priority' => 0, 'endpoint' => $endpoint)); $apiId = $this->sql->getLastInsertId(); foreach ($types as $type) { $this->sql->insert($this->registry['table.xrds_type'], array('apiId' => $apiId, 'type' => $type->nodeValue)); } $logger->info('> Register endpoint ' . $endpoint); } } } } catch (\Exception $e) { $logger->error($e->getMessage()); } } }
public function notify(Service\Record $record, DOMDocument $config, Logger $logger) { $permissions = $config->getElementsByTagName('permissions')->item(0); if ($permissions !== null) { $logger->info('Create user permissions'); try { $namespace = str_replace('\\', '_', strtolower($record->namespace)); $pos = strpos($namespace, '_'); $namespace = substr($namespace, $pos !== false ? $pos + 1 : 0); // remove vendor part $perms = $permissions->childNodes; for ($i = 0; $i < $perms->length; $i++) { $perm = $perms->item($i); if (!$perm instanceof DOMElement) { continue; } if ($perm->nodeName == 'perm') { $name = $perm->getAttribute('name'); $desc = $perm->getAttribute('description'); if (!empty($name) && !empty($desc)) { $name = $namespace . '_' . $name; $this->sql->insert($this->registry['table.user_right'], array('serviceId' => $record->id, 'name' => $name, 'description' => $desc)); $logger->info('> Created permission "' . $name . '"'); } } } } catch (\Exception $e) { $logger->error($e->getMessage()); } } }
public function testLogsToDatabase() { $this->logger->info('some information'); $log = $this->pdo->query('SELECT * FROM logs')->fetch(); $this->assertNotEmpty($log); $this->assertTrue(isset($log['message'])); $this->assertEquals('some information', $log['message']); }
/** * * @throws {@link DifficultyException} if something happened and the balance could not be obtained. */ function getDifficulty(Logger $logger) { $url = $this->difficulty_url; $logger->info($url); $value = Fetch::get($url); $logger->info("Difficulty: " . number_format($value)); return $value; }
/** * Execute command * * @param InputInterface $input Input * @param OutputInterface $output Output * * @return int * * @throws \Exception */ public function execute(InputInterface $input, OutputInterface $output) { $this->logger->info('Execute start command'); $isSuccessfull = $this->taskRunner->run($output); $exitCode = $isSuccessfull ? 0 : -1; $this->logger->info('Start command leave with exit code ' . $exitCode); return $exitCode; }
protected function processMessage(QueueMessage $message) { $msg = null; if (isset($message->getBody()->Message)) { $msg = json_decode($message->getBody()->Message); } $this->logger->debug("Processing kill message", ['message' => $msg]); if ($msg != null) { $jobId = $msg->jobId; /** @var Job $job */ $job = $this->elasticsearch->getJob($jobId); if (!is_null($job)) { if ($job->getProcess()['host'] == gethostname()) { if ($job->getStatus() == Job::STATUS_WAITING) { $job->setStatus(Job::STATUS_CANCELLED); $job->setResult(['message' => 'Job cancelled by user']); $this->getComponentJobMapper($job->getComponent())->update($job); $this->logger->info("receive-kill: Job '{$jobId}' cancelled", ['job' => $job->getData()]); $this->requeue($job->getId()); } else { if ($job->getStatus() == Job::STATUS_PROCESSING) { $job->setStatus(Job::STATUS_TERMINATING); $this->getComponentJobMapper($job->getComponent())->update($job); $pid = $job->getProcess()['pid']; // kill child processes $process = new Process(' function getcpid() { cpids=`pgrep -P $1|xargs` for cpid in $cpids; do echo "$cpid" getcpid $cpid done } getcpid ' . $pid); $process->run(); $processes = $process->getOutput(); if ($processes && count(explode("\n", $processes))) { foreach (explode("\n", $processes) as $child) { if ($child != '') { (new Process("sudo kill -KILL {$child}"))->run(); } } } // kill parent process posix_kill($pid, SIGKILL); $this->logger->info("receive-kill: Job '{$jobId}' killed", ['job' => $job->getData()]); } else { $this->logger->info("receive-kill: Job is not in waiting or processing state", ['job' => $job->getData()]); } } } } } else { $this->logger->warn("Corrupted message received", ['message' => $message]); } $this->queue->deleteMessage($message); }
public function listen() { $cmd = sprintf('php -S %s -t %s %s', escapeshellarg($this->host . ':' . $this->port), escapeshellarg($this->docRoot), escapeshellarg($this->routerPath)); if ($this->log) { $this->log->info(sprintf("Starting webserver on %s:%d ...\n", $this->host, $this->port)); } putenv('foo=bar'); exec($cmd); }
public function execute(Job $job) { // simulate long running job for ($i = 0; $i < 20; $i++) { // this will trigger pcntl_signal_dispatch() $this->logger->info("I'm running!"); sleep(3); } }
/** * * @throws {@link DifficultyException} if something happened and the balance could not be obtained. */ function getDifficulty(Logger $logger) { $url = sprintf("%sdifficulty", $this->url); $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); $value = $json['difficulty']; $logger->info("Difficulty: " . number_format($value)); return $value; }
/** * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event */ public function onKernelResponse(FilterResponseEvent $event) { if (!$this->enabled) { return; } $request = $event->getRequest(); $response = $event->getResponse(); $requestHeadersStr = ''; foreach ($request->headers->all() as $k => $v) { $requestHeadersStr .= $k . ': ' . $v[0] . PHP_EOL; } $responseHeadersStr = ''; foreach ($response->headers->all() as $k => $v) { $responseHeadersStr .= $k . ': ' . $v[0] . PHP_EOL; } $requestContent = $request->getContent(); if ($this->maxLength && strlen($requestContent) > $this->maxLength) { $requestContent = substr($requestContent, 0, $this->maxLength) . '...'; } elseif (strlen($requestContent) == 0) { $requestContent = 'Request content is empty'; } $responseContent = $response->getContent(); if ($this->maxLength && strlen($responseContent) > $this->maxLength) { $responseContent = substr($responseContent, 0, $this->maxLength) . '...'; } elseif (strlen($responseContent) == 0) { $responseContent = 'Response content is empty'; } $responseStatusCode = $response->getStatusCode(); $this->logger->info(<<<REQUEST = Request Info = Path: {$request->getPathInfo()} Method: {$request->getMethod()} Remote Host: {$request->server->get('REMOTE_ADDR')} = Request Headers = {$requestHeadersStr} = Request Content= {$requestContent} REQUEST , array('http_capture', 'request')); $this->logger->info(<<<RESPONSE = Response Info = StatusCode: {$responseStatusCode} = Response Headers = {$responseHeadersStr} = Response Content = {$responseContent} RESPONSE , array('http_capture', 'response')); }
/** * @param null $logLevel * @return null */ public function shutDown($logLevel = null) { $msg = $this->loggerPostfix . " was stopped."; if ($logLevel) { $this->logger->{$logLevel}($msg); } else { $this->logger->info($msg); } return null; }
/** * Dumps the result of an environment scan in the * form of configuration to use the resources, * the environment offers. * * @param Event $event */ public static function writeAutoConfiguration(Event $event) { $logger = new Logger(InstallHooks::class); $logger->info('Called writeAutoConfiguration()'); $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir'); if (getenv('DYNO')) { $logger->info('Detected Heroku Environment'); } file_put_contents($vendorDir . '/../web/typo3conf/AutoConfiguration.php', self::getFileTemplate()); }
public static function run($job) { $logger = new Logger('MyJob', [new ErrorLogHandler()]); $logger->info($job->data('message')); $sleep = $job->data('sleep'); if (!empty($sleep)) { $logger->info("Sleeping for " . $job->data('sleep') . " seconds"); sleep($job->data('sleep')); } }
public function buildForPreprocessor(Request $request, Route $preprocessorRoute) { // Localhost case if ($preprocessorRoute->getDefault('_http_host') === $request->getHttpHost()) { $this->logger->info("InternalForwarder built for preprocessor: Localhost forwarder.", ['host' => $request->getHttpHost()]); return $this->container->get('prestashop.public_writer.protocol.internal_forwarder.localhost'); } // Error case: localhost case was not matching, but there is no other forwarder available. $this->logger->error("InternalForwarder built for preprocessor: NO forwarder found to reach distant host.", ['host' => $request->getHttpHost()]); throw new \ErrorException("InternalForwarder building for preprocessor: NO forwarder found to reach distant host: " . $request->getHttpHost()); }
function getDifficulty(Logger $logger) { $url = $this->info_url; $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); if (!isset($json['Difficulty'])) { throw new DifficultyException("Could not find difficulty"); } $value = $json['Difficulty']; $logger->info("Difficulty: " . number_format($value)); return $value; }
public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); $defaultAccount = $this->accountProvider->getDefaultAccount(); $token = $request->attributes->get('_account'); $this->logger->info("Token", array('token' => $token)); $account = $this->accountProvider->findAccountForToken($token); if (!$account) { $account = $defaultAccount; } $request->attributes->set('_account', $account); }
function getBlockCount(Logger $logger) { $url = $this->blocks_url; $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); if (isset($json['data'][0]['nm_height'])) { $value = $json['data'][0]['nm_height']; $logger->info("Block count: " . number_format($value)); } else { throw new BlockException("Could not find block count for currency '" . $this->currency->getCode() . "'"); } return $value; }
function getBalance($address, Logger $logger) { $url = sprintf("http://api.blockscan.com/api2?module=address&action=balance&btc_address=%s&asset=%s", $address, $this->asset_name); $logger->info($url); $json = Fetch::jsonDecode(Fetch::get($url)); if ($json['status'] == "error") { throw new BalanceException($json['message']); } else { $balance = $json['data'][0]['balance']; } $logger->info("Blockchain balance for " . htmlspecialchars($address) . ": " . $balance); return $balance; }
protected function stopDaemon() { if (!file_exists(PHPCI_DIR . '/daemon/daemon.pid')) { echo "Not started\n"; $this->logger->warning("Can't stop daemon as not started"); return "notstarted"; } $cmd = "kill \$(cat %s/daemon/daemon.pid)"; $command = sprintf($cmd, PHPCI_DIR); exec($command); $this->logger->info("Daemon stopped"); unlink(PHPCI_DIR . '/daemon/daemon.pid'); }
/** * @param $method * @param $endPoint * @param array $formParams * * @throws \Exception * * @return \Psr\Http\Message\ResponseInterface */ public function request($method, $endPoint, array $formParams = []) { $options = ['headers' => ['Accept' => 'application/json'], 'form_params' => $formParams]; $requestUrl = $this->formatRequestUrl($endPoint); $this->logger->info('Remote request: ', ['method' => $method, 'url' => $requestUrl, 'options' => $options]); try { $response = $this->httpClient->{$method}($requestUrl, $options); } catch (\Exception $e) { $this->logger->error('Api request error.', ['exception' => $e->getMessage()]); throw $e; } $this->logger->info('Remote response: ', ['statusCode' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => $response->getBody()]); return $response; }