ENHANCEMENT: Added retention value to jobs

This commit is contained in:
Jeroen De Meerleer 2021-07-20 16:29:03 +02:00
parent 7e2fdbfd18
commit f8e0c59731
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 55 additions and 15 deletions

View File

@ -12,12 +12,13 @@ use phpseclib3\Net\SSH2;
class Job extends Repository
{
public function getAllJobs()
public function getAllJobs(bool $idiskey = false)
{
$jobsSql = "SELECT * FROM job";
$jobsStmt = $this->dbcon->prepare($jobsSql);
$jobsRslt = $jobsStmt->executeQuery();
$jobs = $jobsRslt->fetchAllAssociative();
$returnbyid = [];
foreach ($jobs as $key=>&$job) {
$job['data'] = json_decode($job['data'], true);
$job['host-displayname'] = $job['data']['host'];
@ -28,7 +29,10 @@ class Job extends Repository
if(!empty($job['data']['containertype']) && $job['data']['containertype'] != 'none') {
$job['host-displayname'] = $job['data']['service'] . ' on ' . $job['data']['host'];
}
if($idiskey) $returnbyid[$job['id']] = $job;
}
if($idiskey) return $returnbyid;
array_multisort(
array_column($jobs, 'name'), SORT_ASC,
array_column($jobs, 'host'), SORT_ASC,
@ -379,6 +383,8 @@ class Job extends Repository
}
$values['nextrun'] = DateTime::createFromFormat('d/m/Y H:i:s', $values['nextrun'])->getTimestamp();
$values['data']['retention'] = $values['retention'];
$values['data']['crontype'] = $values['crontype'];
$values['data']['hosttype'] = $values['hosttype'];
$values['data']['containertype'] = $values['containertype'];
@ -540,10 +546,8 @@ class Job extends Repository
public function deleteJob(int $id)
{
$addJobSql = "DELETE FROM job WHERE id = :id";
$addJobStmt = $this->dbcon->prepare($addJobSql);
$addJobStmt->executeQuery([':id' => $id]);
$this->dbcon->prepare("DELETE FROM job WHERE id = :id")->executeStatement([':id' => $id]);
$this->dbcon->prepare("DELETE FROM run WHERE job_id = :id")->executeStatement([':id' => $id]);
return ['success' => true, 'message' => 'Cronjob succesfully deleted'];
}

View File

@ -49,20 +49,40 @@ class Run extends Repository
return $slowJob['average'] > $timelimit;
}
public function cleanupRuns(array $jobids, int $maxage): int
public function cleanupRuns(array $jobids, int $maxage = NULL): int
{
$sql = 'DELETE FROM run WHERE timestamp < :timestamp';
$params[':timestamp'] = time() - ($maxage * 24 * 60 * 60);
if(!empty($jobids)) {
$jobidsql = [];
foreach($jobids as $key=>$jobid){
$jobidsql[] = ':job' . $key;
$params[':job' . $key] = $jobid;
$jobRepo = new Job($this->dbcon);
$allJobs = $jobRepo->getAllJobs(true);
if(empty($jobids)) {
foreach($allJobs as $key=>$job) {
$jobids[] = $key;
}
$sql .= ' AND job_id in (' . implode(',', $jobidsql) . ')';
}
$sqldelete = [];
if($maxage == NULL) {
foreach ($allJobs as $key=>$job) {
if(isset($job['data']['retention']) && in_array($key, $jobids)) {
$sqldelete[] = '( job_id = :job' . $key . ' AND timestamp < :timestamp' . $key . ')';
$params[':job' . $key] = $key;
$params[':timestamp' . $key] = time() - ($job['data']['retention'] * 24 * 60 * 60);
}
}
} else {
$sqljobids = '';
if(!empty($jobids)) {
$jobidsql = [];
foreach($jobids as $key=>$jobid){
$jobidsql[] = ':job' . $key;
$params[':job' . $key] = $jobid;
}
$sqljobids = ' AND job_id in (' . implode(',', $jobidsql) . ')';
}
$params[':timestamp'] = time() - ($maxage * 24 * 60 * 60);
$sqldelete[] = 'timestamp < :timestamp' . $sqljobids;
}
$sql = 'DELETE FROM run WHERE ' . implode(' OR ', $sqldelete);
try {
return $this->dbcon->prepare($sql)->executeQuery($params)->rowCount();
return $this->dbcon->prepare($sql)->executeStatement($params);
} catch(Exception $exception) {
throw $exception;
}

View File

@ -5,10 +5,12 @@
<form method="post" class="form-horizontal" enctype="multipart/form-data" action="{{ path('job_add') }}">
<h3>General info</h3>
<div class="mb-3">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="System update">
</div>
<div class="mb-3">
<label for="name">Interval (in seconds)</label>
<div class="input-group">
@ -27,10 +29,12 @@
<input type="number" class="form-control" id="interval" name="interval">
</div>
</div>
<div class="mb-3">
<label for="nextrun">Next run</label>
<input type="text" autocomplete="off" id="nextrunselector" class="form-control datetimepicker-input" data-target="#nextrunselector" data-toggle="datetimepicker" name="nextrun">
</div>
<div class="mb-3">
<label for="lastrun">Last run</label>
<div class="input-group">
@ -44,6 +48,12 @@
</div>
</div>
<div class="mb-3">
<label for="retention">Retention (in days)</label>
<input type="number" name="retention" class="form-control" id="retention" placeholder="7">
<small id="retention-help" class="form-text text-muted">How many days (at least) to keep runs of this job in the database</small>
</div>
<h3>Job details</h3>
<div class="mb-3 btn-group croncategory-selector">
<div class="dropdown croncategory-group crontype-group">

View File

@ -44,6 +44,12 @@
</div>
</div>
<div class="mb-3">
<label for="retention">Retention (in days)</label>
<input type="number" name="retention" class="form-control" id="retention" placeholder="7" value="{% if attribute(data, 'retention') is not empty %}{{ attribute(data, 'retention') }}{% endif %}">
<small id="retention-help" class="form-text text-muted">How many days (at least) to keep runs of this job in the database</small>
</div>
<h3>Job details</h3>
<div class="mb-3 btn-group croncategory-selector">
<div class="dropdown croncategory-group crontype-group{% if data.crontype != 'http' %} btn-group{% endif %}">