Merge pull request #1612 from jygaulier/PHRAS-846_tasks-logs

Phras 846 tasks logs
This commit is contained in:
Benoît Burnichon
2016-01-06 10:14:48 +01:00
9 changed files with 149 additions and 41 deletions

View File

@@ -181,13 +181,13 @@ class TaskManagerController extends Controller
/** @var LogFileFactory $factory */ /** @var LogFileFactory $factory */
$factory = $this->app['task-manager.log-file.factory']; $factory = $this->app['task-manager.log-file.factory'];
$logFile = $factory->forManager(); $logFile = $factory->forManager();
if ($request->query->get('clr')) { if ($request->query->get('clr') && $request->query->get('version') !== null) {
$logFile->clear(); $logFile->clear($request->query->get('version'));
} }
return $this->render('admin/task-manager/log.html.twig', [ return $this->render('admin/task-manager/log_scheduler.html.twig', [
'logfile' => $logFile, 'logfile' => $logFile,
'logname' => 'Scheduler', 'version' => $request->query->get('version')
]); ]);
} }
@@ -196,13 +196,13 @@ class TaskManagerController extends Controller
/** @var LogFileFactory $factory */ /** @var LogFileFactory $factory */
$factory = $this->app['task-manager.log-file.factory']; $factory = $this->app['task-manager.log-file.factory'];
$logFile = $factory->forTask($task); $logFile = $factory->forTask($task);
if ($request->query->get('clr')) { if ($request->query->get('clr') && $request->query->get('version') !== null) {
$logFile->clear(); $logFile->clear($request->query->get('version'));
} }
return $this->render('admin/task-manager/log.html.twig', [ return $this->render('admin/task-manager/log_task.html.twig', [
'logfile' => $logFile, 'logfile' => $logFile,
'logname' => sprintf('%s (task id %d)', $task->getName(), $task->getId()), 'version' => $request->query->get('version')
]); ]);
} }

View File

