2015-12-02 04:02:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Clicommands;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Cli\Command;
|
2016-06-16 14:41:02 +02:00
|
|
|
use Icinga\Module\Director\Job\JobRunner;
|
2016-06-28 14:35:03 +02:00
|
|
|
use Icinga\Module\Director\Objects\DirectorJob;
|
2015-12-02 04:02:45 +01:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
|
|
|
use Icinga\Module\Director\Objects\SyncRule;
|
2016-02-28 16:30:11 +01:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
2015-12-02 04:02:45 +01:00
|
|
|
use Icinga\Module\Director\Import\Import;
|
|
|
|
use Icinga\Module\Director\Import\Sync;
|
|
|
|
use Icinga\Application\Benchmark;
|
2016-06-16 14:41:02 +02:00
|
|
|
use Icinga\Application\Logger;
|
2015-12-02 04:02:45 +01:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class JobsCommand extends Command
|
|
|
|
{
|
|
|
|
public function runAction()
|
|
|
|
{
|
2016-06-26 13:53:58 +02:00
|
|
|
$forever = $this->params->shift('forever');
|
2016-06-28 14:35:03 +02:00
|
|
|
$jobId = $this->params->shift();
|
|
|
|
if ($jobId) {
|
|
|
|
$job = DirectorJob::load($jobId, $this->db());
|
|
|
|
$job->run();
|
|
|
|
exit(0);
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-26 13:53:58 +02:00
|
|
|
if ($forever) {
|
2016-06-16 14:41:02 +02:00
|
|
|
$this->runforever();
|
2016-06-17 16:18:02 +02:00
|
|
|
} else {
|
|
|
|
$this->runAllPendingJobs();
|
2016-06-16 14:41:02 +02:00
|
|
|
}
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
protected function runforever()
|
2015-12-02 04:02:45 +01:00
|
|
|
{
|
2016-06-23 16:11:28 +02:00
|
|
|
// We'll terminate ourselves after 24h for now:
|
|
|
|
$runUnless = time() + 86400;
|
|
|
|
|
|
|
|
// We'll exit in case more than 100MB of memory are still in use
|
|
|
|
// after the last job execution:
|
|
|
|
$maxMem = 100 * 1024 * 1024;
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
while (true) {
|
|
|
|
$this->runAllPendingJobs();
|
2016-06-23 16:11:28 +02:00
|
|
|
if (memory_get_usage() > $maxMem) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time() > $runUnless) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
sleep(2);
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
protected function runAllPendingJobs()
|
2015-12-02 04:02:45 +01:00
|
|
|
{
|
2016-06-16 14:41:02 +02:00
|
|
|
$jobs = new JobRunner($this->db());
|
2015-12-02 04:02:45 +01:00
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
try {
|
|
|
|
if ($this->hasBeenDisabled()) {
|
|
|
|
return;
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
$jobs->runPendingJobs();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Logger::error('Director Job Error: ' . $e->getMessage());
|
|
|
|
sleep(10);
|
|
|
|
}
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-06-16 14:41:02 +02:00
|
|
|
protected function hasBeenDisabled()
|
2015-12-02 04:02:45 +01:00
|
|
|
{
|
2016-06-16 14:41:02 +02:00
|
|
|
return $this->db()->getSetting('disable_all_jobs') === 'y';
|
2015-12-02 04:02:45 +01:00
|
|
|
}
|
|
|
|
}
|