/** * @param array $config * * @return array */ private function getJobConfig(array $config) { $helper = new Helper(); if (isset($config['closure'])) { $config['closure'] = $this->getSerializer()->serialize($config['closure']); } return array_merge(['enabled' => 1, 'haltDir' => null, 'runOnHost' => $helper->getHost(), 'dateFormat' => 'Y-m-d H:i:s', 'schedule' => '* * * * *', 'output' => $this->logFile, 'maxRuntime' => null, 'runAs' => null], $config); }
/** * Run all jobs. */ public function run() { $isUnix = $this->helper->getPlatform() === Helper::UNIX; if ($isUnix && !extension_loaded('posix')) { throw new Exception('posix extension is required'); } foreach ($this->jobs as $job => $config) { if ($isUnix) { $this->runUnix($job, $config); } else { $this->runWindows($job, $config); } } }
protected function runFile() { // If job should run as another user, we must be on *nix and // must have sudo privileges. $isUnix = $this->helper->getPlatform() === Helper::UNIX; $useSudo = ''; if ($isUnix) { $runAs = $this->config['runAs']; $isRoot = posix_getuid() === 0; if (!empty($runAs) && $isRoot) { $useSudo = "sudo -u {$runAs}"; } } // Start execution. Run in foreground (will block). $command = $this->config['command']; $logfile = $this->getLogfile() ?: '/dev/null'; exec("{$useSudo} {$command} 1>> \"{$logfile}\" 2>&1", $dummy, $retval); if ($retval !== 0) { throw new Exception("Job exited with status '{$retval}'."); } }
/** * @covers ::getHost */ public function testGetHostname() { $actual = $this->helper->getHost(); $this->assertContains($actual, [gethostname(), php_uname('n')]); }