/** * Jobby scheduler entry point */ public function actionRun() { $jobby = new Jobby(); $tasks = $this->getTasks(); /** @var JobbyModelInterface[] $tasks */ foreach ($tasks as $task) { $output = $task->getJobbyOutput(); $jobby->add($task->getPrimaryKey(), ['command' => $task->getJobbyCommand(), 'schedule' => $task->getJobbySchedule(), 'output' => $output ? $output : null, 'enabled' => $task->getJobbyEnabled(), 'debug' => false]); } $jobby->run(); echo count($tasks) . ' jobby tasks found' . PHP_EOL; }
/** * Register all of our tasks with our job runner. * * @param \Jobby\Jobby $runner The Jobby instance to bind these tasks to. * @param array $task_list A list of tasks to register. */ private function register_tasks(\Jobby\Jobby $runner, $task_list) { foreach ($task_list as $task) { $task_class = $this->crony_job_namespace . '\\' . $task; // Per the interface... $task_config = call_user_func($task_class . '::config'); // If there's no command registered in the configuration, we'll bind an anonymous function to // run our specified task. if (!isset($task_config['command'])) { $task_config['command'] = function () use($task_class) { return call_user_func($task_class . '::run'); }; } $runner->add($task, $task_config); } }
use Jobby\Jobby; use Psr\Log\LoggerInterface as Logger; use DI\ContainerBuilder; use Standard\Controllers\Cron\BetterBackgroundJob; require __DIR__ . '/../vendor/autoload.php'; $containerBuilder = new ContainerBuilder(); $container = $containerBuilder->addDefinitions(require_once __DIR__ . '/../app/config/config_cli.php')->useAnnotations(true)->build(); try { $crons = TableRegistry::get('Cron')->find()->hydrate(false)->toArray(); $settings = TableRegistry::get('CronSettings')->find()->hydrate(false)->toArray()[0]; } catch (\Exception $e) { $container->get(Logger::class)->error('Error while fetching DB contents: ' . $e->getMessage()); return; } $container->set('jobby', function () use($container) { $j = new Jobby($container->get('cron-config')['emails'] ? ['mailer' => 'smtp', 'smtpUsername' => getenv('MAILGUN_SMTP_LOGIN'), 'smtpPassword' => getenv('MAILGUN_SMTP_PASS'), 'smtpHost' => getenv('MAILGUN_SMTP_HOST')] : []); $j->setConfig(['jobClass' => BetterBackgroundJob::class]); return $j; }); /** @var array $cron */ foreach ($crons as $cron) { $cron = array_merge($settings, array_filter($cron)); $jobName = $cron['name']; unset($cron['name']); $cron['output'] = $cron['output'] ?? false ? $container->get('site-config')['logFolder'] . '/' . $cron['output'] : $container->get('site-config')['logFolder'] . '/cron.log'; try { if (strpos($cron['command'], '::')) { // Assuming it's a Class::method syntax list($class, $method) = explode('::', $cron['command']); $cron['command'] = function () use($class, $method) { $containerBuilder = new ContainerBuilder();
/** * @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()); }
public function testShouldFailIfMaxRuntimeExceeded() { $jobby = new Jobby(); $jobby->add('slow job', ['command' => 'sleep 4', 'schedule' => '* * * * *', 'maxRuntime' => 1, 'output' => $this->logFile]); $jobby->run(); sleep(2); $jobby->run(); sleep(1); $this->assertContains('ERROR: MaxRuntime of 1 secs exceeded!', $this->getLogContent()); }
public function testShouldFailIfMaxRuntimeExceeded() { if ($this->helper->getPlatform() === Helper::WINDOWS) { $this->markTestSkipped("'maxRuntime' is not supported on Windows"); } $jobby = new Jobby(); $jobby->add('slow job', ['command' => 'sleep 4', 'schedule' => '* * * * *', 'maxRuntime' => 1, 'output' => $this->logFile]); $jobby->run(); sleep(2); $jobby->run(); sleep(2); $this->assertContains('ERROR: MaxRuntime of 1 secs exceeded!', $this->getLogContent()); }
/** * @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()); }