Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core\task;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once (__DIR__ . '/../fixtures/task_fixtures.php');
  22  
  23  /**
  24   * This file contains unit tests for the 'task running' data.
  25   *
  26   * @package   core
  27   * @category  test
  28   * @copyright 2019 The Open University
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class running_test extends \advanced_testcase {
  32  
  33      /**
  34       * Test for ad-hoc tasks.
  35       */
  36      public function test_adhoc_task_running() {
  37          $this->resetAfterTest();
  38  
  39          // Specify lock factory. The reason is that Postgres locks don't work within a single
  40          // process (i.e. if you try to get a lock that you already locked, it will just let you)
  41          // which is usually OK but not here where we are simulating running two tasks at once in
  42          // the same process.
  43          set_config('lock_factory', '\core\lock\db_record_lock_factory');
  44  
  45          // Create and queue 2 new ad-hoc tasks.
  46          $task1 = new adhoc_test_task();
  47          $task1->set_next_run_time(time() - 20);
  48          manager::queue_adhoc_task($task1);
  49          $task2 = new adhoc_test2_task();
  50          $task2->set_next_run_time(time() - 10);
  51          manager::queue_adhoc_task($task2);
  52  
  53          // Check no tasks are marked running.
  54          $running = manager::get_running_tasks();
  55          $this->assertEmpty($running);
  56  
  57          // Mark the first task running and check results.
  58          $before = time();
  59          $next1 = manager::get_next_adhoc_task(time());
  60          manager::adhoc_task_starting($next1);
  61          $after = time();
  62          $running = manager::get_running_tasks();
  63          $this->assertCount(1, $running);
  64          foreach ($running as $item) {
  65              $this->assertEquals('adhoc', $item->type);
  66              $this->assertLessThanOrEqual($after, $item->timestarted);
  67              $this->assertGreaterThanOrEqual($before, $item->timestarted);
  68          }
  69  
  70          // Mark the second task running and check results.
  71          $next2 = manager::get_next_adhoc_task(time());
  72          manager::adhoc_task_starting($next2);
  73          $running = manager::get_running_tasks();
  74          $this->assertCount(2, $running);
  75  
  76          // Second task completes successfully.
  77          manager::adhoc_task_complete($next2);
  78          $running = manager::get_running_tasks();
  79          $this->assertCount(1, $running);
  80  
  81          // First task fails.
  82          manager::adhoc_task_failed($next1);
  83          $running = manager::get_running_tasks();
  84          $this->assertCount(0, $running);
  85      }
  86  
  87      /**
  88       * Test for scheduled tasks.
  89       */
  90      public function test_scheduled_task_running() {
  91          global $DB;
  92          $this->resetAfterTest();
  93  
  94          // Check no tasks are marked running.
  95          $running = manager::get_running_tasks();
  96          $this->assertEmpty($running);
  97  
  98          // Disable all the tasks, except two, and set those two due to run.
  99          $DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ? AND classname != ?',
 100                  ['\core\task\session_cleanup_task', '\core\task\file_trash_cleanup_task']);
 101          $DB->set_field('task_scheduled', 'nextruntime', 1,
 102                  ['classname' => '\core\task\session_cleanup_task']);
 103          $DB->set_field('task_scheduled', 'nextruntime', 1,
 104                  ['classname' => '\core\task\file_trash_cleanup_task']);
 105          $DB->set_field('task_scheduled', 'lastruntime', time() - 1000,
 106                  ['classname' => '\core\task\session_cleanup_task']);
 107          $DB->set_field('task_scheduled', 'lastruntime', time() - 500,
 108                  ['classname' => '\core\task\file_trash_cleanup_task']);
 109  
 110          // Get the first task and start it off.
 111          $next1 = manager::get_next_scheduled_task(time());
 112          $before = time();
 113          manager::scheduled_task_starting($next1);
 114          $after = time();
 115          $running = manager::get_running_tasks();
 116          $this->assertCount(1, $running);
 117          foreach ($running as $item) {
 118              $this->assertLessThanOrEqual($after, $item->timestarted);
 119              $this->assertGreaterThanOrEqual($before, $item->timestarted);
 120              $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
 121          }
 122  
 123          // Mark the second task running and check results. We have to change the times so the other
 124          // one comes up first, otherwise it repeats the same one.
 125          $DB->set_field('task_scheduled', 'lastruntime', time() - 1500,
 126                  ['classname' => '\core\task\file_trash_cleanup_task']);
 127  
 128          // Make sure that there is a time gap between task to sort them as expected.
 129          sleep(1);
 130          $next2 = manager::get_next_scheduled_task(time());
 131          manager::scheduled_task_starting($next2);
 132  
 133          // Check default sorting by timestarted.
 134          $running = manager::get_running_tasks();
 135          $this->assertCount(2, $running);
 136          $item = array_shift($running);
 137          $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
 138          $item = array_shift($running);
 139          $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
 140  
 141          // Check sorting by time ASC.
 142          $running = manager::get_running_tasks('time ASC');
 143          $this->assertCount(2, $running);
 144          $item = array_shift($running);
 145          $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
 146          $item = array_shift($running);
 147          $this->assertEquals('\core\task\session_cleanup_task', $item->classname);
 148  
 149          // Complete the file trash one.
 150          manager::scheduled_task_complete($next2);
 151          $running = manager::get_running_tasks();
 152          $this->assertCount(1, $running);
 153  
 154          // Other task fails.
 155          manager::scheduled_task_failed($next1);
 156          $running = manager::get_running_tasks();
 157          $this->assertCount(0, $running);
 158      }
 159  }