Skip to content

Commit

Permalink
Code tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
trampgeek committed Mar 21, 2024
1 parent 9406608 commit eb9aca5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions classes/jobesandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
$this->apikey = $params['jobeapikey'];
}
}
// QUESTION: Do we need this when using cached result?
$this->currentjobid = sprintf('%08x', mt_rand());

// Create a single curl object here, with support for cookies, and use it for all requests.
// This supports Jobe back-ends that use cookies for sticky load-balancing.
// Make a place to store the cookies.
make_temp_directory('qtype_coderunner');
$cookiefile = $CFG->tempdir . '/qtype_coderunner/session_cookies_' . $this->currentjobid . '.txt';

$curl = new curl();
$curl->setopt([
'CURLOPT_COOKIEJAR' => $cookiefile,
'CURLOPT_COOKIEFILE' => $cookiefile,
]);

$cache = cache::make('qtype_coderunner', 'coderunner_grading_cache');
$runresult = null;
Expand All @@ -239,7 +226,20 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
}
}

if (!$runresult) { // if cache read failed regrade to be safe
if (!$runresult) { // if cache read failed regrade, to be safe.
$this->currentjobid = sprintf('%08x', mt_rand());

// Create a single curl object here, with support for cookies, and use it for all requests.
// This supports Jobe back-ends that use cookies for sticky load-balancing.
// Make a place to store the cookies.
make_temp_directory('qtype_coderunner');
$cookiefile = $CFG->tempdir . '/qtype_coderunner/session_cookies_' . $this->currentjobid . '.txt';

$curl = new curl();
$curl->setopt([
'CURLOPT_COOKIEJAR' => $cookiefile,
'CURLOPT_COOKIEFILE' => $cookiefile,
]);
$postbody = ['run_spec' => $runspec];
// Try submitting the job. If we get a 404, try again after
// putting all the files on the server. Anything else is an error.
Expand All @@ -256,8 +256,8 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
}
}

// Delete the cookie file.
unlink($cookiefile);
// Delete the cookie file.
unlink($cookiefile);

$runresult = [];
$runresult['sandboxinfo'] = [
Expand All @@ -269,8 +269,8 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
if (
!$okresponse // If it's not an OK response...
|| !is_object($this->response) // ... or there's any sort of broken ...
|| !isset($this->response->outcome)
) { // ... communication with server.
|| !isset($this->response->outcome) // ... communication with server.
) {
// Return with errorcode set and as much extra info as possible in stderr.
$errorcode = $okresponse ? self::UNKNOWN_SERVER_ERROR : $this->get_error_code($httpcode);
$this->currentjobid = null;
Expand All @@ -295,7 +295,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
// Got a useable result from Jobe server so cache it if required.
if (WRITE_TO_CACHE) {
$key = hash("md5", serialize($runspec));
$cache->set($key, $runresult); // set serializes the result, get will unserialize.
$cache->set($key, $runresult); // Set serializes the result, get will unserialize.
// echo 'CACHE WRITE for ---> ' . $key . '<br>';
}
}
Expand Down

0 comments on commit eb9aca5

Please sign in to comment.