/** * @covers ::checkMaxRuntime */ public function testCheckMaxRuntimeShouldFailIsExceeded() { if ($this->helper->getPlatform() !== Helper::UNIX) { $this->markTestSkipped("'maxRuntime' is not supported on Windows"); } $helper = $this->getMock('Jobby\\Helper', ['getLockLifetime']); $helper->expects($this->once())->method('getLockLifetime')->will($this->returnValue(2)); $this->runJob(['command' => 'true', 'maxRuntime' => 1], $helper); $this->assertContains('MaxRuntime of 1 secs exceeded! Current runtime: 2 secs', $this->getLogContent()); }
/** * @covers ::getLockLifetime */ public function testGetLocklifetime() { if ($this->helper->getPlatform() === Helper::WINDOWS) { $this->markTestSkipped("Test relies on posix_ functions"); } $this->helper->acquireLock($this->lockFile); $this->assertEquals(0, $this->helper->getLockLifetime($this->lockFile)); sleep(1); $this->assertEquals(1, $this->helper->getLockLifetime($this->lockFile)); sleep(1); $this->assertEquals(2, $this->helper->getLockLifetime($this->lockFile)); $this->helper->releaseLock($this->lockFile); }
/** * 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); } } }
/** * Run all jobs. */ public function run() { $isUnix = $this->helper->getPlatform() === Helper::UNIX; if ($isUnix && !extension_loaded('posix')) { throw new Exception('posix extension is required'); } $scheduleChecker = new ScheduleChecker(); foreach ($this->jobs as $job => $config) { if (!$scheduleChecker->isDue($config['schedule'])) { continue; } 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) { $hasRunAs = !empty($this->config["runAs"]); $isRoot = posix_getuid() === 0; if ($hasRunAs && $isRoot) { $useSudo = "sudo -u {$this->config['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 ::getPlatform */ public function testGetPlatform() { $actual = $this->helper->getPlatform(); $this->assertContains($actual, [Helper::UNIX, Helper::WINDOWS]); }
private function getSleepTime() { return $this->helper->getPlatform() === Helper::UNIX ? 1 : 2; }