From 4e63b531de6d012b54eed74094b7b0685dcacb7f Mon Sep 17 00:00:00 2001 From: "Heekyoung, Oh" Date: Wed, 19 Apr 2017 14:22:23 +0900 Subject: [PATCH] [TIC-Web] Update the function for the cancel - to cancel for each - Change-Id: Idc93db324e0aa27aece2e0b9a2e51e7d5140ed59 Signed-off-by: Heekyoung, Oh --- config.json | 1 + controller/mic.js | 68 +++++++++++-------------------- controller/socketio.js | 2 +- public/src/css/style.css | 7 +++- public/src/js/model/JobModel.js | 10 +++++ public/src/js/page/job.js | 77 ++++++++++++++++++++++++++++++++++-- public/src/js/widget/JobTableItem.js | 12 ++++-- 7 files changed, 123 insertions(+), 54 deletions(-) diff --git a/config.json b/config.json index 6756b19..1c95f09 100644 --- a/config.json +++ b/config.json @@ -35,6 +35,7 @@ "FS_IMAGE_ADD_TO": "ws/fs/image/add/to", "FS_IMAGE_ADD_FINISH": "ws/fs/image/add/finish", "FS_IMAGE_ADD_FAIL": "ws/fs/image/add/fail", + "FS_IMAGE_ADD_CANCEL": "ws/fs/image/add/cancel", "FS_IMAGE_ADD_KILL": "ws/fs/image/add/kill", "MIC_AVAILABLE_FROM": "ws/mic/available/from", "MIC_AVAILABLE_TO": "ws/mic/available/to", diff --git a/controller/mic.js b/controller/mic.js index 9da93ef..30dc3e6 100644 --- a/controller/mic.js +++ b/controller/mic.js @@ -33,16 +33,6 @@ var PROCESS_CNT_ONE = 1; var Mic = {}; -/** - * Process Manager - * ObjectArray - * processMgr = { - * 'jobId': jobId, - * 'jobProcessId': pid - * }); - */ -var processMgr = []; - Mic.hasNextJob = function hasNextJob(callback) { function onSuccess(rows) { var queueCnt, hasNext; @@ -73,37 +63,25 @@ Mic.isAvailable = function isAvailable(callback) { } Mic.kill = function kill(paramObj) { - var strJobId, psIndex, psObj, psId; - logger.info('MIC Kill: paramObj = ' + JSON.stringify(paramObj)); - - strJobId = paramObj.jobId; + var jobId, jobPid; + logger.info('Mic.Kill: ' + JSON.stringify(paramObj)); - psIndex = _.findIndex(processMgr, function (o) { - return o.jobId == strJobId; - }) + // set values + jobId = paramObj.jobId; + jobPid = paramObj.jobPid; // when not exists - if (psIndex < 0) { + if (!jobId || !jobPid) { return; } - psObj = processMgr[psIndex]; - psId = psObj.jobProcessId; - - TreeKill(psId, 'SIGKILL'); - processMgr.pop(psIndex); + TreeKill(jobPid, 'SIGKILL'); } Mic.process = function process(command, callback, jobId) { + var ps, psOptions; // execution - var ps = exec(command); - - // Process Manager - add - processMgr.push({ - 'jobId': jobId, - 'jobProcessId': ps.pid - }); - + ps = exec(command); ps.stdout.on('data', callback.stdout || function (out) { process.stdout.write(out); }); @@ -111,8 +89,8 @@ Mic.process = function process(command, callback, jobId) { process.stdout.write(err); }); ps.on('error', callback.error); + ps.on('close', callback.close); ps.on('exit', callback.exit); - return ps; }; @@ -153,34 +131,34 @@ Mic.create = function create(paramObj, io) { micCommand, { stdout: function (out) { - logger.info(out); + logger.info('MICProcess.stdout:' + out); sendMsg(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_TO, { msg: out, jobId: strJobId }); }, stderr: function (out) { - logger.info(out); + logger.info('MICProcess.stderr:' + out); sendMsg(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_TO, { msg: out, jobId: strJobId }); }, error: function (out) { - logger.info(out); + logger.error('MICProcess.error:' + out); sendMsg(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_FAIL, 'Failed (' + code + ')'); }, + close: function (code) { + logger.info('MICProcess.colse: all process were closed.') + }, exit: function (code) { - // code is the final exit code of the process - var psIndex = _.findIndex(processMgr, function (o) { - return o.jobId == strJobId; - }); - if (psIndex >= 0) { - processMgr.pop(psIndex); - } - - logger.info('Terminated (' + code + ')'); - if (code === 0) { + logger.info('MICProcess.exit: Terminated (' + code + ')'); + if (!code) { + sendMsg(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_CANCEL, { + msg: 'Canceled (' + code + ')', + jobId: strJobId + }); + } else if (code === 0) { sendMsg(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_FINISH, { msg: 'Terminated (' + code + ')', jobId: strJobId, diff --git a/controller/socketio.js b/controller/socketio.js index 909c066..029a156 100644 --- a/controller/socketio.js +++ b/controller/socketio.js @@ -62,7 +62,7 @@ var listen = function listen (server) { }); /** - * AppConfig.EVENT.SOCKET.MIC_AVAILABLE_FROM = 'ws/mic/nextjob/from' + * AppConfig.EVENT.SOCKET.MIC_NEXT_JOB_FROM = 'ws/mic/nextjob/from' */ socket.on(AppConfig.EVENT.SOCKET.MIC_NEXT_JOB_FROM, function () { Mic.hasNextJob(function (hasNext) { diff --git a/public/src/css/style.css b/public/src/css/style.css index f3ebd73..8da8c7a 100644 --- a/public/src/css/style.css +++ b/public/src/css/style.css @@ -179,7 +179,12 @@ body { color: #337ab7; display: table-cell; } -.job-status-cancel, .job-status-failed { +.job-status-cancel { + font-size: 2vh; + color: #2d2222; + display: table-cell; +} +.job-status-failed { font-size: 2vh; color: #e05151; display: table-cell; diff --git a/public/src/js/model/JobModel.js b/public/src/js/model/JobModel.js index 1107cc6..f0a8de6 100644 --- a/public/src/js/model/JobModel.js +++ b/public/src/js/model/JobModel.js @@ -49,6 +49,7 @@ define([ this.jobStatusClass = null; this.jobImageName = null; this.jobImageSize = null; + this.jobPid = null; this.jobPath = null; this.jobImagePath = null; this.jobHasKsFile = false; @@ -82,6 +83,7 @@ define([ this.setJobStatus(obj.job_status); this.setJobImageName(obj.job_image_name); this.setJobImageSize(obj.job_image_size); + this.setJobPid(obj.job_pid); this.setJobHasKsFile(obj.job_hasksfile); this.setJobKs(obj.job_ks); @@ -200,6 +202,14 @@ define([ this.jobImageSize = value ? Util.bytesToSize(value) : '0KB'; }; + JobModel.prototype.getJobPid = function () { + return this.jobPid; + }; + + JobModel.prototype.setJobPid = function (value) { + this.jobPid = value || null; + }; + JobModel.prototype.getJobPath = function () { return this.jobPath; }; diff --git a/public/src/js/page/job.js b/public/src/js/page/job.js index 3959f3b..23e186b 100644 --- a/public/src/js/page/job.js +++ b/public/src/js/page/job.js @@ -62,7 +62,9 @@ define([ // const var JOB_STATUS_DONE = 'DONE'; + var JOB_STATUS_READY = 'READY'; var JOB_STATUS_INPROGRESS = 'INPROGRESS'; + var JOB_STATUS_CANCELED = 'CANCELED'; var JOB_STATUS_FAILED = 'FAILED'; var USER_DEFAULT = 'GUEST'; @@ -104,17 +106,45 @@ define([ // bind events - cancel button $('#tic-job-table a.btncancel').on('click', function (e) { - var msgData, jobId; + var msgData, jobId, jobPid, jobStatus; + e.preventDefault(); + // btn disabled $(this).addClass('btnnotactive'); + // checks that the status of job, whether ready or inprogress or not + jobStatus = $(this).data('jobstatus'); + if (!(jobStatus === JOB_STATUS_READY || jobStatus === JOB_STATUS_INPROGRESS)) { + return; + } + jobId = $(this).data('jobid'); + jobPid = $(this).data('jobpid'); + + if (jobStatus === JOB_STATUS_INPROGRESS) { + if (!jobPid) { + return; + } + } + msgData = { - jobId: jobId + jobId: jobId, + job_status: JOB_STATUS_CANCELED, + jobPid: jobPid, + job_updater: UserInfo.email }; - client.emit(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_KILL, msgData); + if (jobStatus === JOB_STATUS_READY) { + // update job_status + Util.POST(AppConfig.EVENT.JOB.JOB_EDIT_ONE + jobId, msgData) + .then(function () { + updateList(); + }); + } else if (jobStatus === JOB_STATUS_INPROGRESS) { + // kill + client.emit(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_KILL, msgData); + } }); } @@ -229,7 +259,8 @@ define([ job_updater: UserInfo.email }; logger.info('MIC_ADD_PID_JOB_TO: ' + JSON.stringify(msgObj)); - Util.POST(AppConfig.EVENT.JOB.JOB_EDIT_ONE + dataObj.jobId, msgObj); + // update tic_job; + return Util.POST(AppConfig.EVENT.JOB.JOB_EDIT_ONE + dataObj.jobId, msgObj); }); /** @@ -394,6 +425,44 @@ define([ updateList(ModelJobPaging.getCurrentPoint()); }); }); + + /** + * when canceled + * + * AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_CANCEL = 'ws/fs/image/add/cancel' + */ + client.on(AppConfig.EVENT.SOCKET.FS_IMAGE_ADD_CANCEL, function (dataObj) { + var jobId, msgObj; + + // jobId + jobId = dataObj.jobId; + + // log + new JobLog(dataObj); + + // log + new JobLog({ + msg: 'Canceled to create an image.', + jobId: jobId + }); + + // notification popup + Util.showAlertDialog('Canceled to create an image. The #ID is ' + jobId + '.'); + + // update the status + msgObj = { + jobId: jobId, + job_status: JOB_STATUS_CANCELED, + job_updater: UserInfo.email + }; + Util.POST(AppConfig.EVENT.JOB.JOB_EDIT_ONE + jobId, msgObj) + .then(checksNextJob) + .then(function () { + // upate the list of jobs + updateList(ModelJobPaging.getCurrentPoint()); + }); + + }); } /** diff --git a/public/src/js/widget/JobTableItem.js b/public/src/js/widget/JobTableItem.js index d935961..b7ff0bf 100644 --- a/public/src/js/widget/JobTableItem.js +++ b/public/src/js/widget/JobTableItem.js @@ -32,7 +32,7 @@ define([ 'Download', '<%= jobImageSize %>', '<%= jobUptime %>', - 'Cancel', + 'Cancel', 'KS', 'Log', '' @@ -45,7 +45,7 @@ define([ 'Download', '<%= jobImageSize %>', '<%= jobUptime %>', - 'Cancel', + 'Cancel', 'KS', 'Log', '', @@ -58,6 +58,7 @@ define([ ]; var JOB_STATUS_INPROGRESS = 'INPROGRESS'; + var JOB_STATUS_READY = 'READY'; var JOB_STATUS_DONE = 'DONE'; var USER_GROUP_ADMIN = 'ADMIN'; var USER_GROUP_MASTER = 'MASTER'; @@ -86,7 +87,8 @@ define([ userGroup = data.getJobUserGroup(); cssCancelbtn = ''; if (userGroup === USER_GROUP_ADMIN || userGroup === USER_GROUP_MASTER) { - if (statusValue !== JOB_STATUS_INPROGRESS) { + // only when 'READY' or 'INPROGRESS', can cancel the job. + if (!(statusValue === JOB_STATUS_INPROGRESS || statusValue === JOB_STATUS_READY)) { cssCancelbtn = 'btnnotactive'; } } else { @@ -104,6 +106,8 @@ define([ 'jobImageName': data.getJobImageName(), 'jobImageSize': data.getJobImageSize(), 'jobImagePath': data.getJobAbsImagePath(), + 'jobPid': data.getJobPid(), + 'jobStatusValue': data.getJobStatus(), 'classJobImageDownload': data.getJobStatus() === JOB_STATUS_DONE ? '' : 'btnnotactive', 'classJobCancel': getCssCancelbtn() , 'classJobKsPath': data.getJobHasKsFile() === '0' ? 'btnnotactive' : '', @@ -122,6 +126,8 @@ define([ 'jobImageName': data.getJobImageName(), 'jobImageSize': data.getJobImageSize(), 'jobImagePath': data.getJobAbsImagePath(), + 'jobPid': data.getJobPid(), + 'jobStatusValue': data.getJobStatus(), 'classJobCancel': getCssCancelbtn() , 'classJobKsPath': data.getJobHasKsFile() === '0' ? 'btnnotactive' : '', 'jobKsPath': data.getJobAbsKsPath(), -- 2.7.4