1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
import {debounce} from 'core/utils';
|
|
|
17 |
import {LoadingProcess} from 'core/local/process_monitor/loadingprocess';
|
|
|
18 |
import log from 'core/log';
|
|
|
19 |
|
|
|
20 |
const TOASTSTIMER = 3000;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* A process queue manager.
|
|
|
24 |
*
|
|
|
25 |
* Adding process to the queue will guarante process are executed in sequence.
|
|
|
26 |
*
|
|
|
27 |
* @module core/local/process_monitor/processqueue
|
|
|
28 |
* @class ProcessQueue
|
|
|
29 |
* @copyright 2022 Ferran Recio <ferran@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
export class ProcessQueue {
|
|
|
33 |
/** @var {Array} pending the pending queue. */
|
|
|
34 |
pending = [];
|
|
|
35 |
|
|
|
36 |
/** @var {LoadingProcess} current the current uploading process. */
|
|
|
37 |
currentProcess = null;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Class constructor.
|
|
|
41 |
* @param {ProcessMonitorManager} manager the monitor manager
|
|
|
42 |
*/
|
|
|
43 |
constructor(manager) {
|
|
|
44 |
this.manager = manager;
|
|
|
45 |
this.cleanFinishedProcesses = debounce(
|
|
|
46 |
() => manager.dispatch('cleanFinishedProcesses'),
|
|
|
47 |
TOASTSTIMER
|
|
|
48 |
);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Adds a new pending upload to the queue.
|
|
|
53 |
* @param {String} processName the process name
|
|
|
54 |
* @param {Function} processor the execution function
|
|
|
55 |
*/
|
|
|
56 |
addPending(processName, processor) {
|
|
|
57 |
const process = new LoadingProcess(this.manager, {name: processName});
|
|
|
58 |
process.setExtraData({
|
|
|
59 |
processor,
|
|
|
60 |
});
|
|
|
61 |
process.onFinish((uploadedFile) => {
|
|
|
62 |
if (this.currentProcess?.id !== uploadedFile.id) {
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
this._discardCurrent();
|
|
|
66 |
});
|
|
|
67 |
this.pending.push(process);
|
|
|
68 |
this._continueProcessing();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Adds a new pending upload to the queue.
|
|
|
73 |
* @param {String} processName the file info
|
|
|
74 |
* @param {String} errorMessage the file processor
|
|
|
75 |
*/
|
|
|
76 |
addError(processName, errorMessage) {
|
|
|
77 |
const process = new LoadingProcess(this.manager, {name: processName});
|
|
|
78 |
process.setError(errorMessage);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Discard the current process and execute the next one if any.
|
|
|
83 |
*/
|
|
|
84 |
_discardCurrent() {
|
|
|
85 |
if (this.currentProcess) {
|
|
|
86 |
this.currentProcess = null;
|
|
|
87 |
}
|
|
|
88 |
this.cleanFinishedProcesses();
|
|
|
89 |
this._continueProcessing();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Return the current file uploader.
|
|
|
94 |
* @return {FileUploader}
|
|
|
95 |
*/
|
|
|
96 |
_currentProcessor() {
|
|
|
97 |
return this.currentProcess.data.processor;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Continue the queue processing if no current process is defined.
|
|
|
102 |
*/
|
|
|
103 |
async _continueProcessing() {
|
|
|
104 |
if (this.currentProcess !== null || this.pending.length === 0) {
|
|
|
105 |
return;
|
|
|
106 |
}
|
|
|
107 |
this.currentProcess = this.pending.shift();
|
|
|
108 |
try {
|
|
|
109 |
const processor = this._currentProcessor();
|
|
|
110 |
await processor(this.currentProcess);
|
|
|
111 |
} catch (error) {
|
|
|
112 |
this.currentProcess.setError(error.message);
|
|
|
113 |
log.error(error);
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|