Skip to content

Commit a44edda

Browse files
committed
Add log endpoint to CEM api
1 parent 114301c commit a44edda

3 files changed

Lines changed: 39 additions & 29 deletions

File tree

api/v1/cem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,21 @@ public function post() {
217217
Factory::getNodeFactory()->update($node);
218218
break;
219219

220+
case(strtolower('jobLog')):
221+
$jobId = trim($this->request['jobId']);
222+
$job = Factory::getJobFactory()->get($jobId);
223+
if (!$job) {
224+
$this->setStatusCode(API::STATUS_NUM_JOB_DOES_NOT_EXIST);
225+
throw new Exception('Job does not exist!');
226+
}
227+
if (empty($this->request['message'])) {
228+
throw new Exception('No log message provided.');
229+
}
230+
// Make log messages blue
231+
$message = "\e[34m" . "CEM > " . $this->request['message'] . "\e[0m\n";
232+
Util::appendToJobLog($job, $message );
233+
break;
234+
220235
default:
221236
throw new Exception('Unsupported action');
222237
}

api/v1/job.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ public function post() {
134134
}
135135
break;
136136
case(strtolower('appendLog')):
137-
$this->appendLog($this->get['id']);
137+
if (empty($this->request['log'])) {
138+
throw new Exception('No or insufficient data provided.');
139+
}
140+
Util::appendToJobLog($job, $this->request['log']);
138141
break;
142+
139143
case 'upload':
140144
$this->upload($job);
141145
break;
@@ -270,32 +274,4 @@ private function getHttpUploadTarget($id) {
270274
return $data;
271275
}
272276

273-
274-
/**
275-
* @param $id
276-
* @throws Exception
277-
*/
278-
private function appendLog($id) {
279-
$job = Factory::getJobFactory()->get($id);
280-
if (!$job) {
281-
$this->setStatusCode(API::STATUS_NUM_JOB_DOES_NOT_EXIST);
282-
throw new Exception('Job does not exist!');
283-
}
284-
if (empty($this->request['log'])) {
285-
throw new Exception('No or insufficient data provided.');
286-
}
287-
288-
// check if the data directory is mounted and if not, mount it
289-
/*$mount = new Mount_Library();
290-
if ($mount->checkIfDataDirectoryIsMounted() === false) {
291-
Logger_Library::getInstance()->warning("Data directory is not mounted. Execute mount!");
292-
$mount->mountDataDirectory();
293-
}*/ // skip mount
294-
295-
// write log to file
296-
if (!file_exists(UPLOADED_DATA_PATH . 'log')) {
297-
mkdir(UPLOADED_DATA_PATH . 'log');
298-
}
299-
file_put_contents(UPLOADED_DATA_PATH . 'log/' . $id . '.log', $this->request['log'], FILE_APPEND);
300-
}
301277
}

core/util.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,23 @@ public static function extractSoftwareVersionElements($experiment): array {
610610
return $versionInputs;
611611
}
612612

613+
/**
614+
* @param Job $job
615+
* @param string $message
616+
*/
617+
public static function appendToJobLog(DBA\Job $job, string $message) {
618+
// check if the data directory is mounted and if not, mount it
619+
/*$mount = new Mount_Library();
620+
if ($mount->checkIfDataDirectoryIsMounted() === false) {
621+
Logger_Library::getInstance()->warning("Data directory is not mounted. Execute mount!");
622+
$mount->mountDataDirectory();
623+
}*/ // skip mount
624+
625+
// write log to file
626+
if (!file_exists(UPLOADED_DATA_PATH . 'log')) {
627+
mkdir(UPLOADED_DATA_PATH . 'log');
628+
}
629+
file_put_contents(UPLOADED_DATA_PATH . 'log/' . $job->getId() . '.log', $message, FILE_APPEND);
630+
}
631+
613632
}

0 commit comments

Comments
 (0)