-
Notifications
You must be signed in to change notification settings - Fork 5
/
Test1.ts
69 lines (58 loc) · 2.47 KB
/
Test1.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import {LongRun} from "../LongRun";
function executeTest1() {
const params = [];
params.push(3); // How many times the process should be executed
params.push(1); // How long does it take to process one case? (in seconds)
params.push(1); // Maximum acceptable run time in seconds (less than 6 minutes, of course)
params.push(1); // How many minutes later the next trigger will be activated
LongRun.instance.setParameters('LongRunTask', params);
LongRunTask();
}
function LongRunTask(
// there must be no arguments, because the parameters must be retrieved from LongRun class.
/* times: number, funcExecutionSeconds: number, maxExecutionSeconds: number, triggerDelayMinutes: number */
) {
let longRun = LongRun.instance;
// funcName must equal this function's name.
const funcName = 'LongRunTask';
// you can get the parameters from LongRun class.
const params = longRun.getParameters(funcName);
const times = parseInt(params[0]);
const funcExecutionSeconds = parseInt(params[1]);
const maxExecutionSeconds = parseInt(params[2]);
const triggerDelayMinutes = parseInt(params[3]);
// you can set the long-running configurations. of course you can use the default values.
longRun.setMaxExecutionSeconds(maxExecutionSeconds); // default is 240 seconds
longRun.setTriggerDelayMinutes(triggerDelayMinutes); // default is 1 minute
// you should get the index to resume(zero for the first time)
let startIndex = longRun.startOrResume(funcName);
if( startIndex === 0 ){
console.log('--- LongRunTask started. ---');
}
try {
// Execute the iterative process.
for (let i = startIndex; i < times; i++) {
console.log('Processing: ' + i);
// Each time before executing a process, you need to check if it should be stopped or not.
if (longRun.checkShouldSuspend(funcName, i)) {
// if checkShouldSuspend() returns true, the next trigger has been set
// and you should get out of the loop.
console.log('*** The process has been suspended. ***');
break;
}
// *** code your main process here! ***
Utilities.sleep(funcExecutionSeconds * 1000); // demonstrate the process
console.log('Processing Done!: ' + i);
}
}
catch (e) {
console.log(e.message);
}
finally {
// you must always call end() to reset the long-running variables if there is no next trigger.
const finished = longRun.end(funcName);
if( finished ){
console.log('--- LongRunTask finished. ---');
}
}
}