Merge pull request #4501 from nextcloud/downstream-27144

Add duration of last job execution to the table
This commit is contained in:
Morris Jobke 2017-04-26 23:16:21 -03:00 committed by GitHub
commit 4dab01d9ed
6 changed files with 59 additions and 18 deletions

View File

@ -121,11 +121,9 @@ try {
break; break;
} }
$logger->debug('Run ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
$job->execute($jobList, $logger); $job->execute($jobList, $logger);
// clean up after unclean jobs // clean up after unclean jobs
\OC_Util::tearDownFS(); \OC_Util::tearDownFS();
$logger->debug('Finished ' . get_class($job) . ' job with ID ' . $job->getId(), ['app' => 'cron']);
$jobList->setLastJob($job); $jobList->setLastJob($job);
$executedJobs[$job->getId()] = true; $executedJobs[$job->getId()] = true;

View File

@ -1012,6 +1012,14 @@
<notnull>false</notnull> <notnull>false</notnull>
</field> </field>
<field>
<!-- time for execution of the job -->
<name>execution_duration</name>
<type>integer</type>
<default></default>
<notnull>true</notnull>
</field>
<index> <index>
<name>job_class_index</name> <name>job_class_index</name>
<field> <field>

View File

@ -49,8 +49,18 @@ abstract class Job implements IJob {
*/ */
public function execute($jobList, ILogger $logger = null) { public function execute($jobList, ILogger $logger = null) {
$jobList->setLastRun($this); $jobList->setLastRun($this);
if ($logger === null) {
$logger = \OC::$server->getLogger();
}
try { try {
$jobStartTime = time();
$logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
$this->run($this->argument); $this->run($this->argument);
$timeTaken = time() - $jobStartTime;
$logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']);
$jobList->setExecutionTime($this, $timeTaken);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($logger) { if ($logger) {
$logger->logException($e, [ $logger->logException($e, [

View File

@ -275,7 +275,7 @@ class JobList implements IJobList {
* *
* @param IJob $job * @param IJob $job
*/ */
public function setLastJob($job) { public function setLastJob(IJob $job) {
$this->unlockJob($job); $this->unlockJob($job);
$this->config->setAppValue('backgroundjob', 'lastjob', $job->getId()); $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
} }
@ -285,7 +285,7 @@ class JobList implements IJobList {
* *
* @param IJob $job * @param IJob $job
*/ */
public function unlockJob($job) { public function unlockJob(IJob $job) {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->update('jobs') $query->update('jobs')
->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT)) ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
@ -310,11 +310,23 @@ class JobList implements IJobList {
* *
* @param IJob $job * @param IJob $job
*/ */
public function setLastRun($job) { public function setLastRun(IJob $job) {
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->update('jobs') $query->update('jobs')
->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT)) ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
$query->execute(); $query->execute();
} }
/**
* @param IJob $job
* @param $timeTaken
*/
public function setExecutionTime(IJob $job, $timeTaken) {
$query = $this->connection->getQueryBuilder();
$query->update('jobs')
->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
$query->execute();
}
} }

View File

@ -91,7 +91,7 @@ interface IJobList {
* @param \OCP\BackgroundJob\IJob $job * @param \OCP\BackgroundJob\IJob $job
* @since 7.0.0 * @since 7.0.0
*/ */
public function setLastJob($job); public function setLastJob(IJob $job);
/** /**
* Remove the reservation for a job * Remove the reservation for a job
@ -99,7 +99,7 @@ interface IJobList {
* @param IJob $job * @param IJob $job
* @since 9.1.0 * @since 9.1.0
*/ */
public function unlockJob($job); public function unlockJob(IJob $job);
/** /**
* get the id of the last ran job * get the id of the last ran job
@ -115,8 +115,17 @@ interface IJobList {
/** /**
* set the lastRun of $job to now * set the lastRun of $job to now
* *
* @param \OCP\BackgroundJob\IJob $job * @param IJob $job
* @since 7.0.0 * @since 7.0.0
*/ */
public function setLastRun($job); public function setLastRun(IJob $job);
/**
* set the run duration of $job
*
* @param IJob $job
* @param $timeTaken
* @since 12.0.0
*/
public function setExecutionTime(IJob $job, $timeTaken);
} }

View File

@ -7,6 +7,7 @@
*/ */
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
use OCP\BackgroundJob\IJob;
/** /**
* Class DummyJobList * Class DummyJobList
@ -15,7 +16,7 @@ namespace Test\BackgroundJob;
*/ */
class DummyJobList extends \OC\BackgroundJob\JobList { class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* @var \OC\BackgroundJob\Job[] * @var IJob[]
*/ */
private $jobs = array(); private $jobs = array();
@ -25,7 +26,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
} }
/** /**
* @param \OC\BackgroundJob\Job|string $job * @param IJob|string $job
* @param mixed $argument * @param mixed $argument
*/ */
public function add($job, $argument = null) { public function add($job, $argument = null) {
@ -40,7 +41,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
} }
/** /**
* @param \OC\BackgroundJob\Job|string $job * @param IJob|string $job
* @param mixed $argument * @param mixed $argument
*/ */
public function remove($job, $argument = null) { public function remove($job, $argument = null) {
@ -64,7 +65,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* get all jobs in the list * get all jobs in the list
* *
* @return \OC\BackgroundJob\Job[] * @return IJob[]
*/ */
public function getAll() { public function getAll() {
return $this->jobs; return $this->jobs;
@ -73,7 +74,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* get the next job in the list * get the next job in the list
* *
* @return \OC\BackgroundJob\Job * @return IJob|null
*/ */
public function getNext() { public function getNext() {
if (count($this->jobs) > 0) { if (count($this->jobs) > 0) {
@ -93,7 +94,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
* *
* @param \OC\BackgroundJob\Job $job * @param \OC\BackgroundJob\Job $job
*/ */
public function setLastJob($job) { public function setLastJob(IJob $job) {
$i = array_search($job, $this->jobs); $i = array_search($job, $this->jobs);
if ($i !== false) { if ($i !== false) {
$this->last = $i; $this->last = $i;
@ -104,7 +105,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* @param int $id * @param int $id
* @return Job * @return IJob
*/ */
public function getById($id) { public function getById($id) {
foreach ($this->jobs as $job) { foreach ($this->jobs as $job) {
@ -127,9 +128,12 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* set the lastRun of $job to now * set the lastRun of $job to now
* *
* @param \OC\BackgroundJob\Job $job * @param IJob $job
*/ */
public function setLastRun($job) { public function setLastRun(IJob $job) {
$job->setLastRun(time()); $job->setLastRun(time());
} }
public function setExecutionTime(IJob $job, $timeTaken) {
}
} }