From ee3948c40d387f84d3f7104232854f1aa07fc51d Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Fri, 8 Dec 2017 16:45:49 +0800 Subject: [PATCH] #13 hotfix to resolve SN for automation --- lib/store.js | 22 ++++++++++++++++++++++ web/controllers/api.js | 32 +++++++++++++++++++------------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/store.js b/lib/store.js index e36c527..a54f837 100644 --- a/lib/store.js +++ b/lib/store.js @@ -115,6 +115,28 @@ const Store = new Class({ }); }, + /** + * 计算下一个 Record执行的sn + */ + resolveNextRecordSN: function(job, callback){ + var self = this; + callback = callback || utils.NOOP; + return self.Record.find({ + projectName: job.project ? job.project.name : job.projectName, + name: job.name + }, { offset: 0 }, 1, + ['sn', 'Z'], function (err, records) { + if (err) return callback(err); + if (job.refused) { + // refused 的 Job 的 sn 固定为 0; + job.sn = 0; + } else { + job.sn = records && records[0] ? records[0].sn + 1 : 1; + } + callback(null, job.sn); + }); + }, + /** * 插入 job 执行记录 * @param {Job} job job 实例 diff --git a/web/controllers/api.js b/web/controllers/api.js index 9e3c258..6745c77 100644 --- a/web/controllers/api.js +++ b/web/controllers/api.js @@ -52,19 +52,25 @@ const ApiController = nokit.define({ **/ trigger: function (context, params) { var self = this; - self.server.ci.externalInvoke( - self.projectName, - self.jobName, - { - params: params || self.params - }, - null, - function (started) { - self.send(started ? 202 : 400, { - message: started ? 'Job is triggered' : 'Trigger failed' - }); - } - ); + self.server.ci.store.resolveNextRecordSN({ + projectName: self.projectName, + name: self.jobName, + refused: false + }, function(err, sn) { + self.server.ci.externalInvoke( + self.projectName, + self.jobName, { + params: params || self.params + }, + null, + function(started) { + self.send(started ? 202 : 400, { + message: started ? 'Job is triggered' : 'Trigger failed', + sn: sn + }); + } + ); + }); }, /**