| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const ScriptContext = require('./script_context'); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 22:44:46 -05:00
										 |  |  | async function executeScript(dataKey, script, params) { | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  |     log.info('Executing script: ' + script); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 22:44:46 -05:00
										 |  |  |     const ctx = new ScriptContext(dataKey); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const paramsStr = getParams(params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     let ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-02-24 21:23:04 -05:00
										 |  |  |         ret = await (function() { return eval(`const api = this; (${script})(${paramsStr})`); }.call(ctx)); | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  | const timeouts = {}; | 
					
						
							|  |  |  | const intervals = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function clearExistingJob(name) { | 
					
						
							|  |  |  |     if (timeouts[name]) { | 
					
						
							|  |  |  |         clearTimeout(timeouts[name]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         delete timeouts[name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (intervals[name]) { | 
					
						
							|  |  |  |         clearInterval(intervals[name]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         delete intervals[name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function setJob(opts) { | 
					
						
							|  |  |  |     clearExistingJob(opts.name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (opts.runEveryMs && opts.runEveryMs > 0) { | 
					
						
							|  |  |  |         intervals[opts.name] = setInterval(() => executeScript(null, opts.job, opts.params), opts.runEveryMs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (opts.initialRunAfterMs && opts.initialRunAfterMs > 0) { | 
					
						
							|  |  |  |         timeouts[opts.name] = setTimeout(() => executeScript(null, opts.job, opts.params), opts.initialRunAfterMs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | function getParams(params) { | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     if (!params) { | 
					
						
							|  |  |  |         return params; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 09:53:36 -05:00
										 |  |  |     return params.map(p => { | 
					
						
							|  |  |  |         if (typeof p === "string" && p.startsWith("!@#Function: ")) { | 
					
						
							|  |  |  |             return p.substr(13); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return JSON.stringify(p); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }).join(","); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     executeScript, | 
					
						
							|  |  |  |     setJob | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | }; |