@@ -34,11 +34,11 @@ abstract class AbstractLogFile implements LogFileInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContent() public function getContent($version = '')
{ {
$path = $this->getPath(); $path = $this->getPath($version);
if (is_file($path)) { if (is_file($path)) {
return file_get_contents($this->getPath()); return file_get_contents($this->getPath($version));
} }
return ''; return '';
@@ -47,9 +47,9 @@ abstract class AbstractLogFile implements LogFileInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContentStream() public function getContentStream($version = '')
{ {
$path = $this->getPath(); $path = $this->getPath($version);
return function () use ($path) { return function () use ($path) {
$handle = fopen($path, 'r'); $handle = fopen($path, 'r');
@@ -64,8 +64,17 @@ abstract class AbstractLogFile implements LogFileInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function clear() public function clear($version = '')
{ {
file_put_contents($this->getPath(), ''); file_put_contents($this->getPath($version), sprintf("[%s] File cleared\n", date(\DateTime::ATOM)));
} }
/**
* {@inheritdoc}
*/
public function versionExists($version)
{
return file_exists($this->getPath($version));
}
} }

View File

@@ -14,30 +14,53 @@ namespace Alchemy\Phrasea\TaskManager\Log;
interface LogFileInterface interface LogFileInterface
{ {
/** /**
* Returns the path to the logfile. * Returns versions (suffixes due to logrotate) of the logfiles.
* ex. foo.log --> ""
* bar-2015-12-25.log --> "2015-12-25"
* *
* @return string * @return string[]
*/ */
public function getPath(); public function getVersions();
/** /**
* Returns the content of the logfile. * Returns the path of a logfile.
* *
* @param string $version
* @return string * @return string
*/ */
public function getContent(); public function getPath($version = '');
/** /**
* Streams the content of the logfile. * Returns the content of a logfile.
*
* @param string $version
* @return string
*/
public function getContent($version = '');
/**
* Streams the content of a logfile.
* *
* This methods returns a closure that echoes the output. * This methods returns a closure that echoes the output.
* *
* @param string $version
* @return Closure * @return Closure
*/ */
public function getContentStream(); public function getContentStream($version = '');
/** /**
* Clears the content of the logfile. * Clears the content of a logfile.
*
* @param string $version
*/ */
public function clear(); public function clear($version = '');
/**
* Returns true if the logfile exists
*
* @param $version
* @return bool
*/
public function versionExists($version);
} }

View File

@@ -11,13 +11,34 @@
namespace Alchemy\Phrasea\TaskManager\Log; namespace Alchemy\Phrasea\TaskManager\Log;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class ManagerLogFile extends AbstractLogFile implements LogFileInterface class ManagerLogFile extends AbstractLogFile implements LogFileInterface
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() public function getVersions()
{ {
return sprintf('%s/scheduler.log', $this->root); $x = '/^scheduler(|(-.*))\.log$/';
$f = new Finder();
$versions = [];
/** @var \SplFileInfo $file */
foreach($f->files()->in($this->root) as $file) {
$matches = [];
if(preg_match($x, $file->getBasename(), $matches)) {
$versions[] = $matches[1];
}
}
return $versions;
}
/**
* {@inheritdoc}
*/
public function getPath($version = '')
{
return sprintf('%s/scheduler%s.log', $this->root, $version);
} }
} }

View File

@@ -12,6 +12,8 @@
namespace Alchemy\Phrasea\TaskManager\Log; namespace Alchemy\Phrasea\TaskManager\Log;
use Alchemy\Phrasea\Model\Entities\Task; use Alchemy\Phrasea\Model\Entities\Task;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class TaskLogFile extends AbstractLogFile implements LogFileInterface class TaskLogFile extends AbstractLogFile implements LogFileInterface
{ {
@@ -24,6 +26,24 @@ class TaskLogFile extends AbstractLogFile implements LogFileInterface
$this->task = $task; $this->task = $task;
} }
/**
* {@inheritdoc}
*/
public function getVersions()
{
$x = sprintf('/^task_%d(|(-.*))\.log$/', $this->task->getId());
$f = new Finder();
$versions = [];
/** @var \SplFileInfo $file */
foreach($f->files()->in($this->root) as $file) {
$matches = [];
if(preg_match($x, $file->getBasename(), $matches)) {
$versions[] = $matches[1];
}
}
return $versions;
}
/** /**
* Returns the related Task entity. * Returns the related Task entity.
* *
@@ -37,8 +57,9 @@ class TaskLogFile extends AbstractLogFile implements LogFileInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPath() public function getPath($version = '')
{ {
return sprintf('%s/task_%d.log', $this->root, $this->task->getId()); return sprintf('%s/task_%d%s.log', $this->root, $this->task->getId(), $version);
} }
} }

View File

@@ -1,12 +0,0 @@
<h4>
<a href="{{ path('admin_tasks_list') }} ">
< {{ 'Return' | trans }}
</a>
{{ logname }}
<a href="{{ path('admin_tasks_scheduler_log', { 'clr' : logfile.getPath() }) }} ">
{{ 'Clear' | trans }}
</a>
</h4>
<pre>
{{ logfile.getContent() }}
</pre>

View File

@@ -0,0 +1,22 @@
{% if version is null and logfile.versionExists('') %}
{% set version = '' %}
{% endif %}
<h4>
sheduler log
{% if version is not null %}
version {{ version ? version : "(now)" }}
{% endif %}
</h4>
{% for v in logfile.getVersions() %}
<a href="{{ path('admin_tasks_scheduler_log', {'version' : v }) }}">{{ v ? v : "(now)" }}</a>
&nbsp;
{% endfor %}
{% if version is not null %}
<pre>{{ logfile.getContent(version) }}</pre>
<a href="{{ path('admin_tasks_scheduler_log', {'version' : version, 'clr':'1' }) }} ">
{{ 'Clear' | trans }}
</a>
{% endif %}

View File

@@ -0,0 +1,22 @@
{% if version is null and logfile.versionExists('') %}
{% set version = '' %}
{% endif %}
<h4>
{{ logfile.getTask().getName() }} (task id {{ logfile.getTask.getId() }})
{% if version is not null %}
version {{ version ? version : "(now)" }}
{% endif %}
</h4>
{% for v in logfile.getVersions() %}
<a href="{{ path('admin_tasks_task_log', {'task':logfile.getTask.getId(), 'version' : v }) }}">{{ v ? v : "(now)" }}</a>
&nbsp;
{% endfor %}
{% if version is not null %}
<pre>{{ logfile.getContent(version) }}</pre>
<a href="{{ path('admin_tasks_task_log', {'task':logfile.getTask.getId(), 'version' : version, 'clr':'1' }) }} ">
{{ 'Clear' | trans }}
</a>
{% endif %}

View File

@@ -60,7 +60,9 @@ abstract class LogFileTestCase extends \PhraseanetTestCase
$log = $this->getLogFile($this->root); $log = $this->getLogFile($this->root);
file_put_contents($log->getPath(), 'hello world'); file_put_contents($log->getPath(), 'hello world');
$log->clear(); $log->clear();
$this->assertSame('', $log->getContent()); $matches = [];
$this->assertEquals(1, preg_match('/^\[(.*)\] File cleared/', $log->getContent(), $matches));
$this->assertDateAtom($matches[1]);
} }
/** /**