Beispiel #1
0
 /**
  *  Tests the Cron run route with setting up the security key and with sending the right key
  *
  *  @covers \Liebig\Cron\CronServiceProvider
  */
 public function testRunRouteWithKeyWithCorrectSendKey()
 {
     \Event::listen('cron.collectJobs', function () {
         Cron::add('test1', "* * * * *", function () {
         });
         Cron::add('test2', "* * * * *", function () {
             return 'No';
         });
     });
     if ($this->laravelVersion >= 5) {
         \Config::set('liebigCron.cronKey', 'yT7yt3sa4tg5vtlLWbofF95v65FSWWZ8');
         \Config::set('liebigCron.logOnlyErrorJobsToDatabase', false);
     } else {
         \Config::set('cron::cronKey', 'yT7yt3sa4tg5vtlLWbofF95v65FSWWZ8');
         \Config::set('cron::logOnlyErrorJobsToDatabase', false);
     }
     $response = $this->call('GET', 'cron.php', array('key' => 'yT7yt3sa4tg5vtlLWbofF95v65FSWWZ8'));
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertEquals(1, \Liebig\Cron\Models\Manager::count());
     $this->assertEquals(2, \Liebig\Cron\Models\Job::count());
     $jobs = \Liebig\Cron\Models\Job::all();
     $this->assertEquals(2, count($jobs));
     $this->assertEquals('test1', $jobs[0]->name);
     $this->assertEquals('', $jobs[0]->return);
     $this->assertEquals('test2', $jobs[1]->name);
     $this->assertEquals('No', $jobs[1]->return);
 }
Beispiel #2
0
 /**
  * Delete old manager and job entries
  *
  * @static
  * @return bool Return true if the database is cleaned of old entries or false if the database was not cleaned successfully
  */
 private static function deleteOldDatabaseEntries()
 {
     // Get the delete after hours value
     $deleteDatabaseEntriesAfter = self::getDeleteDatabaseEntriesAfter();
     // If the value is not set or equals 0 delete old database entries is disabled
     if (!empty($deleteDatabaseEntriesAfter)) {
         // Get the current time and subtract the hour values
         $now = new \DateTime();
         date_sub($now, date_interval_create_from_date_string($deleteDatabaseEntriesAfter . ' hours'));
         // Get the old manager entries which are expired
         $oldManagers = \Liebig\Cron\Models\Manager::where('rundate', '<=', $now->format('Y-m-d H:i:s'))->get();
         foreach ($oldManagers as $manager) {
             // Get the old job entries from thee expired manager
             $oldJobs = $manager->cronJobs()->get();
             foreach ($oldJobs as $job) {
                 // Delete old job
                 $job->delete();
             }
             // After running through the manager jobs - delete the manager entry
             $manager->delete();
         }
         // Database was cleaned successfully
         return true;
     }
     // Database clean was skipped
     return false;
 }