public function testSetterGetter() { $this->assertNull($this->crontab->getUser()); $this->assertEquals('root', $this->crontab->setUser('root')->getUser()); $this->assertCount(0, $this->crontab->getJobs()); $this->crontab->setJobs(array($this->job1, $this->job2)); $this->assertCount(2, $this->crontab->getJobs()); $this->crontab->removeAllJobs(); $this->assertCount(0, $this->crontab->getJobs()); $job = new Job(); $this->crontab->addJob($job); $this->assertCount(1, $this->crontab->getJobs()); $this->crontab->addJob($job); $this->assertCount(1, $this->crontab->getJobs()); $job = new Job(); $job->setCommand('test'); $this->crontab->addJob($job); $this->assertCount(2, $this->crontab->getJobs()); $this->crontab->removeAllJobs(); $this->crontab->setJobs(array($this->job1, $this->job2)); $this->crontab->removeJob($this->job1); $this->assertCount(1, $this->crontab->getJobs()); $job = $this->crontab->getJobs(); $this->assertEquals(array_shift($job), $this->job2); }
/** * @dataProvider crontabProvider */ public function testAdd($crontabs) { $crontab1 = $crontabs[0]; $crontab2 = $crontabs[1]; $crontab3 = $crontabs[2]; $crontab4 = $crontabs[3]; $crontab5 = $crontabs[4]; $job1 = new Job(); $job1->setMonth($crontab1['minute'])->setHour($crontab1['hour'])->setDayOfMonth($crontab1['dayOfMonth'])->setMonth($crontab1['month'])->setDayOfWeek($crontab1['dayOfWeek'])->setCommand($crontab1['command'])->setLogFile($crontab1['logFile'])->setErrorFile($crontab1['errorFile'])->setComments($crontab1['comment']); $this->entries->add($job1); $this->assertEquals(1, count($this->entries->all())); $job2 = new Job(); $job2->setMonth($crontab2['minute'])->setHour($crontab2['hour'])->setDayOfMonth($crontab2['dayOfMonth'])->setMonth($crontab2['month'])->setDayOfWeek($crontab2['dayOfWeek'])->setCommand($crontab2['command'])->setLogFile($crontab2['logFile'])->setErrorFile($crontab2['errorFile'])->setComments($crontab2['comment']); $this->entries->add($job2); $this->assertEquals(2, count($this->entries->all())); $job3 = new Job(); $job3->setMonth($crontab3['minute'])->setHour($crontab3['hour'])->setDayOfMonth($crontab3['dayOfMonth'])->setMonth($crontab3['month'])->setDayOfWeek($crontab3['dayOfWeek'])->setCommand($crontab3['command'])->setLogFile($crontab3['logFile'])->setErrorFile($crontab3['errorFile'])->setComments($crontab3['comment']); $this->entries->add($job3); $this->assertEquals(3, count($this->entries->all())); $job4 = new Job(); $job4->setMonth($crontab4['minute'])->setHour($crontab4['hour'])->setDayOfMonth($crontab4['dayOfMonth'])->setMonth($crontab4['month'])->setDayOfWeek($crontab4['dayOfWeek'])->setCommand($crontab4['command'])->setLogFile($crontab4['logFile'])->setErrorFile($crontab4['errorFile'])->setComments($crontab4['comment']); $this->entries->add($job4); $this->assertEquals(4, count($this->entries->all())); $job5 = new Job(); $job5->setMonth($crontab5['minute'])->setHour($crontab5['hour'])->setDayOfMonth($crontab5['dayOfMonth'])->setMonth($crontab5['month'])->setDayOfWeek($crontab5['dayOfWeek'])->setCommand($crontab5['command'])->setLogFile($crontab5['logFile'])->setErrorFile($crontab5['errorFile'])->setComments($crontab5['comment']); $this->entries->add($job5); $this->assertEquals(5, count($this->entries->all())); // remove all cronjobs $this->entries->clear(); }
/** * Parse crontab line into Job object * * @param $jobLine * * @return Job * @throws \InvalidArgumentException */ static function parse($jobLine) { // split the line $parts = preg_split('@ @', $jobLine, NULL, PREG_SPLIT_NO_EMPTY); // check the number of part if (count($parts) < 5) { throw new \InvalidArgumentException('Wrong job number of arguments.'); } // analyse command $command = implode(' ', array_slice($parts, 5)); // prepare variables $lastRunTime = $logFile = $logSize = $errorFile = $errorSize = $comments = null; // extract comment if (strpos($command, '#')) { list($command, $comment) = explode('#', $command); $comments = trim($comment); } // extract error file if (strpos($command, '2>>')) { list($command, $errorFile) = explode('2>>', $command); $errorFile = trim($errorFile); } // extract log file if (strpos($command, '>>')) { list($command, $logPart) = explode('>>', $command); $logPart = explode(' ', trim($logPart)); $logFile = trim($logPart[0]); } // compute last run time, and file size if (isset($logFile) && file_exists($logFile)) { $lastRunTime = filemtime($logFile); $logSize = filesize($logFile); } if (isset($errorFile) && file_exists($errorFile)) { $lastRunTime = max($lastRunTime ?: 0, filemtime($errorFile)); $errorSize = filesize($errorFile); } $command = trim($command); // compute status $status = 'error'; if ($logSize === null && $errorSize === null) { $status = 'unknown'; } else { if ($errorSize === null || $errorSize == 0) { $status = 'success'; } } // set the Job object $job = new Job(); $job->setMinute($parts[0])->setHour($parts[1])->setDayOfMonth($parts[2])->setMonth($parts[3])->setDayOfWeek($parts[4])->setCommand($command)->setErrorFile($errorFile)->setErrorSize($errorSize)->setLogFile($logFile)->setLogSize($logSize)->setComments($comments)->setLastRunTime($lastRunTime)->setStatus($status); return $job; }
/** * Returns an array of Cron Jobs based on the contents of a file. * * @param string $input * * @return Job[] */ protected function parseString($input) { $jobs = array(); $lines = array_filter(explode(PHP_EOL, $input), function ($line) { return '' != trim($line); }); foreach ($lines as $line) { $trimmed = trim($line); // if line is not a comment, convert it to a cron if (0 !== \strpos($trimmed, '#')) { $jobs[] = Job::parse($line); } } return $jobs; }
require_once $newscoopDir . 'vendor/yzalis/crontab/src/Crontab/CrontabFileHandler.php'; require_once $newscoopDir . 'vendor/symfony/symfony/src/Symfony/Component/Process/Process.php'; require_once $newscoopDir . 'vendor/symfony/symfony/src/Symfony/Component/Process/ProcessPipes.php'; use Crontab\Crontab; use Crontab\Job; $crontab = new Crontab(); $newscoopRealPath = realpath($newscoopDir); $newscoopJobs = array($newscoopRealPath . '/application/console user:garbage', $newscoopRealPath . '/bin/newscoop-autopublish', $newscoopRealPath . '/bin/newscoop-indexer', $newscoopRealPath . '/bin/subscription-notifier', $newscoopRealPath . '/bin/events-notifier', $newscoopRealPath . '/bin/newscoop-statistics', $newscoopRealPath . '/bin/newscoop-stats', $newscoopRealPath . '/scripts/newscoop.php', $newscoopRealPath . '/application/console log:maintenance'); $connection = mysqli_connect($Campsite['db']['host'], $Campsite['db']['user'], $Campsite['db']['pass'], $Campsite['db']['name']); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } foreach ($crontab->getJobs() as $key => $job) { foreach ($newscoopJobs as $key => $value) { if (strpos($job->getCommand(), $value) !== false) { $crontab->removeJob($job); } } } $job = new Job(); $job->setMinute('*')->setHour('*')->setDayOfMonth('*')->setMonth('*')->setDayOfWeek('*')->setCommand('php ' . $newscoopRealPath . '/application/console scheduler:run'); $crontab->addJob($job); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Remove obsolete pending users data','" . $newscoopRealPath . "/application/console user:garbage','30 0 * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Autopublish pending issues and articles','" . $newscoopRealPath . "/bin/newscoop-autopublish','* * * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Runs Newscoop Indexer - articles indexing','" . $newscoopRealPath . "/bin/newscoop-indexer','0 */4 * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Send Newscoop subscriptions notifications','" . $newscoopRealPath . "/bin/subscription-notifier','0 */8 * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Send Newscoop events notifications','" . $newscoopRealPath . "/bin/events-notifier','*/2 * * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Remove old statistics from Newscoop database','" . $newscoopRealPath . "/bin/newscoop-statistics','0 */4 * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`) VALUES ('Send Newscoop stats to Sourcefabric','" . $newscoopRealPath . "/bin/newscoop-stats','0 5 * * *', 1, NOW(), 0)"); mysqli_query($connection, "INSERT INTO `cron_jobs`(`name`, `command`, `schedule`, `is_active`, `created_at`, `sendMail`, `detailsUrl`) VALUES ('Display the last 7 days logged actions when going to Configure -> Logs. All the rest are stored in newscoop-audit.log.','" . $newscoopRealPath . "/application/console log:maintenance','30 1 * * *', 0, NOW(), 0, 'http://sourcefabric.booktype.pro/newscoop-42-for-journalists-and-editors/log-file-maintenance/')"); $crontab->write();
public function testParseJobLine() { $jobLine = '1 2 3 4 5 cmd >> test.log 2>&1 # some comments'; $job = Job::parse($jobLine); $this->assertEquals('1', $job->getMinute()); $this->assertEquals('2', $job->getHour()); $this->assertEquals('3', $job->getDayOfMonth()); $this->assertEquals('4', $job->getMonth()); $this->assertEquals('5', $job->getDayOfWeek()); $this->assertEquals('cmd', $job->getCommand()); $this->assertEquals('test.log', $job->getLogFile()); $this->assertEquals('some comments', $job->getComments()); }
/** * Remove a specified job in the current crontab * * @param Job $job * * @return Crontab */ public function removeJob(Job $job) { unset($this->jobs[$job->getHash()]); return $this; }
/** * Returns an array of Cron Jobs based on the contents of a file. * * @param string $input * * @return array of Variable and Job instances */ protected function parseString($input) { $elements = array(); $lines = array_filter(explode(PHP_EOL, $input), function ($line) { return '' != trim($line); }); foreach ($lines as $line) { $trimmed = trim($line); // if line is not a comment, convert it to a cron if (0 !== \strpos($trimmed, '#')) { if (preg_match('/^[^\\s]+\\s?=/', $line)) { $elements[] = Variable::parse($line); } else { $elements[] = Job::parse($line); } } } return $elements; }
/** * Save newscoop cronjobs in user cronjob file * * @param SchedulerService $scheduler Cron job scheduler service * @return bolean */ public function saveCronjobs(SchedulerServiceInterface $scheduler) { $binDirectory = realpath($this->newscoopDir . '/bin'); $appDirectory = realpath($this->newscoopDir . '/application/console'); $scheduler->registerJob("Autopublish pending issues and articles", array('command' => $binDirectory . '/newscoop-autopublish', 'schedule' => '* * * * *')); $scheduler->registerJob("Runs Newscoop Indexer - articles indexing", array('command' => $binDirectory . '/newscoop-indexer --silent', 'schedule' => '0 */4 * * *')); $scheduler->registerJob("Send Newscoop subscriptions notifications", array('command' => $binDirectory . '/subscription-notifier', 'schedule' => '0 */8 * * *')); $scheduler->registerJob("Send Newscoop events notifications", array('command' => $binDirectory . '/events-notifier', 'schedule' => '*/2 * * * *')); $scheduler->registerJob("Remove old statistics from Newscoop database", array('command' => $binDirectory . '/newscoop-statistics', 'schedule' => '0 */4 * * *')); $scheduler->registerJob("Send Newscoop stats to Sourcefabric", array('command' => $binDirectory . '/newscoop-stats', 'schedule' => '0 5 * * *')); $scheduler->registerJob("Remove obsolete pending users data", array('command' => $appDirectory . ' user:garbage', 'schedule' => '30 0 * * *')); $scheduler->registerJob("Display the last 7 days logged actions when going to Configure -> Logs. All the rest are stored in newscoop-audit.log.", array('command' => $appDirectory . ' log:maintenance', 'schedule' => '30 1 * * *', 'enabled' => false, 'detailsUrl' => 'http://sourcefabric.booktype.pro/newscoop-42-for-journalists-and-editors/log-file-maintenance/')); $crontab = new Crontab(); $job = new Job(); $job->setMinute('*')->setHour('*')->setDayOfMonth('*')->setMonth('*')->setDayOfWeek('*')->setCommand('php ' . $appDirectory . ' scheduler:run'); $crontab->addJob($job); $crontab->write(); return true; }