Author: Benjamin Laugueux (benjamin@yzalis.com)
 public function testWriteToFileIsSuccessfulWhenFileIsWritable()
 {
     $this->crontabFileHandler->parseFromFile($this->crontab, $this->fixturesDir . '/crontab');
     $file = tempnam(sys_get_temp_dir(), 'cron');
     $this->crontabFileHandler->writeToFile($this->crontab, $file);
     $this->assertSame($this->crontab->render() . PHP_EOL, file_get_contents($file));
     unlink($file);
 }
Beispiel #2
0
<?php

require_once __DIR__ . '/../../../../../../conf/database_conf.php';
global $Campsite;
$newscoopDir = dirname(__FILE__) . '/../../../../../../';
require_once $newscoopDir . 'vendor/yzalis/crontab/src/Crontab/Crontab.php';
require_once $newscoopDir . 'vendor/yzalis/crontab/src/Crontab/BaseJob.php';
require_once $newscoopDir . 'vendor/yzalis/crontab/src/Crontab/Job.php';
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)");
 /**
  * Write the current crons to a file.
  *
  * @param Crontab $crontab
  * @param string  $filename
  *
  * @return CrontabFileHandler
  * @throws \InvalidArgumentException
  */
 public function writeToFile(Crontab $crontab, $filename)
 {
     if (!is_writable($filename)) {
         throw new \InvalidArgumentException('File ' . $filename . ' is not writable.');
     }
     file_put_contents($filename, $crontab->render() . PHP_EOL);
     return $this;
 }
 public function testWriteToFileIsSuccessfulWhenFileIsWritable()
 {
     $this->crontabFileHandler->parseFromFile($this->crontab, $this->fixtureFile);
     $this->crontabFileHandler->writeToFile($this->crontab, $this->tempFile);
     $this->assertSame($this->crontab->render() . PHP_EOL, file_get_contents($this->tempFile));
 }
Beispiel #5
0
 public function render()
 {
     $description = implode(PHP_EOL, array("# AUTO GENERATED BY CRONMONITOR", "# PLEASE DON'T EDIT ANYTHING"));
     $result = sprintf("%s%s%s", $description, PHP_EOL, parent::render());
     return $result;
 }
 /**
  * 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;
 }
Beispiel #7
0
 /**
  * Deletes all cron jobs
  * 
  * @return Crontab
  */
 public function clear()
 {
     $this->crontab->removeAllJobs();
     return $this;
 }