11import { CronJob } from 'cron' ;
2- import { buildThis } from '@App /pkg/sandbox/sandbox' ;
2+ import { buildThis } from '. /pkg/sandbox/sandbox' ;
33import { SandboxContext } from './apps/grant/frontend' ;
44import { SendLogger } from './pkg/utils/utils' ;
55import { App , ENV_FRONTEND , InitApp } from './apps/app' ;
@@ -23,46 +23,50 @@ type ExecType = 'run' | 'crontab' | 'retry' | 'debug';
2323
2424async function execScript (
2525 script : Script ,
26- func : Function ,
26+ func : ( param : AnyMap ) => any ,
2727 context : SandboxContext ,
2828 type : ExecType = 'run' ,
2929) : Promise < boolean > {
30- return new Promise ( async ( resolve , reject ) => {
30+ return new Promise ( ( resolve , reject ) => {
3131 //使用SandboxContext接管postRequest
3232 script . delayruntime = 0 ;
3333 context . CAT_setRunError ( '' , 0 ) ;
3434 script . lastruntime = new Date ( ) . getTime ( ) ;
3535 context . CAT_setLastRuntime ( script . lastruntime ) ;
36- SendLogger ( LOGGER_LEVEL_INFO , type , 'exec script id: ' + script . id , script . name , script . id ) ;
36+ SendLogger ( LOGGER_LEVEL_INFO , type , 'exec script id: ' + script . id . toString ( ) , script . name , script . id ) ;
3737 let execRet ;
3838 try {
3939 execRet = func ( buildThis ( window , context ) ) ;
4040 } catch ( error : any ) {
41- let msg = 'exec script id: ' + script . id + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
42- if ( error ) {
41+ let msg = 'exec script id: ' + script . id . toString ( ) + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
42+ let errMsg = '' ;
43+ if ( typeof error === 'string' ) {
4344 msg += ' error: ' + error ;
45+ errMsg = error ;
46+ } else {
47+ errMsg = 'unknow error' ;
4448 }
4549 SendLogger ( LOGGER_LEVEL_ERROR , type , msg , script . name , script . id ) ;
4650 script . delayruntime = 0 ;
47- context . CAT_setRunError ( error , script . delayruntime ) ;
51+ context . CAT_setRunError ( errMsg , script . delayruntime ) ;
4852 reject ( error ) ;
4953 return
5054 }
5155 if ( execRet instanceof Promise ) {
5256 execRet
5357 . then ( ( result : any ) => {
54- let msg = 'exec script id: ' + script . id + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
55- if ( result ) {
58+ let msg = 'exec script id: ' + script . id . toString ( ) + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
59+ if ( typeof result == 'string' ) {
5660 msg += ' result: ' + result ;
5761 }
5862 SendLogger ( LOGGER_LEVEL_INFO , type , msg , script . name , script . id ) ;
5963 context . CAT_runComplete ( ) ;
60- resolve ( result ) ;
64+ resolve ( true ) ;
6165 } )
6266 . catch ( ( error : string , delayrun = 0 ) => {
63- let msg = 'exec script id: ' + script . id + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
67+ let msg = 'exec script id: ' + script . id . toString ( ) + ' time: ' + ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) + 'ms'
6468 if ( error ) {
65- msg += ' error: ' + error + ( delayrun ? ' delayrun: ' + delayrun : '' )
69+ msg += ' error: ' + error + ( delayrun ? ' delayrun: ' + delayrun . toString ( ) : '' )
6670 }
6771 SendLogger ( LOGGER_LEVEL_ERROR , type , msg , script . name , script . id ) ;
6872 if ( delayrun > 0 ) {
@@ -74,14 +78,26 @@ async function execScript(
7478 reject ( error ) ;
7579 } ) ;
7680 } else {
81+ let result = ' result:' ;
82+ switch ( typeof execRet ) {
83+ case 'string' :
84+ result += execRet ;
85+ break ;
86+ case 'undefined' :
87+ result = '' ;
88+ break ;
89+ default :
90+ result += JSON . stringify ( execRet ) ;
91+ break ;
92+ }
7793 SendLogger (
7894 LOGGER_LEVEL_INFO ,
7995 type ,
8096 'exec script id: ' +
81- script . id +
97+ script . id . toString ( ) +
8298 ' time: ' +
8399 ( new Date ( ) . getTime ( ) - ( script . lastruntime || 0 ) ) . toString ( ) +
84- 'ms' + ( execRet ? ' result:' + execRet : '' ) ,
100+ 'ms' + result ,
85101 script . name , script . id
86102 ) ;
87103 context . CAT_runComplete ( ) ;
@@ -95,16 +111,16 @@ function start(script: ScriptCache): any {
95111 return runCrontab ( script ) ;
96112 } else if ( script . metadata [ 'background' ] ) {
97113 const context = createSandboxContext ( script ) ;
98- App . Cache . set ( 'script:' + script . id , context ) ;
99- execScript ( script , compileScript ( script ) , context , 'run' ) ;
100- return top ! . postMessage ( { action : 'start' , data : '' } , '*' ) ;
114+ void App . Cache . set ( 'script:' + script . id . toString ( ) , context ) ;
115+ void execScript ( script , compileScript ( script ) , context , 'run' ) ;
116+ return top ? .postMessage ( { action : 'start' , data : '' } , '*' ) ;
101117 }
102118}
103119
104120function runCrontab ( script : ScriptCache ) {
105121 const crontab = script . metadata [ 'crontab' ] ;
106122 const context = createSandboxContext ( script ) ;
107- App . Cache . set ( 'script:' + script . id , context ) ;
123+ void App . Cache . set ( 'script:' + script . id . toString ( ) , context ) ;
108124 const func = compileScript ( script ) ;
109125
110126 const list = new Array < CronJob > ( ) ;
@@ -132,13 +148,13 @@ function runCrontab(script: ScriptCache) {
132148 ( ) => {
133149 if ( oncePos ) {
134150 if ( ! script . lastruntime ) {
135- execScript ( script , func , context , 'crontab' ) ;
151+ void execScript ( script , func , context , 'crontab' ) ;
136152 return ;
137153 }
138154 const now = new Date ( ) ;
139155 if ( script . delayruntime && script . delayruntime < now . getTime ( ) ) {
140156 //TODO:使用单独的计时器执行
141- execScript ( script , func , context , 'retry' ) ;
157+ void execScript ( script , func , context , 'retry' ) ;
142158 return ;
143159 }
144160 if ( script . lastruntime > now . getTime ( ) ) {
@@ -167,10 +183,10 @@ function runCrontab(script: ScriptCache) {
167183 default :
168184 }
169185 if ( flag ) {
170- execScript ( script , func , context , 'crontab' ) ;
186+ void execScript ( script , func , context , 'crontab' ) ;
171187 }
172188 } else {
173- execScript ( script , func , context , 'crontab' ) ;
189+ void execScript ( script , func , context , 'crontab' ) ;
174190 }
175191 } ,
176192 null ,
@@ -179,44 +195,44 @@ function runCrontab(script: ScriptCache) {
179195 list . push ( cron ) ;
180196 } ) ;
181197 cronjobMap . set ( script . id , list ) ;
182- return top ! . postMessage ( { action : 'start' , data : '' } , '*' ) ;
198+ return top ? .postMessage ( { action : 'start' , data : '' } , '*' ) ;
183199}
184200
185- async function exec ( script : ScriptCache , isdebug : boolean ) {
201+ function exec ( script : ScriptCache , isdebug : boolean ) {
186202 const context = createSandboxContext ( script ) ;
187- App . Cache . set ( 'script:' + ( isdebug ? 'debug:' : '' ) + script . id , context ) ;
203+ void App . Cache . set ( 'script:' + ( isdebug ? 'debug:' : '' ) + script . id . toString ( ) , context ) ;
188204 execScript ( script , compileScript ( script ) , context , isdebug ? 'debug' : 'run' ) . then ( result => {
189- top ! . postMessage ( { action : 'exec respond' , data : 'success' , result : result } , '*' ) ;
205+ top ? .postMessage ( { action : 'exec respond' , data : 'success' , result : result } , '*' ) ;
190206 } ) . catch ( error => {
191- top ! . postMessage ( { action : 'exec respond' , data : 'error' , error : error } , '*' ) ;
207+ top ? .postMessage ( { action : 'exec respond' , data : 'error' , error : error } , '*' ) ;
192208 } ) ;
193- return top ! . postMessage ( { action : 'exec' , data : '' } , '*' ) ;
209+ return top ? .postMessage ( { action : 'exec' , data : '' } , '*' ) ;
194210}
195211
196212async function disable ( script : Script ) {
197213 const context = < SandboxContext > (
198- await App . Cache . get ( 'script:' + script . id )
214+ await App . Cache . get ( 'script:' + script . id . toString ( ) )
199215 ) ;
200216 if ( context ) {
201217 context . destruct ( ) ;
202218 }
203219 if ( script . type != SCRIPT_TYPE_CRONTAB ) {
204- return top ! . postMessage ( { action : 'disable' } , '*' ) ;
220+ return top ? .postMessage ( { action : 'disable' } , '*' ) ;
205221 }
206222 const list = cronjobMap . get ( script . id ) ;
207223 if ( ! list ) {
208- return top ! . postMessage ( { action : 'disable' } , '*' ) ;
224+ return top ? .postMessage ( { action : 'disable' } , '*' ) ;
209225 }
210226 list . forEach ( ( val ) => {
211227 val . stop ( ) ;
212228 } ) ;
213229 cronjobMap . delete ( script . id ) ;
214- return top ! . postMessage ( { action : 'disable' } , '*' ) ;
230+ return top ? .postMessage ( { action : 'disable' } , '*' ) ;
215231}
216232
217233async function stop ( script : Script , isdebug : boolean ) {
218234 const context = < SandboxContext > (
219- await App . Cache . get ( 'script:' + ( isdebug ? 'debug:' : '' ) + script . id )
235+ await App . Cache . get ( 'script:' + ( isdebug ? 'debug:' : '' ) + script . id . toString ( ) )
220236 ) ;
221237 if ( context ) {
222238 if ( script . type == SCRIPT_TYPE_CRONTAB ) {
@@ -225,7 +241,7 @@ async function stop(script: Script, isdebug: boolean) {
225241 context . destruct ( ) ;
226242 }
227243 }
228- return top ! . postMessage ( { action : 'stop' } , '*' ) ;
244+ return top ? .postMessage ( { action : 'stop' } , '*' ) ;
229245}
230246
231247function getWeek ( date : Date ) {
0 commit comments