Skip to content

Commit

Permalink
Result caching added for Jobe runs. It is turned off by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
mckeownp committed Jul 16, 2024
1 parent 63db0bb commit ad84a80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
22 changes: 10 additions & 12 deletions classes/jobesandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public function __construct() {
global $CFG;
qtype_coderunner_sandbox::__construct();
$this->jobeserver = get_config('qtype_coderunner', 'jobe_host');
if (qtype_coderunner_sandbox::is_canterbury_server($this->jobeserver)
&& qtype_coderunner_sandbox::is_using_test_sandbox()) {
if (
qtype_coderunner_sandbox::is_canterbury_server($this->jobeserver)
&& qtype_coderunner_sandbox::is_using_test_sandbox()
) {
throw new Exception("Please don't use the Canterbury jobe server for test runs");
}
$this->apikey = get_config('qtype_coderunner', 'jobe_apikey');
Expand Down Expand Up @@ -122,10 +124,10 @@ public function get_languages() {
* will be used. This is true by default (which is usually for normal
* grading runs) but is typically set to false by calls from the
* web-service/sandbox (eg, scratchpad or try-it box runs, as these are never
* going to be regraded). Note, the coderunner settings for enabling
* cache reads and writes are combined with this argument to determine
* going to be regraded). Note, the coderunner setting for enabling
* cache are combined with this argument to determine
* if cache reading and writing are done. For example, if usecache
* is true and enablegradecachereads is true then this function
* is true and enablegradecache is true then this function
* will try to read the result from the grading cache but if
* either is false then it won't.
* @return an object with at least the attribute 'error'.
Expand Down Expand Up @@ -198,7 +200,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
if (self::DEBUGGING) {
$runspec['debug'] = 1;
}
//echo "<br> $sourcecode";

if ($params !== null) {
// Process any given sandbox parameters.
$runspec['parameters'] = $params;
Expand All @@ -219,17 +221,14 @@ public function execute($sourcecode, $language, $input, $files = null, $params =

$cache = cache::make('qtype_coderunner', 'coderunner_grading_cache');
$runresult = null;
if (get_config('qtype_coderunner', 'enablegradecachereads') && $usecache) {
if (get_config('qtype_coderunner', 'enablegradecache') && $usecache) {
// NOTE: Changing jobeserver setting will effectively flush the cache
// eg, adding another jobeserver to a list of servers will mean the
// jobeserver parameter has changed and therefore the key will change.

$key = hash("md5", serialize($runspec));
// Debugger: echo '<pre>' . serialize($runspec) . '</pre>';.
$runresult = $cache->get($key); // Unserializes the returned value :) false if not found.
if ($runresult) {
// echo $key . '-----------> FOUND' . '<br>'; .
}
}

if (!$runresult) { // if cache read failed regrade, to be safe.
Expand Down Expand Up @@ -299,10 +298,9 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
$runresult['output'] = $this->filter_file_path($this->response->stdout);

// Got a useable result from Jobe server so cache it if required.
if (get_config('qtype_coderunner', 'enablegradecachewrites') && $usecache) {
if (get_config('qtype_coderunner', 'enablegradecache') && $usecache) {
$key = hash("md5", serialize($runspec));
$cache->set($key, $runresult); // Set serializes the result, get will unserialize.
// echo 'CACHE WRITE for ---> ' . $key . '<br>';
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions lang/en/qtype_coderunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,5 @@ function should be applied, e.g. <code>{{STUDENT_ANSWER | e(\'py\')}}</code> is
$string['wssubmissionrateexceeded'] = 'You have exceeded the maximum hourly \'Try it!\' submission rate. Request denied.';

$string['xmlcoderunnerformaterror'] = 'XML format error in coderunner question';
$string['enablegradecachereads'] = 'Enable reading of results from the Coderunner grading cache.';
$string['enablegradecachereads_desc'] = 'Enable writing of results to the Coderunner grading cache. The cache is a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for steps where the same quesiton and answer have already been graded.';
$string['enablegradecachewrites'] = 'Enable reading of results from grading cache.';
$string['enablegradecachewrites_desc'] = 'Enables writing of grading results to the Coderunner grading cache. If the same question, student answer pairing is found then the cached result is used. The cache is a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for steps where the same quesiton and answer have already been graded.';
$string['coderunnercachesettingsheading'] = 'Coderunner grade result cache settings';
$string['enablegradecache'] = 'Enable reading/writing of job,result pairs from/to the Coderunner grading cache.';
$string['enablegradecache_desc'] = 'The cache is a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for jobe runs where the same jobe submission has already been graded. Currently WS jobs (eg, try-it boxes and scratchpad runs) will never be cached. NOTE: If you turn off grade caching then it is usually good to empty the Coderunner grade cache before you turn it on again so you have a known state for the cache. You should also clear the cache if you change the Jobe back-end (eg, installing a new version of Python there) as results may now differ from what is in the cache.';
32 changes: 8 additions & 24 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,22 @@
''
));

$settings->add(new admin_setting_configtext(
"qtype_coderunner/ideone_password",
get_string('ideone_pass', 'qtype_coderunner'),
get_string('ideone_pass_desc', 'qtype_coderunner'),
''
));


$settings->add(new admin_setting_heading(
'codeRunnercachesettings',
get_string('coderunnercachesettingsheading', 'qtype_coderunner'),
''
));

$settings->add(new admin_setting_configcheckbox(
"qtype_coderunner/enablegradecachereads",
get_string('enablegradecachereads', 'qtype_coderunner'),
get_string('enablegradecachereads_desc', 'qtype_coderunner'),
"qtype_coderunner/enablegradecache",
get_string('enablegradecache', 'qtype_coderunner'),
get_string('enablegradecache_desc', 'qtype_coderunner'),
false
));

$settings->add(new admin_setting_configcheckbox(
"qtype_coderunner/enablegradecachewrites",
get_string('enablegradecachewrites', 'qtype_coderunner'),
get_string('enablegradecachewrites_desc', 'qtype_coderunner'),
false
$settings->add(new admin_setting_configtext(
"qtype_coderunner/ideone_password",
get_string('ideone_pass', 'qtype_coderunner'),
get_string('ideone_pass_desc', 'qtype_coderunner'),
''
));




$settings->add(new admin_setting_heading(
'codeRunnerwssettings',
get_string('coderunnerwssettings', 'qtype_coderunner'),
Expand Down Expand Up @@ -158,4 +143,3 @@
get_string('wsmaxcputime_desc', 'qtype_coderunner'),
'5'
));

0 comments on commit ad84a80

Please sign in to comment.