From bab3a98ce5bd257e99a001f8d13bf25d1a7ec378 Mon Sep 17 00:00:00 2001 From: Newnius Date: Mon, 25 Mar 2019 15:40:28 +0800 Subject: [PATCH] update --- ajax.php | 15 +++- job.logic.php | 95 ++++++++++++++++++++++++-- modals.php | 91 +++++++++++++++---------- static/job.js | 170 ++++++++++++++++++++++++++++++++++++++++++---- static/ucenter.js | 4 ++ ucenter.php | 21 ++++++ 6 files changed, 342 insertions(+), 54 deletions(-) diff --git a/ajax.php b/ajax.php index 53a3560..b157f0e 100644 --- a/ajax.php +++ b/ajax.php @@ -68,7 +68,7 @@ switch ($action) { case 'job_submit': $job = new CRObject(); - $job->set('name', cr_get_POST('name')); + $job->set('name', cr_get_POST('name', '') . '-' . time()); $job->set('virtual_cluster', cr_get_POST('cluster')); $job->set('workspace', cr_get_POST('workspace')); $job->set('priority', cr_get_POST('priority')); @@ -90,6 +90,19 @@ switch ($action) { $res = job_describe($job); break; + case 'job_status': + $job = new CRObject(); + $job->set('name', cr_get_GET('name')); + $res = job_status($job); + break; + + case 'task_logs': + $task = new CRObject(); + $task->set('job', cr_get_GET('job')); + $task->set('task', cr_get_GET('task')); + $res = task_logs($task); + break; + case 'agent_list': $rule = new CRObject(); $rule->set('offset', cr_get_GET('offset')); diff --git a/job.logic.php b/job.logic.php index add7c24..24b3d4d 100644 --- a/job.logic.php +++ b/job.logic.php @@ -21,6 +21,7 @@ function job_submit(CRObject $job) return $res; } $job->set('created_by', Session::get('uid')); + $job->set('created_at', time()); $res['errno'] = JobManager::add($job) ? Code::SUCCESS : Code::UNKNOWN_ERROR; $log = new CRObject(); $log->set('scope', Session::get('uid')); @@ -28,7 +29,27 @@ function job_submit(CRObject $job) $content = array('job' => $job, 'response' => $res['errno']); $log->set('content', json_encode($content)); CRLogger::log($log); - /* TODO notify scheduler */ + + /* notify YAO-scheduler */ + $spider = new Spider(); + $tasks = json_decode($job->get('tasks'), true); + foreach ($tasks as $i => $task) { + $task['cpu_number'] = intval($task['cpu_number']); + $task['memory'] = intval($task['memory']); + $task['gpu_number'] = intval($task['gpu_number']); + $task['gpu_memory'] = intval($task['gpu_memory']); + $tasks[$i] = $task; + } + $job->set('tasks', $tasks); + $job->set('workspace', $job->getInt('workspace')); + $job->set('virtual_cluster', $job->getInt('virtual_cluster')); + $job->set('priority', $job->getInt('priority')); + $job->set('run_before', $job->getInt('run_before')); + $job->set('created_by', $job->getInt('created_by')); + $data['job'] = json_encode($job); + $spider->doPost(YAO_SCHEDULER_ADDR . '?action=job_submit', $data); + $res['message'] = $spider->getBody(); + return $res; } @@ -72,9 +93,75 @@ function job_list(CRObject $rule) $res['errno'] = Code::NO_PRIVILEGE; return $res; } - $res['jobs'] = JobManager::gets($rule); - $res['count'] = JobManager::count($rule); - $res['errno'] = $res['jobs'] === null ? Code::FAIL : Code::SUCCESS; + + + $spider = new Spider(); + $spider->doGet(YAO_SCHEDULER_ADDR . '?action=jobs'); + $msg = json_decode($spider->getBody(), true); + + + if ($msg['code'] !== 0) { + $res['errno'] = $msg['code']; + $res['msg'] = $msg['error']; + return $res; + } + + $res['jobs'] = array_reverse($msg['jobs']); + for ($i = 0; $i < sizeof($res['jobs']); $i++) { + $res['jobs'][$i]['tasks'] = json_encode($res['jobs'][$i]['tasks']); + if ($res['jobs'][$i]['run_before'] === 0) { + $res['jobs'][$i]['run_before'] = null; + } + } + $res['errno'] = Code::SUCCESS; + + //$res['jobs'] = JobManager::gets($rule); + //$res['count'] = JobManager::count($rule); + //$res['errno'] = $res['jobs'] === null ? Code::FAIL : Code::SUCCESS; + return $res; +} + +function job_status(CRObject $job) +{ + if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'job.list')) { + $res['errno'] = Code::NO_PRIVILEGE; + return $res; + } + + $spider = new Spider(); + $spider->doGet(YAO_SCHEDULER_ADDR . '?action=job_status&id=' . $job->get('name')); + $msg = json_decode($spider->getBody(), true); + + if ($msg['code'] !== 0) { + $res['errno'] = $msg['code']; + $res['msg'] = $msg['error']; + return $res; + } + + $res['tasks'] = array_reverse($msg['status']); + $res['errno'] = Code::SUCCESS; + return $res; +} + +function task_logs(CRObject $job) +{ + if (!AccessController::hasAccess(Session::get('role', 'visitor'), 'job.list')) { + $res['errno'] = Code::NO_PRIVILEGE; + return $res; + } + + $spider = new Spider(); + $spider->doGet(YAO_SCHEDULER_ADDR . '?action=task_logs&job=' . $job->get('job') . '&task=' . $job->get('task')); + $msg = json_decode($spider->getBody(), true); + + if ($msg['code'] !== 0) { + $res['errno'] = $msg['code']; + $res['msg'] = $msg['error']; + return $res; + } + + $res['logs'] = $msg['logs']; + $res['errno'] = Code::SUCCESS; return $res; } diff --git a/modals.php b/modals.php index 45d5cce..8c95d52 100755 --- a/modals.php +++ b/modals.php @@ -31,6 +31,23 @@ + + + -
-
- -
- +
+
+
+ +
+ +
-
-
- -
- +
+ +
+ +
-
-
- -
- +
+ +
+ +
-
-
- -
- +
+ +
+ +
-
-
- -
- +
+ +
+ +
-
-
- -
- +
+ +
+ +
diff --git a/static/job.js b/static/job.js index ba218d9..ffdf156 100755 --- a/static/job.js +++ b/static/job.js @@ -14,6 +14,17 @@ function register_events_job() { run_before = moment(run_before).unix(); } var tasks = []; + $('#form-job-tasks').find('.row').each(function () { + var vals = $(this).find('input'); + var task = {}; + task['name'] = vals.eq(0).val(); + task['cmd'] = vals.eq(1).val(); + task['cpu_number'] = vals.eq(2).val(); + task['memory'] = vals.eq(3).val(); + task['gpu_number'] = vals.eq(4).val(); + task['gpu_memory'] = vals.eq(5).val(); + tasks.push(task); + }); /* TODO validate form */ @@ -153,29 +164,33 @@ var clusterFormatter = function (cluster) { }; var priorityFormatter = function (status) { + status = parseInt(status); switch (status) { - case '1': + case 1: return 'Low'; - case '25': + case 25: return 'Medium'; - case '50': + case 50: return 'High'; - case '99': + case 99: return 'Urgent'; } return 'Unknown (' + status + ')'; }; var statusFormatter = function (status) { + status = parseInt(status); switch (status) { - case '0': - return 'Pending'; - case '1': + case 0: + return 'Created'; + case 1: + return 'Starting'; + case 2: return 'Running'; - case '2': - return 'Finished'; - case '3': + case 3: return 'Stopped'; + case 4: + return 'Finished'; } return 'Unknown(' + status + ')'; }; @@ -195,19 +210,25 @@ function jobResponseHandler(res) { function jobOperateFormatter(value, row, index) { var div = '
'; if (page_type === 'jobs') - div += ''; - if (page_type === 'jobs' && (row.status === '0' || row.status === '1')) + div += ''; + if (page_type === 'jobs') + div += ''; + if (page_type === 'jobs' && (parseInt(row.status) === 0 || parseInt(row.status) === 1)) div += ''; div += '
'; return div; } window.jobOperateEvents = { - 'click .view': function (e, value, row, index) { + 'click .config': function (e, value, row, index) { + row.tasks = JSON.parse(row.tasks); var formattedData = JSON.stringify(row, null, '\t'); $('#modal-job-description-content').text(formattedData); $('#modal-job-description').modal('show'); }, + 'click .stats': function (e, value, row, index) { + window.open("?job_status&name=" + row.name); + }, 'click .stop': function (e, value, row, index) { if (!confirm('Are you sure to stop this job?')) { return; @@ -230,4 +251,127 @@ window.jobOperateEvents = { $('#table-job').bootstrapTable("refresh"); }); } +}; + +function load_job_status(name) { + $("#table-task").bootstrapTable({ + url: window.config.BASE_URL + '/service?action=job_status&name=' + name, + responseHandler: jobStatusResponseHandler, + sidePagination: 'server', + cache: false, + striped: true, + pagination: false, + pageSize: 10, + pageList: [10, 25, 50, 100, 200], + search: false, + showColumns: true, + showRefresh: true, + showToggle: false, + showPaginationSwitch: true, + minimumCountColumns: 2, + clickToSelect: false, + sortName: 'nobody', + sortOrder: 'desc', + smartDisplay: true, + mobileResponsive: true, + showExport: true, + columns: [{ + field: 'id', + title: 'ID', + align: 'center', + valign: 'middle' + }, { + field: 'image', + title: 'Image', + align: 'center', + valign: 'middle', + visible: false, + }, { + field: 'image_digest', + title: 'Image Version', + align: 'center', + valign: 'middle', + visible: false, + }, { + field: 'hostname', + title: 'Hostname', + align: 'center', + valign: 'middle' + }, { + field: 'command', + title: 'Command', + align: 'center', + valign: 'middle' + }, { + field: 'created_at', + title: 'Created At', + align: 'center', + valign: 'middle' + }, { + field: 'finished_at', + title: 'Finished At', + align: 'center', + valign: 'middle', + visible: false + }, { + field: 'status', + title: 'Status', + align: 'center', + valign: 'middle' + }, { + field: 'operate', + title: 'Operate', + align: 'center', + events: jobStatusOperateEvents, + formatter: jobStatusOperateFormatter + }] + }); +} + +function jobStatusResponseHandler(res) { + if (res['errno'] === 0) { + var tmp = {}; + tmp["total"] = res["count"]; + tmp["rows"] = res["tasks"]; + return tmp; + } + $("#modal-msg-content").html(res["msg"]); + $("#modal-msg").modal('show'); + return []; +} + +function jobStatusOperateFormatter(value, row, index) { + var div = '
'; + div += ''; + div += '
'; + return div; +} + +window.jobStatusOperateEvents = { + 'click .logs': function (e, value, row, index) { + var job = getParameterByName('name'); + var task = row.id; + + var ajax = $.ajax({ + url: window.config.BASE_URL + "/service?action=task_logs", + type: 'GET', + data: { + job: job, + task: task + } + }); + ajax.done(function (res) { + if (res["errno"] !== 0) { + $("#modal-msg-content").html(res["msg"]); + $("#modal-msg").modal('show'); + } + $('#modal-task-logs-content').text(res['logs']); + $('#modal-task-logs').modal('show'); + }); + ajax.fail(function (jqXHR, textStatus) { + $("#modal-msg-content").html("Request failed : " + jqXHR.statusText); + $("#modal-msg").modal('show'); + $('#table-job').bootstrapTable("refresh"); + }); + } }; \ No newline at end of file diff --git a/static/ucenter.js b/static/ucenter.js index 0200b95..1443671 100755 --- a/static/ucenter.js +++ b/static/ucenter.js @@ -11,6 +11,10 @@ $(function () { register_events_job(); load_jobs('self'); break; + case "job_status": + register_events_job(); + load_job_status(getParameterByName('name')); + break; case "agents": register_events_agent(); load_agents(''); diff --git a/ucenter.php b/ucenter.php index 1621770..c491655 100755 --- a/ucenter.php +++ b/ucenter.php @@ -30,6 +30,9 @@ if (isset($_GET['logs'])) { } elseif (isset($_GET['jobs'])) { $page_type = 'jobs'; +} elseif (isset($_GET['job_status'])) { + $page_type = 'job_status'; + } elseif (isset($_GET['jobs_all'])) { $page_type = 'jobs_all'; @@ -147,6 +150,24 @@ foreach ($entries as $entry) {
+ +
+
+
Job Status
+
+
+
+ +
+ +
+
+
+
+
+