Skip to content

Commit dfd3d34

Browse files
committed
fix multiple scripts executing simultaneously
1 parent 1e19fa0 commit dfd3d34

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

src/js/module/workbench/script/execute.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function Execute(data) {
4040
});
4141
this.on('execute:queryState', () => {
4242
this.queryStatus({ isKill: false });
43+
this.queryProgress();
4344
});
4445
this.on('stateEnd', () => {
4546
this.getResultPath();
@@ -121,9 +122,6 @@ function Execute(data) {
121122
});
122123
this.on('downgrade', ({ data, execute }) => {
123124
execute.postType = 'http';
124-
if (data) {
125-
execute.httpExecute();
126-
}
127125
});
128126
this.on('dataError', ({ data, execute }) => {
129127
execute.run = false;
@@ -156,6 +154,9 @@ Execute.prototype.restore = function({ execID, taskID }) {
156154
Execute.prototype.on = function(name, cb) {
157155
this.event.$on(name, cb);
158156
};
157+
Execute.prototype.off = function() {
158+
this.event.$off();
159+
};
159160
Execute.prototype.once = function(name, cb) {
160161
this.event.$once(name, cb);
161162
};

src/js/module/workbench/script/script.vue

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<script>
120120
import _ from 'lodash';
121121
import api from '@/js/service/api';
122+
import storage from '@/js/helper/storage';
122123
import util from '@/js/util';
123124
import module from '../index';
124125
import { Script } from '../modal.js';
@@ -145,7 +146,14 @@ export default {
145146
},
146147
data() {
147148
return {
148-
script: null,
149+
script: {
150+
data: '',
151+
oldData: '',
152+
result: {},
153+
steps: [],
154+
progress: {},
155+
resultList: null,
156+
},
149157
scriptViewState: {
150158
showPanel: 'log',
151159
height: 0,
@@ -178,20 +186,12 @@ export default {
178186
watch: {
179187
'script.data': function() {
180188
if (!this.work.ismodifyByOldTab) {
181-
if (this.script.data == this.script.oldData) {
182-
this.work.unsave = false;
183-
} else {
184-
this.work.unsave = true;
185-
}
189+
this.work.unsave = this.script.data != this.script.oldData;
186190
}
187191
},
188192
'script.oldData': function() {
189193
if (!this.work.ismodifyByOldTab) {
190-
if (this.script.data == this.script.oldData) {
191-
this.work.unsave = false;
192-
} else {
193-
this.work.unsave = true;
194-
}
194+
this.work.unsave = this.script.data != this.script.oldData;
195195
}
196196
},
197197
'execute.run': function(val) {
@@ -215,6 +215,7 @@ export default {
215215
this.userName = this.getUserName();
216216
// 如果当前work存在data,表示工作已经被打开
217217
if (this.work.data) {
218+
delete this.work.data.oldData;
218219
this.script = this.work.data;
219220
// 用缓存的scriptViewState替换当前this.scriptViewState
220221
this.scriptViewState = this.script.scriptViewState;
@@ -231,14 +232,13 @@ export default {
231232
data: this.work.code,
232233
params: this.work.params,
233234
}));
234-
// 删掉无用的code和params,因为已经存储在script对象中
235-
delete this.work.code;
236-
delete this.work.params;
237235
// 把新创建的scriptViewState挂到script对象上
238-
239236
this.script.scriptViewState = this.scriptViewState;
240237
}
241-
238+
// 删掉无用的code和params,因为已经存储在script对象中
239+
delete this.work.code;
240+
delete this.work.params;
241+
this.script.oldData = this.script.data;
242242
this.dispatch('IndexedDB:getResult', { tabId: this.script.id,
243243
cb: (resultList) => {
244244
if (resultList) {
@@ -269,9 +269,9 @@ export default {
269269
this.dispatch('IndexedDB:getHistory', { tabId: this.script.id,
270270
cb: (historyList) => {
271271
this.script.history = _.dropRight(_.values(historyList));
272-
this.script.status = this.script.history.length ? this.script.history[0].status : 'Inited';
273272
} });
274273
let cacheWork = await this.getCacheWork(this.work);
274+
this._running_scripts_key = 'running_scripts_' + this.userName;
275275
if (cacheWork) {
276276
let { data, taskID, execID } = cacheWork;
277277
// cacheWork.taskID && .execID && cacheWork.data && cacheWork.data.running
@@ -287,7 +287,27 @@ export default {
287287
this.work.execID = execID;
288288
this.work.taskID = taskID;
289289
}
290+
} else { // 脚本正在执行中关掉了Tab,之后再打开页面,执行进度恢复
291+
let runningScripts = storage.get(this._running_scripts_key, 'local') || {};
292+
if (runningScripts[this.script.id]) {
293+
// 存在execID表示任务已经在执行,否则任务已提交或排尚未真正执行
294+
if (runningScripts[this.script.id].execID) {
295+
this.script.steps = runningScripts[this.script.id].steps;
296+
this.script.progress = runningScripts[this.script.id].progress;
297+
this.run({
298+
taskID: runningScripts[this.script.id].taskID,
299+
execID: runningScripts[this.script.id].execID,
300+
isRestore: true,
301+
id: this.script.id,
302+
});
303+
this.work.execID = runningScripts[this.script.id].execID;
304+
this.work.taskID = runningScripts[this.script.id].taskID;
305+
}
306+
} else {
307+
this.script.status = 'Inited';
308+
}
290309
}
310+
291311
this.dispatch('IndexedDB:recordTab', this.work);
292312
this.dispatch('IndexedDB:updateGlobalCache', {
293313
id: this.userName,
@@ -310,6 +330,22 @@ export default {
310330
},
311331
beforeDestroy() {
312332
clearTimeout(this.autoSaveTimer);
333+
let runningScripts = storage.get(this._running_scripts_key, 'local') || {};
334+
if (this.script.running && this.execute.taskID && this.execute.id) {
335+
runningScripts[this.script.id] = {
336+
execID: this.execute.id,
337+
taskID: this.execute.taskID,
338+
progress: this.script.progress,
339+
steps: this.script.steps,
340+
};
341+
} else {
342+
delete runningScripts[this.script.id];
343+
}
344+
storage.set(this._running_scripts_key, runningScripts, 'local');
345+
if (this.execute) {
346+
this.execute.off();
347+
this.execute = null;
348+
}
313349
window.onbeforeunload = null;
314350
this.scriptViewState.bottomPanelHeight = this.scriptViewState.cacheBottomPanelHeight || this.scriptViewState.bottomPanelHeight;
315351
},
@@ -320,12 +356,13 @@ export default {
320356
}
321357
},
322358
'Workbench:socket'({ type, ...args }) {
359+
if (type === 'downgrade') {
360+
this.postType = 'http';
361+
}
323362
if (this.execute) {
324363
this.execute.trigger(type, Object.assign(args, {
325364
execute: this.execute,
326365
}));
327-
} else if (type === 'downgrade') {
328-
this.postType = 'http';
329366
}
330367
},
331368
'Workbench:run'(option, cb) {
@@ -603,7 +640,15 @@ export default {
603640
} else {
604641
progressInfo = [];
605642
}
643+
644+
if (progress == 1) {
645+
let runningScripts = storage.get(this._running_scripts_key, 'local') || {};
646+
delete runningScripts[this.script.id];
647+
storage.set(this._running_scripts_key, runningScripts, 'local');
648+
}
649+
606650
this.script.progress.current = progress;
651+
607652
if (waitingSize !== null && waitingSize >= 0) {
608653
this.script.progress.waitingSize = waitingSize;
609654
}
@@ -726,7 +771,6 @@ export default {
726771
},
727772
resetData() {
728773
// upgrade only one time
729-
this.postType = 'socket';
730774
this.script.result = null;
731775
this.script.log = { all: '', error: '', warning: '', info: '' };
732776
this.script.logLine = 1;

0 commit comments

Comments
 (0)