/** * @param array $config * @return array */ private function getJobConfig(array $config) { $helper = new Helper(); if ($config['command'] instanceof \Closure) { $config['command'] = $helper->closureToString($config['command']); } return array_merge(array("enabled" => 1, "haltDir" => null, "runOnHost" => $helper->getHost(), "dateFormat" => "Y-m-d H:i:s", "schedule" => "* * * * *", "output" => $this->logFile, "maxRuntime" => null), $config); }
public function testProcessClosure() { $command = 'ClosureExample'; $config = array('command' => function () { echo "I'm a function!\n"; return true; }, 'schedule' => '0 * * * *', 'output' => 'logs/closure.log', 'enabled' => true); $helper = new Helper(); $config['command'] = $helper->closureToString($config['command']); $message = $this->prepareMessage(); $message->body = serialize(array('cmd' => $command, 'config' => $config)); $worker = new Job('localhost'); $worker->setJobFactory($this->prepareJobFactory($command, $config)); $worker->process($message); }
/** * 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}'."); } }
/** * @param array $config * * @return array */ private function getJobConfig(array $config) { $helper = new Helper(); if ($config['command'] instanceof \Closure) { $config['command'] = $helper->closureToString($config['command']); } 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); }
/** * @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); }
/** * @covers ::sendMail * @covers ::getCurrentMailer */ public function testSendMail() { $mailer = $this->getSwiftMailerMock(); $mailer->expects($this->once())->method('send'); $jobby = new Jobby(); $config = $jobby->getDefaultConfig(); $config['output'] = 'output message'; $config['recipients'] = 'a@a.com,b@b.com'; $helper = new Helper($mailer); $mail = $helper->sendMail('job', $config, 'message'); $host = $helper->getHost(); $email = "jobby@{$host}"; $this->assertContains('job', $mail->getSubject()); $this->assertContains("[{$host}]", $mail->getSubject()); $this->assertEquals(1, count($mail->getFrom())); $this->assertEquals('jobby', current($mail->getFrom())); $this->assertEquals($email, current(array_keys($mail->getFrom()))); $this->assertEquals($email, current(array_keys($mail->getSender()))); $this->assertContains($config['output'], $mail->getBody()); $this->assertContains('message', $mail->getBody()); }
private function getSleepTime() { return $this->helper->getPlatform() === Helper::UNIX ? 1 : 2; }
/** * Serialize command if necessary (e.g. anonymous function). * * @param $config * @return array */ protected function prepare(array $config) { if ($config['command'] instanceof \Closure) { $helper = new Helper(); $config['command'] = $helper->closureToString($config['command']); } return $config; }
/** * @covers Jobby\Helper::sendMail * @covers Jobby\Helper::getCurrentMailer */ public function testSendMail() { $mailer = $this->getSwiftMailerMock(); $mailer->expects($this->once())->method("send"); $jobby = new Jobby(); $config = $jobby->getDefaultConfig(); $config["output"] = "output message"; $config["recipients"] = "a@a.com,b@b.com"; $helper = new Helper($mailer); $mail = $helper->sendMail("job", $config, "message"); $host = $helper->getHost(); $email = "jobby@{$host}"; $this->assertContains("job", $mail->getSubject()); $this->assertContains("[{$host}]", $mail->getSubject()); $this->assertEquals(1, count($mail->getFrom())); $this->assertEquals("jobby", current($mail->getFrom())); $this->assertEquals($email, current(array_keys($mail->getFrom()))); $this->assertEquals($email, current(array_keys($mail->getSender()))); $this->assertContains($config["output"], $mail->getBody()); $this->assertContains("message", $mail->getBody()); }