cc96965b3b
When you kill a process which is executing a task, it now marks it as failed.
220 lines
8.5 KiB
PHP
220 lines
8.5 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
namespace core\task;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
require_once(__DIR__ . '/../fixtures/task_fixtures.php');
|
|
|
|
/**
|
|
* This file contains unit tests for the 'task running' data.
|
|
*
|
|
* @package core
|
|
* @category test
|
|
* @copyright 2019 The Open University
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class running_test extends \advanced_testcase {
|
|
|
|
/**
|
|
* Test for ad-hoc tasks.
|
|
*/
|
|
public function test_adhoc_task_running() {
|
|
$this->resetAfterTest();
|
|
|
|
// Specify lock factory. The reason is that Postgres locks don't work within a single
|
|
// process (i.e. if you try to get a lock that you already locked, it will just let you)
|
|
// which is usually OK but not here where we are simulating running two tasks at once in
|
|
// the same process.
|
|
set_config('lock_factory', '\core\lock\db_record_lock_factory');
|
|
|
|
// Create and queue 2 new ad-hoc tasks.
|
|
$task1 = new adhoc_test_task();
|
|
$task1->set_next_run_time(time() - 20);
|
|
manager::queue_adhoc_task($task1);
|
|
$task2 = new adhoc_test2_task();
|
|
$task2->set_next_run_time(time() - 10);
|
|
manager::queue_adhoc_task($task2);
|
|
|
|
// Check no tasks are marked running.
|
|
$running = manager::get_running_tasks();
|
|
$this->assertEmpty($running);
|
|
|
|
// Mark the first task running and check results.
|
|
$before = time();
|
|
$next1 = manager::get_next_adhoc_task(time());
|
|
manager::adhoc_task_starting($next1);
|
|
$after = time();
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(1, $running);
|
|
foreach ($running as $item) {
|
|
$this->assertEquals('adhoc', $item->type);
|
|
$this->assertLessThanOrEqual($after, $item->timestarted);
|
|
$this->assertGreaterThanOrEqual($before, $item->timestarted);
|
|
}
|
|
|
|
// Mark the second task running and check results.
|
|
$next2 = manager::get_next_adhoc_task(time());
|
|
manager::adhoc_task_starting($next2);
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(2, $running);
|
|
|
|
// Second task completes successfully.
|
|
manager::adhoc_task_complete($next2);
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(1, $running);
|
|
|
|
// First task fails.
|
|
manager::adhoc_task_failed($next1);
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(0, $running);
|
|
}
|
|
|
|
/**
|
|
* Test for scheduled tasks.
|
|
*/
|
|
public function test_scheduled_task_running() {
|
|
global $DB;
|
|
$this->resetAfterTest();
|
|
|
|
// Check no tasks are marked running.
|
|
$running = manager::get_running_tasks();
|
|
$this->assertEmpty($running);
|
|
|
|
// Disable all the tasks, except two, and set those two due to run.
|
|
$DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ? AND classname != ?',
|
|
['\core\task\session_cleanup_task', '\core\task\file_trash_cleanup_task']);
|
|
$DB->set_field('task_scheduled', 'nextruntime', 1,
|
|
['classname' => '\core\task\session_cleanup_task']);
|
|
$DB->set_field('task_scheduled', 'nextruntime', 1,
|
|
['classname' => '\core\task\file_trash_cleanup_task']);
|
|
$DB->set_field('task_scheduled', 'lastruntime', time() - 1000,
|
|
['classname' => '\core\task\session_cleanup_task']);
|
|
$DB->set_field('task_scheduled', 'lastruntime', time() - 500,
|
|
['classname' => '\core\task\file_trash_cleanup_task']);
|
|
|
|
// Get the first task and start it off.
|
|
$next1 = manager::get_next_scheduled_task(time());
|
|
$before = time();
|
|
manager::scheduled_task_starting($next1);
|
|
$after = time();
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(1, $running);
|
|
foreach ($running as $item) {
|
|
$this->assertLessThanOrEqual($after, $item->timestarted);
|
|
$this->assertGreaterThanOrEqual($before, $item->timestarted);
|
|
$this->assertEquals('\core\task\session_cleanup_task', $item->classname);
|
|
}
|
|
|
|
// Mark the second task running and check results. We have to change the times so the other
|
|
// one comes up first, otherwise it repeats the same one.
|
|
$DB->set_field('task_scheduled', 'lastruntime', time() - 1500,
|
|
['classname' => '\core\task\file_trash_cleanup_task']);
|
|
|
|
// Make sure that there is a time gap between task to sort them as expected.
|
|
sleep(1);
|
|
$next2 = manager::get_next_scheduled_task(time());
|
|
manager::scheduled_task_starting($next2);
|
|
|
|
// Check default sorting by timestarted.
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(2, $running);
|
|
$item = array_shift($running);
|
|
$this->assertEquals('\core\task\session_cleanup_task', $item->classname);
|
|
$item = array_shift($running);
|
|
$this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
|
|
|
|
// Check sorting by time ASC.
|
|
$running = manager::get_running_tasks('time ASC');
|
|
$this->assertCount(2, $running);
|
|
$item = array_shift($running);
|
|
$this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname);
|
|
$item = array_shift($running);
|
|
$this->assertEquals('\core\task\session_cleanup_task', $item->classname);
|
|
|
|
// Complete the file trash one.
|
|
manager::scheduled_task_complete($next2);
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(1, $running);
|
|
|
|
// Other task fails.
|
|
manager::scheduled_task_failed($next1);
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(0, $running);
|
|
}
|
|
|
|
/**
|
|
* Test that adhoc tasks are set as failed when shutdown is called during execution.
|
|
* @covers \core\task\manager::fail_running_task
|
|
*/
|
|
public function test_adhoc_task_running_will_fail_when_shutdown(): void {
|
|
$this->resetAfterTest();
|
|
$this->preventResetByRollback();
|
|
|
|
$task1 = new adhoc_test_task();
|
|
$task1->set_next_run_time(time() - 20);
|
|
manager::queue_adhoc_task($task1);
|
|
|
|
$next1 = manager::get_next_adhoc_task(time());
|
|
\core\task\manager::adhoc_task_starting($next1);
|
|
|
|
self::assertEmpty(manager::get_failed_adhoc_tasks());
|
|
|
|
// Trigger shutdown handler.
|
|
\core_shutdown_manager::shutdown_handler();
|
|
|
|
$failedtasks = manager::get_failed_adhoc_tasks();
|
|
|
|
self::assertCount(1, $failedtasks);
|
|
self::assertEquals($next1->get_id(), $failedtasks[0]->get_id());
|
|
}
|
|
|
|
/**
|
|
* Test that scheduled tasks are set as failed when shutdown is called during execution.
|
|
* @covers \core\task\manager::fail_running_task
|
|
*/
|
|
public function test_scheduled_task_running_will_fail_when_shutdown(): void {
|
|
global $DB;
|
|
|
|
$this->resetAfterTest();
|
|
$this->preventResetByRollback();
|
|
|
|
// Disable all the tasks, so we can insert our own and be sure it's the only one being run.
|
|
$DB->set_field('task_scheduled', 'disabled', 1);
|
|
|
|
$task1 = new scheduled_test_task();
|
|
$task1->set_minute('*');
|
|
$task1->set_next_run_time(time() - HOURSECS);
|
|
$DB->insert_record('task_scheduled', manager::record_from_scheduled_task($task1));
|
|
|
|
$next1 = \core\task\manager::get_next_scheduled_task(time());
|
|
\core\task\manager::scheduled_task_starting($next1);
|
|
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(1, $running);
|
|
|
|
// Trigger shutdown handler.
|
|
\core_shutdown_manager::shutdown_handler();
|
|
|
|
$running = manager::get_running_tasks();
|
|
$this->assertCount(0, $running);
|
|
|
|
$scheduledtask1 = manager::get_scheduled_task(scheduled_test_task::class);
|
|
self::assertGreaterThan($next1->get_fail_delay(), $scheduledtask1->get_fail_delay());
|
|
}
|
|
}
|