Skip to content

Commit

Permalink
Adds some prelim logging to blog cron
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Jul 22, 2017
1 parent ec16867 commit 8447ca3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
23 changes: 23 additions & 0 deletions cron/index.php
Expand Up @@ -62,6 +62,23 @@
throw new Exception('Must specify a -s flag to determine which cron to run');
}

// set up logger
$di->set('logger', $di->lazyNew(
'Monolog\Logger',
[
'name' => 'default',
],
[
'pushHandler' => $di->lazyNew(
'Monolog\Handler\StreamHandler',
[
'stream' => __DIR__ . "/../logs/{$opts['s']}.log",
'level' => Monolog\Logger::DEBUG,
]
),
]
));

use Jacobemerick\LifestreamService\Cron;

switch ($opts['s']) {
Expand All @@ -76,4 +93,10 @@
break;
}

$cron->setLogger($di->get('logger'));
$cron->run();

$di->get('logger')->addInfo('Runtime stats', [
'time' => (microtime(true) - $startTime),
'memory' => (memory_get_usage() - $startMemory),
]);
35 changes: 31 additions & 4 deletions src/Cron/Blog.php
Expand Up @@ -5,14 +5,21 @@
use DateTime;
use DateTimeZone;
use Exception;
use SimpleXMLElement;


use GuzzleHttp\ClientInterface as Client;
use Interop\Container\ContainerInterface as Container;
use Jacobemerick\LifestreamService\Model\Blog as BlogModel;
use SimpleXMLElement;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

class Blog implements CronInterface
class Blog implements CronInterface, LoggerAwareInterface
{

use LoggerAwareTrait;

/** @var Container */
protected $container;

Expand All @@ -22,23 +29,43 @@ class Blog implements CronInterface
public function __construct(Container $container)
{
$this->container = $container;

$this->logger = new NullLogger;
}

public function run()
{
$posts = $this->fetchPosts($this->container->get('blogClient'));
try {
$posts = $this->fetchPosts($this->container->get('blogClient'));
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
return;
}

foreach ($posts as $post) {
$postExists = $this->checkPostExists($this->container->get('blogModel'), (string) $post->guid);
if ($postExists) {
continue;
}

$this->insertPost($this->container->get('blogModel'), $post, $this->container->get('timezone'));
try {
$this->insertPost(
$this->container->get('blogModel'),
$post,
$this->container->get('timezone')
);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
return;
}

$this->logger->debug("Inserted new blog post: {$post->guid}");
}
}

/**
* @param Client $client
* @return array
*/
protected function fetchPosts(Client $client)
{
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Cron/BlogTest.php
Expand Up @@ -122,6 +122,8 @@ public function testRunChecksIfEachPostExists()

public function testRunPassesOntoInsertIfPostNotExists()
{
$this->markTestIncomplete();

$posts = [
new SimpleXMLElement('<guid>http://site.com/some-post</guid>'),
];
Expand Down

0 comments on commit 8447ca3

Please sign in to comment.