PHRAS-846 #time 1d

display of tasks logs in admin is ok.
if many logs (logrotate) display links
This commit is contained in:
Jean-Yves Gaulier
2015-12-28 15:14:56 +01:00
parent 8a012973bc
commit 62df4a806f
7 changed files with 75 additions and 34 deletions

View File

@@ -178,35 +178,31 @@ class TaskManagerController extends Controller
public function getSchedulerLog(Request $request)
{
file_put_contents("/tmp/phraseanet-log.txt", sprintf("%s (%d) %s\n", __FILE__, __LINE__, var_export(null, true)), FILE_APPEND);
/** @var LogFileFactory $factory */
$factory = $this->app['task-manager.log-file.factory'];
$logFile = $factory->forManager();
if ($request->query->get('clr')) {
if ($request->query->get('clr') && $request->query->get('version') !== null) {
$logFile->clear($request->query->get('version'));
}
return $this->render('admin/task-manager/log_scheduler.html.twig', [
'logfile' => $logFile,
'version' => $request->query->get('version'),
'logname' => 'Scheduler',
'version' => $request->query->get('version')
]);
}
public function getTaskLog(Request $request, Task $task)
{
file_put_contents("/tmp/phraseanet-log.txt", sprintf("%s (%d) %s\n", __FILE__, __LINE__, var_export(null, true)), FILE_APPEND);
/** @var LogFileFactory $factory */
$factory = $this->app['task-manager.log-file.factory'];
$logFile = $factory->forTask($task);
if ($request->query->get('clr')) {
if ($request->query->get('clr') && $request->query->get('version') !== null) {
$logFile->clear($request->query->get('version'));
}
return $this->render('admin/task-manager/log_task.html.twig', [
'logfile' => $logFile,
'version' => $request->query->get('version'),
'logname' => sprintf('%s (task id %d)', $task->getName(), $task->getId()),
'version' => $request->query->get('version')
]);
}

View File

@@ -66,6 +66,15 @@ abstract class AbstractLogFile implements LogFileInterface
*/
public function clear($version)
{
file_put_contents($this->getPath($version), '');
file_put_contents($this->getPath($version), sprintf("File cleared %s\n", date('r')));
}
/**
* {@inheritdoc}
*/
public function versionExists($version)
{
return file_exists($this->getPath($version));
}
}

View File

@@ -54,4 +54,13 @@ interface LogFileInterface
* @param string $version
*/
public function clear($version);
/**
* Returns true if the logfile exists
*
* @param $version
* @return bool
*/
public function versionExists($version);
}

View File

@@ -11,6 +11,9 @@
namespace Alchemy\Phrasea\TaskManager\Log;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class ManagerLogFile extends AbstractLogFile implements LogFileInterface
{
/**
@@ -18,7 +21,17 @@ class ManagerLogFile extends AbstractLogFile implements LogFileInterface
*/
public function getVersions()
{
return array('');
$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;
}
/**
@@ -26,6 +39,6 @@ class ManagerLogFile extends AbstractLogFile implements LogFileInterface
*/
public function getPath($version)
{
return sprintf('%s/scheduler%s.log', $this->root, $version ? ('-'.$version) : '' );
return sprintf('%s/scheduler%s.log', $this->root, $version);
}
}

View File

@@ -12,6 +12,8 @@
namespace Alchemy\Phrasea\TaskManager\Log;
use Alchemy\Phrasea\Model\Entities\Task;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
class TaskLogFile extends AbstractLogFile implements LogFileInterface
{
@@ -29,7 +31,17 @@ class TaskLogFile extends AbstractLogFile implements LogFileInterface
*/
public function getVersions()
{
return array('', '2015-12-21');
$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;
}
/**
@@ -47,8 +59,7 @@ class TaskLogFile extends AbstractLogFile implements LogFileInterface
*/
public function getPath($version)
{
$path = sprintf('%s/task_%d%s.log', $this->root, $this->task->getId(), $version ? ('-'.$version) : '');
file_put_contents("/tmp/phraseanet-log.txt", sprintf("%s (%d) %s\n", __FILE__, __LINE__, var_export($path, true)), FILE_APPEND);
return sprintf('%s/task_%d%s.log', $this->root, $this->task->getId(), $version ? ('-'.$version) : '');
return sprintf('%s/task_%d%s.log', $this->root, $this->task->getId(), $version);
}
}

View File

@@ -1,17 +1,22 @@
<a href="{{ path('admin_tasks_list') }} ">
< {{ 'Return' | trans }}
</a>
<h4>{{ logname }}</h4>
{% 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}}]</a>
<a href="{{ path('admin_tasks_scheduler_log', 'version' : v }) }}">{{ v ? v : "(now)" }}</a>
&nbsp;
{% endfor %}
{% if version != null %}
<a href="{{ path('admin_tasks_scheduler_log', { 'clr' : logfile.getPath(version) }) }} ">
{{ 'Clear' | trans }}
</a>
<pre>
{{ logfile.getContent(version) }}
</pre>
{% 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

@@ -1,7 +1,7 @@
{% if version is null and logfile.versionExists('') %}
{% set version = '' %}
{% endif %}
<a href="{{ path('admin_tasks_list') }} ">
< {{ 'Return' | trans }}
</a>
<h4>
{{ logfile.getTask().getName() }} (task id {{ logfile.getTask.getId() }})
{% if version is not null %}
@@ -15,10 +15,8 @@
{% endfor %}
{% if version is not null %}
<pre>
{{ logfile.getContent(version) }}
</pre>
<a href="{{ path('admin_tasks_task_log', {'task':logfile.getTask.getId(), 'clr' : logfile.getPath(version) }) }} ">
<pre>{{ logfile.getContent(version) }}</pre>
<a href="{{ path('admin_tasks_task_log', {'task':logfile.getTask.getId(), 'version' : version, 'clr':'1' }) }} ">
{{ 'Clear' | trans }}
</a>
{% endif %}